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..052fb836 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