From c1a91b10e4e5e8074b59a66a82df0454ac340f9c Mon Sep 17 00:00:00 2001 From: Ricky Lin Date: Fri, 8 Dec 2023 15:13:07 +0800 Subject: [PATCH 1/4] Added JsonSerializerOptions parameter to Set and SetAsync methods. Closed #222 --- src/NRedisStack/Json/IJsonCommands.cs | 6 +- src/NRedisStack/Json/IJsonCommandsAsync.cs | 6 +- src/NRedisStack/Json/JsonCommands.cs | 8 +-- src/NRedisStack/Json/JsonCommandsAsync.cs | 9 +-- tests/NRedisStack.Tests/Json/JsonTests.cs | 78 ++++++++++++++++++++++ tests/NRedisStack.Tests/Person.cs | 1 + 6 files changed, 96 insertions(+), 12 deletions(-) diff --git a/src/NRedisStack/Json/IJsonCommands.cs b/src/NRedisStack/Json/IJsonCommands.cs index 8c8621db..377084e5 100644 --- a/src/NRedisStack/Json/IJsonCommands.cs +++ b/src/NRedisStack/Json/IJsonCommands.cs @@ -193,9 +193,10 @@ public interface IJsonCommands /// The path to set within the key. /// The value to set. /// When to set the value. + /// Json serializer options to use for serialization. /// The disposition of the command /// - bool Set(RedisKey key, RedisValue path, object obj, When when = When.Always); + bool Set(RedisKey key, RedisValue path, object obj, When when = When.Always, JsonSerializerOptions? serializerOptions = default); /// /// Set's the key/path to the provided value. @@ -235,9 +236,10 @@ public interface IJsonCommands /// The key. /// The path to set within the key. /// The value to set. + /// Json serializer options to use for serialization. /// The disposition of the command /// - bool Merge(RedisKey key, RedisValue path, object obj); + bool Merge(RedisKey key, RedisValue path, object obj, JsonSerializerOptions? serializerOptions = default); /// /// Sets or updates the JSON value of one or more keys. diff --git a/src/NRedisStack/Json/IJsonCommandsAsync.cs b/src/NRedisStack/Json/IJsonCommandsAsync.cs index 1578bbd0..309db723 100644 --- a/src/NRedisStack/Json/IJsonCommandsAsync.cs +++ b/src/NRedisStack/Json/IJsonCommandsAsync.cs @@ -193,9 +193,10 @@ public interface IJsonCommandsAsync /// The path to set within the key. /// The value to set. /// When to set the value. + /// Json serializer options to use for serialization. /// The disposition of the command /// - Task SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always); + Task SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always, JsonSerializerOptions? serializerOptions = default); /// /// Set's the key/path to the provided value. @@ -235,9 +236,10 @@ public interface IJsonCommandsAsync /// The key. /// The path to set within the key. /// The value to set. + /// Json serializer options to use for serialization. /// The disposition of the command /// - Task MergeAsync(RedisKey key, RedisValue path, object obj); + Task MergeAsync(RedisKey key, RedisValue path, object obj, JsonSerializerOptions? serializerOptions = default); /// /// Set json file from the provided file Path. diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 00c780fe..e16bc701 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -28,9 +28,9 @@ public RedisResult[] Resp(RedisKey key, string? path = null) } /// - public bool Set(RedisKey key, RedisValue path, object obj, When when = When.Always) + public bool Set(RedisKey key, RedisValue path, object obj, When when = When.Always, JsonSerializerOptions? serializerOptions = default) { - string json = JsonSerializer.Serialize(obj); + string json = JsonSerializer.Serialize(obj, options: serializerOptions); return Set(key, path, json, when); } @@ -53,9 +53,9 @@ public bool Merge(RedisKey key, RedisValue path, RedisValue json) } /// - public bool Merge(RedisKey key, RedisValue path, object obj) + public bool Merge(RedisKey key, RedisValue path, object obj, JsonSerializerOptions? serializerOptions = default) { - string json = JsonSerializer.Serialize(obj); + string json = JsonSerializer.Serialize(obj, options: serializerOptions); return _db.Execute(JsonCommandBuilder.Merge(key, path, json)).OKtoBoolean(); } diff --git a/src/NRedisStack/Json/JsonCommandsAsync.cs b/src/NRedisStack/Json/JsonCommandsAsync.cs index 3382236b..1e3126a8 100644 --- a/src/NRedisStack/Json/JsonCommandsAsync.cs +++ b/src/NRedisStack/Json/JsonCommandsAsync.cs @@ -133,9 +133,10 @@ public async Task RespAsync(RedisKey key, string? path = null) return (RedisResult[])result!; } - public Task SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always) + /// + public Task SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always, JsonSerializerOptions? serializerOptions = default) { - string json = JsonSerializer.Serialize(obj); + string json = JsonSerializer.Serialize(obj, options: serializerOptions); return SetAsync(key, path, json, when); } @@ -156,9 +157,9 @@ public async Task MergeAsync(RedisKey key, RedisValue path, RedisValue jso } /// - public async Task MergeAsync(RedisKey key, RedisValue path, object obj) + public async Task MergeAsync(RedisKey key, RedisValue path, object obj, JsonSerializerOptions? serializerOptions = default) { - string json = JsonSerializer.Serialize(obj); + string json = JsonSerializer.Serialize(obj, options: serializerOptions); return (await _db.ExecuteAsync(JsonCommandBuilder.Merge(key, path, json))).OKtoBoolean(); } diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 694cc472..b3cb5626 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -1152,4 +1152,82 @@ public async Task TestGetIssue198_Async() Assert.Null(result); Assert.Null(result2); } + + [Fact] + public void TestSetWithSerializationOptions() + { + var commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(1); + var key = keys[0]; + var jsonOptions = new JsonSerializerOptions { IncludeFields = true }; + var person = new Person { Name = "Developer", Age = 23, Birthday = DateTime.Today }; + + commands.Set(key, "$", person, serializerOptions: jsonOptions); + Person? result = commands.Get(key, serializerOptions: jsonOptions); + + Assert.NotNull(result); + Assert.Equal(person.Name, result!.Name); + Assert.Equal(person.Age, result!.Age); + Assert.NotNull(result!.Birthday); + Assert.Equal(person.Birthday, result!.Birthday); + } + + [Fact] + public async Task TestSetWithSerializationOptionsAsync() + { + var commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(1); + var key = keys[0]; + var jsonOptions = new JsonSerializerOptions { IncludeFields = true }; + var person = new Person { Name = "Developer", Age = 23, Birthday = DateTime.Today }; + + await commands.SetAsync(key, "$", person, serializerOptions: jsonOptions); + Person? result = await commands.GetAsync(key, serializerOptions: jsonOptions); + + Assert.NotNull(result); + Assert.Equal(person.Name, result!.Name); + Assert.Equal(person.Age, result!.Age); + Assert.NotNull(result!.Birthday); + Assert.Equal(person.Birthday, result!.Birthday); + } + + [SkipIfRedis("7.1.242")] + public void MergeWithSerializationOptions() + { + string expected = "{\"age\":23,\"birthday\":\"2023-12-31T00:00:00\",\"name\":\"Developer\"}"; + + var commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(1); + var key = keys[0]; + commands.Set(key, "$", new { age = 5, birthday = new DateTime(2000, 1, 1) }); + + var jsonOptions = new JsonSerializerOptions { IncludeFields = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + var person = new Person { Name = "Developer", Age = 23, Birthday = new DateTime(2023, 12, 31) }; + commands.Merge(key, "$", person, serializerOptions: jsonOptions); + RedisResult rr = commands.Get(key); + + Assert.False(rr.IsNull); + string actual = rr.ToString()!; + Assert.Equal(expected, actual); + } + + [SkipIfRedis("7.1.242")] + public async Task MergeWithSerializationOptionsAsync() + { + string expected = "{\"age\":23,\"birthday\":\"2023-12-31T00:00:00\",\"name\":\"Developer\"}"; + + var commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(1); + var key = keys[0]; + await commands.SetAsync(key, "$", new { age = 5, birthday = new DateTime(2000, 1, 1) }); + + var jsonOptions = new JsonSerializerOptions { IncludeFields = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + var person = new Person { Name = "Developer", Age = 23, Birthday = new DateTime(2023, 12, 31) }; + await commands.MergeAsync(key, "$", person, serializerOptions: jsonOptions); + RedisResult rr = await commands.GetAsync(key); + + Assert.False(rr.IsNull); + string actual = rr.ToString()!; + Assert.Equal(expected, actual); + } } \ No newline at end of file diff --git a/tests/NRedisStack.Tests/Person.cs b/tests/NRedisStack.Tests/Person.cs index fc217a05..eed41b9e 100644 --- a/tests/NRedisStack.Tests/Person.cs +++ b/tests/NRedisStack.Tests/Person.cs @@ -4,5 +4,6 @@ public class Person { public string? Name { get; set; } public int Age { get; set; } + public DateTime? Birthday; } } \ No newline at end of file From baeed188cf912963bda54296802c8a75cd58e793 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Sun, 10 Dec 2023 15:00:03 +0200 Subject: [PATCH 2/4] format --- tests/NRedisStack.Tests/Json/JsonTests.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index b3cb5626..052fb836 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -1160,11 +1160,11 @@ public void TestSetWithSerializationOptions() var keys = CreateKeyNames(1); var key = keys[0]; var jsonOptions = new JsonSerializerOptions { IncludeFields = true }; - var person = new Person { Name = "Developer", Age = 23, Birthday = DateTime.Today }; - + var person = new Person { Name = "Developer", Age = 23, Birthday = DateTime.Today }; + commands.Set(key, "$", person, serializerOptions: jsonOptions); - Person? result = commands.Get(key, serializerOptions: jsonOptions); - + Person? result = commands.Get(key, serializerOptions: jsonOptions); + Assert.NotNull(result); Assert.Equal(person.Name, result!.Name); Assert.Equal(person.Age, result!.Age); @@ -1182,8 +1182,8 @@ public async Task TestSetWithSerializationOptionsAsync() var person = new Person { Name = "Developer", Age = 23, Birthday = DateTime.Today }; await commands.SetAsync(key, "$", person, serializerOptions: jsonOptions); - Person? result = await commands.GetAsync(key, serializerOptions: jsonOptions); - + Person? result = await commands.GetAsync(key, serializerOptions: jsonOptions); + Assert.NotNull(result); Assert.Equal(person.Name, result!.Name); Assert.Equal(person.Age, result!.Age); From 4ea006c15d9adcb3c2e82be72ccdb6a245418191 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Sun, 10 Dec 2023 15:01:37 +0200 Subject: [PATCH 3/4] { get; set; } for Birthday --- tests/NRedisStack.Tests/Person.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/NRedisStack.Tests/Person.cs b/tests/NRedisStack.Tests/Person.cs index eed41b9e..e6d2394b 100644 --- a/tests/NRedisStack.Tests/Person.cs +++ b/tests/NRedisStack.Tests/Person.cs @@ -4,6 +4,6 @@ public class Person { public string? Name { get; set; } public int Age { get; set; } - public DateTime? Birthday; + public DateTime? Birthday { get; set; } } } \ No newline at end of file From efcb477b154eac4cf1afc12254b389858dcbf1a3 Mon Sep 17 00:00:00 2001 From: shacharPash Date: Sun, 10 Dec 2023 15:22:09 +0200 Subject: [PATCH 4/4] remove { get; set; } --- tests/NRedisStack.Tests/Person.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/NRedisStack.Tests/Person.cs b/tests/NRedisStack.Tests/Person.cs index e6d2394b..eed41b9e 100644 --- a/tests/NRedisStack.Tests/Person.cs +++ b/tests/NRedisStack.Tests/Person.cs @@ -4,6 +4,6 @@ public class Person { public string? Name { get; set; } public int Age { get; set; } - public DateTime? Birthday { get; set; } + public DateTime? Birthday; } } \ No newline at end of file