diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index 6ea7babe..8c8621db 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -1,5 +1,6 @@ using NRedisStack.Json.DataTypes; using StackExchange.Redis; +using System.Text.Json; namespace NRedisStack; @@ -103,7 +104,7 @@ public interface IJsonCommands /// sets the string that's printed at the end of each line /// sets the string that's put between a key and a value /// the path to get. - /// The requested Items + /// The requested items /// RedisResult Get(RedisKey key, RedisValue? indent = null, RedisValue? newLine = null, RedisValue? space = null, RedisValue? path = null); @@ -119,17 +120,18 @@ public interface IJsonCommands RedisResult Get(RedisKey key, string[] paths, RedisValue? indent = null, RedisValue? newLine = null, RedisValue? space = null); /// - /// Generically gets an Item stored in Redis. + /// Generically gets an item stored in Redis. /// /// The key to retrieve /// The path to retrieve + /// Json serializer options to use for deserialization. /// The type retrieved /// The object requested /// - T? Get(RedisKey key, string path = "$"); + T? Get(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default); /// - /// retrieves a group of items stored in redis, appropriate if the path will resolve to multiple records. + /// Retrieves a group of items stored in Redis, appropriate if the path will resolve to multiple records. /// /// The key to pull from. /// The path to pull. diff --git a/src/NRedisStack/Json/IJsonCommandsAsync.cs b/src/NRedisStack/Json/IJsonCommandsAsync.cs index 5e2a1351..1578bbd0 100644 --- a/src/NRedisStack/Json/IJsonCommandsAsync.cs +++ b/src/NRedisStack/Json/IJsonCommandsAsync.cs @@ -1,5 +1,6 @@ using NRedisStack.Json.DataTypes; using StackExchange.Redis; +using System.Text.Json; namespace NRedisStack; @@ -123,10 +124,11 @@ public interface IJsonCommandsAsync /// /// The key to retrieve /// The path to retrieve + /// Json serializer options to use for deserialization. /// The type retrieved /// The object requested /// - Task GetAsync(RedisKey key, string path = "$"); + Task GetAsync(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default); /// /// retrieves a group of items stored in redis, appropriate if the path will resolve to multiple records. diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 582036fa..98d39dfa 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -222,7 +222,7 @@ public RedisResult Get(RedisKey key, string[] paths, RedisValue? indent = null, } /// - public T? Get(RedisKey key, string path = "$") + public T? Get(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default) { var res = _db.Execute(JsonCommandBuilder.Get(key, path)); if (res.Type == ResultType.BulkString) @@ -230,7 +230,7 @@ public RedisResult Get(RedisKey key, string[] paths, RedisValue? indent = null, var arr = JsonSerializer.Deserialize(res.ToString()!); if (arr?.Count > 0) { - return JsonSerializer.Deserialize(JsonSerializer.Serialize(arr[0])); + return JsonSerializer.Deserialize(JsonSerializer.Serialize(arr[0]), serializerOptions); } } diff --git a/src/NRedisStack/Json/JsonCommandsAsync.cs b/src/NRedisStack/Json/JsonCommandsAsync.cs index a4d12639..1e8b48b6 100644 --- a/src/NRedisStack/Json/JsonCommandsAsync.cs +++ b/src/NRedisStack/Json/JsonCommandsAsync.cs @@ -78,7 +78,7 @@ public async Task GetAsync(RedisKey key, string[] paths, RedisValue return await _db.ExecuteAsync(JsonCommandBuilder.Get(key, paths, indent, newLine, space)); } - public async Task GetAsync(RedisKey key, string path = "$") + public async Task GetAsync(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default) { var res = await _db.ExecuteAsync(JsonCommandBuilder.Get(key, path)); if (res.Type == ResultType.BulkString) @@ -86,7 +86,7 @@ public async Task GetAsync(RedisKey key, string[] paths, RedisValue var arr = JsonSerializer.Deserialize(res.ToString()!); if (arr?.Count > 0) { - return JsonSerializer.Deserialize(JsonSerializer.Serialize(arr[0])); + return JsonSerializer.Deserialize(JsonSerializer.Serialize(arr[0]), serializerOptions); } } diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 36b8fa3a..4a67cff8 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -681,14 +681,21 @@ public async Task ForgetAsync() public void Get() { var commands = new JsonCommands(redisFixture.Redis.GetDatabase()); - var keys = CreateKeyNames(2); + var keys = CreateKeyNames(3); var key = keys[0]; var complexKey = keys[1]; + var caseInsensitiveKey = keys[2]; commands.Set(key, "$", new Person() { Age = 35, Name = "Alice" }); commands.Set(complexKey, "$", new { a = new Person() { Age = 35, Name = "Alice" }, b = new { a = new Person() { Age = 35, Name = "Alice" } } }); + commands.Set(caseInsensitiveKey, "$", new { name = "Alice", AGE = 35 }); var result = commands.Get(key); Assert.Equal("Alice", result!.Name); Assert.Equal(35, result.Age); + var jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; + result = commands.Get(caseInsensitiveKey, "$", jsonOptions); + Assert.NotNull(result); + Assert.Equal("Alice", result!.Name); + Assert.Equal(35, result.Age); var people = commands.GetEnumerable(complexKey, "$..a").ToArray(); Assert.Equal(2, people.Length); Assert.Equal("Alice", people[0]!.Name); @@ -701,14 +708,21 @@ public void Get() public async Task GetAsync() { var commands = new JsonCommandsAsync(redisFixture.Redis.GetDatabase()); - var keys = CreateKeyNames(2); + var keys = CreateKeyNames(3); var key = keys[0]; var complexKey = keys[1]; + var caseInsensitiveKey = keys[2]; await commands.SetAsync(key, "$", new Person() { Age = 35, Name = "Alice" }); await commands.SetAsync(complexKey, "$", new { a = new Person() { Age = 35, Name = "Alice" }, b = new { a = new Person() { Age = 35, Name = "Alice" } } }); + await commands.SetAsync(caseInsensitiveKey, "$", new { name = "Alice", AGE = 35 }); var result = await commands.GetAsync(key); Assert.Equal("Alice", result!.Name); Assert.Equal(35, result.Age); + var jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; + result = await commands.GetAsync(caseInsensitiveKey, "$", jsonOptions); + Assert.NotNull(result); + Assert.Equal("Alice", result!.Name); + Assert.Equal(35, result.Age); var people = (await commands.GetEnumerableAsync(complexKey, "$..a")).ToArray(); Assert.Equal(2, people.Length); Assert.Equal("Alice", people[0]!.Name); @@ -730,8 +744,8 @@ public void MSet() new KeyPathValue(key1, "$", new { a = "hello" }), new KeyPathValue(key2, "$", new { a = "world" }) }; - commands.MSet(values) -; + commands.MSet(values); + var result = commands.MGet(keys.Select(x => new RedisKey(x)).ToArray(), "$.a"); Assert.Equal("[\"hello\"]", result[0].ToString()); @@ -753,8 +767,8 @@ public async Task MSetAsync() new KeyPathValue(key1, "$", new { a = "hello" }), new KeyPathValue(key2, "$", new { a = "world" }) }; - await commands.MSetAsync(values) -; + await commands.MSetAsync(values); + var result = await commands.MGetAsync(keys.Select(x => new RedisKey(x)).ToArray(), "$.a"); Assert.Equal("[\"hello\"]", result[0].ToString());