Skip to content

Commit da200b6

Browse files
Pass in JsonSerializerOptions to generic methods (#187)
* Pass in JsonSerializerOptions to generic methods addresses #186 * move the '?' to the right place * Add unit tests for JsonSerializerOptions * add missing json path --------- Co-authored-by: shacharPash <[email protected]> Co-authored-by: shacharPash <[email protected]>
1 parent b178f7a commit da200b6

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

src/NRedisStack/Json/IJsonCommands.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using NRedisStack.Json.DataTypes;
22
using StackExchange.Redis;
3+
using System.Text.Json;
34

45
namespace NRedisStack;
56

@@ -103,7 +104,7 @@ public interface IJsonCommands
103104
/// <param name="newLine">sets the string that's printed at the end of each line</param>
104105
/// <param name="space">sets the string that's put between a key and a value</param>
105106
/// <param name="path">the path to get.</param>
106-
/// <returns>The requested Items</returns>
107+
/// <returns>The requested items</returns>
107108
/// <remarks><seealso href="https://redis.io/commands/json.get"/></remarks>
108109
RedisResult Get(RedisKey key, RedisValue? indent = null, RedisValue? newLine = null, RedisValue? space = null, RedisValue? path = null);
109110

@@ -119,17 +120,18 @@ public interface IJsonCommands
119120
RedisResult Get(RedisKey key, string[] paths, RedisValue? indent = null, RedisValue? newLine = null, RedisValue? space = null);
120121

121122
/// <summary>
122-
/// Generically gets an Item stored in Redis.
123+
/// Generically gets an item stored in Redis.
123124
/// </summary>
124125
/// <param name="key">The key to retrieve</param>
125126
/// <param name="path">The path to retrieve</param>
127+
/// <param name="serializerOptions">Json serializer options to use for deserialization.</param>
126128
/// <typeparam name="T">The type retrieved</typeparam>
127129
/// <returns>The object requested</returns>
128130
/// <remarks><seealso href="https://redis.io/commands/json.get"/></remarks>
129-
T? Get<T>(RedisKey key, string path = "$");
131+
T? Get<T>(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default);
130132

131133
/// <summary>
132-
/// retrieves a group of items stored in redis, appropriate if the path will resolve to multiple records.
134+
/// Retrieves a group of items stored in Redis, appropriate if the path will resolve to multiple records.
133135
/// </summary>
134136
/// <param name="key">The key to pull from.</param>
135137
/// <param name="path">The path to pull.</param>

src/NRedisStack/Json/IJsonCommandsAsync.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using NRedisStack.Json.DataTypes;
22
using StackExchange.Redis;
3+
using System.Text.Json;
34

45
namespace NRedisStack;
56

@@ -123,10 +124,11 @@ public interface IJsonCommandsAsync
123124
/// </summary>
124125
/// <param name="key">The key to retrieve</param>
125126
/// <param name="path">The path to retrieve</param>
127+
/// <param name="serializerOptions">Json serializer options to use for deserialization.</param>
126128
/// <typeparam name="T">The type retrieved</typeparam>
127129
/// <returns>The object requested</returns>
128130
/// <remarks><seealso href="https://redis.io/commands/json.get"/></remarks>
129-
Task<T?> GetAsync<T>(RedisKey key, string path = "$");
131+
Task<T?> GetAsync<T>(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default);
130132

131133
/// <summary>
132134
/// retrieves a group of items stored in redis, appropriate if the path will resolve to multiple records.

src/NRedisStack/Json/JsonCommands.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,15 @@ public RedisResult Get(RedisKey key, string[] paths, RedisValue? indent = null,
222222
}
223223

224224
/// <inheritdoc/>
225-
public T? Get<T>(RedisKey key, string path = "$")
225+
public T? Get<T>(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default)
226226
{
227227
var res = _db.Execute(JsonCommandBuilder.Get<T>(key, path));
228228
if (res.Type == ResultType.BulkString)
229229
{
230230
var arr = JsonSerializer.Deserialize<JsonArray>(res.ToString()!);
231231
if (arr?.Count > 0)
232232
{
233-
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(arr[0]));
233+
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(arr[0]), serializerOptions);
234234
}
235235
}
236236

src/NRedisStack/Json/JsonCommandsAsync.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ public async Task<RedisResult> GetAsync(RedisKey key, string[] paths, RedisValue
7878
return await _db.ExecuteAsync(JsonCommandBuilder.Get(key, paths, indent, newLine, space));
7979
}
8080

81-
public async Task<T?> GetAsync<T>(RedisKey key, string path = "$")
81+
public async Task<T?> GetAsync<T>(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default)
8282
{
8383
var res = await _db.ExecuteAsync(JsonCommandBuilder.Get<T>(key, path));
8484
if (res.Type == ResultType.BulkString)
8585
{
8686
var arr = JsonSerializer.Deserialize<JsonArray>(res.ToString()!);
8787
if (arr?.Count > 0)
8888
{
89-
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(arr[0]));
89+
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(arr[0]), serializerOptions);
9090
}
9191
}
9292

tests/NRedisStack.Tests/Json/JsonTests.cs

+20-6
Original file line numberDiff line numberDiff line change
@@ -681,14 +681,21 @@ public async Task ForgetAsync()
681681
public void Get()
682682
{
683683
var commands = new JsonCommands(redisFixture.Redis.GetDatabase());
684-
var keys = CreateKeyNames(2);
684+
var keys = CreateKeyNames(3);
685685
var key = keys[0];
686686
var complexKey = keys[1];
687+
var caseInsensitiveKey = keys[2];
687688
commands.Set(key, "$", new Person() { Age = 35, Name = "Alice" });
688689
commands.Set(complexKey, "$", new { a = new Person() { Age = 35, Name = "Alice" }, b = new { a = new Person() { Age = 35, Name = "Alice" } } });
690+
commands.Set(caseInsensitiveKey, "$", new { name = "Alice", AGE = 35 });
689691
var result = commands.Get<Person>(key);
690692
Assert.Equal("Alice", result!.Name);
691693
Assert.Equal(35, result.Age);
694+
var jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
695+
result = commands.Get<Person>(caseInsensitiveKey, "$", jsonOptions);
696+
Assert.NotNull(result);
697+
Assert.Equal("Alice", result!.Name);
698+
Assert.Equal(35, result.Age);
692699
var people = commands.GetEnumerable<Person>(complexKey, "$..a").ToArray();
693700
Assert.Equal(2, people.Length);
694701
Assert.Equal("Alice", people[0]!.Name);
@@ -701,14 +708,21 @@ public void Get()
701708
public async Task GetAsync()
702709
{
703710
var commands = new JsonCommandsAsync(redisFixture.Redis.GetDatabase());
704-
var keys = CreateKeyNames(2);
711+
var keys = CreateKeyNames(3);
705712
var key = keys[0];
706713
var complexKey = keys[1];
714+
var caseInsensitiveKey = keys[2];
707715
await commands.SetAsync(key, "$", new Person() { Age = 35, Name = "Alice" });
708716
await commands.SetAsync(complexKey, "$", new { a = new Person() { Age = 35, Name = "Alice" }, b = new { a = new Person() { Age = 35, Name = "Alice" } } });
717+
await commands.SetAsync(caseInsensitiveKey, "$", new { name = "Alice", AGE = 35 });
709718
var result = await commands.GetAsync<Person>(key);
710719
Assert.Equal("Alice", result!.Name);
711720
Assert.Equal(35, result.Age);
721+
var jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
722+
result = await commands.GetAsync<Person>(caseInsensitiveKey, "$", jsonOptions);
723+
Assert.NotNull(result);
724+
Assert.Equal("Alice", result!.Name);
725+
Assert.Equal(35, result.Age);
712726
var people = (await commands.GetEnumerableAsync<Person>(complexKey, "$..a")).ToArray();
713727
Assert.Equal(2, people.Length);
714728
Assert.Equal("Alice", people[0]!.Name);
@@ -730,8 +744,8 @@ public void MSet()
730744
new KeyPathValue(key1, "$", new { a = "hello" }),
731745
new KeyPathValue(key2, "$", new { a = "world" })
732746
};
733-
commands.MSet(values)
734-
;
747+
commands.MSet(values);
748+
735749
var result = commands.MGet(keys.Select(x => new RedisKey(x)).ToArray(), "$.a");
736750

737751
Assert.Equal("[\"hello\"]", result[0].ToString());
@@ -753,8 +767,8 @@ public async Task MSetAsync()
753767
new KeyPathValue(key1, "$", new { a = "hello" }),
754768
new KeyPathValue(key2, "$", new { a = "world" })
755769
};
756-
await commands.MSetAsync(values)
757-
;
770+
await commands.MSetAsync(values);
771+
758772
var result = await commands.MGetAsync(keys.Select(x => new RedisKey(x)).ToArray(), "$.a");
759773

760774
Assert.Equal("[\"hello\"]", result[0].ToString());

0 commit comments

Comments
 (0)