Skip to content

Commit 9f2b62b

Browse files
committed
Added JsonSerializerOptions parameter to Set and SetAsync methods.
Closed #222
1 parent 6a17dfe commit 9f2b62b

File tree

6 files changed

+93
-12
lines changed

6 files changed

+93
-12
lines changed

src/NRedisStack/Json/IJsonCommands.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ public interface IJsonCommands
193193
/// <param name="path">The path to set within the key.</param>
194194
/// <param name="obj">The value to set.</param>
195195
/// <param name="when">When to set the value.</param>
196+
/// <param name="serializerOptions">Json serializer options to use for serialization.</param>
196197
/// <returns>The disposition of the command</returns>
197198
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
198-
bool Set(RedisKey key, RedisValue path, object obj, When when = When.Always);
199+
bool Set(RedisKey key, RedisValue path, object obj, When when = When.Always, JsonSerializerOptions? serializerOptions = default);
199200

200201
/// <summary>
201202
/// Set's the key/path to the provided value.
@@ -235,9 +236,10 @@ public interface IJsonCommands
235236
/// <param name="key">The key.</param>
236237
/// <param name="path">The path to set within the key.</param>
237238
/// <param name="obj">The value to set.</param>
239+
/// <param name="serializerOptions">Json serializer options to use for serialization.</param>
238240
/// <returns>The disposition of the command</returns>
239241
/// <remarks><seealso href="https://redis.io/commands/json.merge"/></remarks>
240-
bool Merge(RedisKey key, RedisValue path, object obj);
242+
bool Merge(RedisKey key, RedisValue path, object obj, JsonSerializerOptions? serializerOptions = default);
241243

242244
/// <summary>
243245
/// Sets or updates the JSON value of one or more keys.

src/NRedisStack/Json/IJsonCommandsAsync.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ public interface IJsonCommandsAsync
193193
/// <param name="path">The path to set within the key.</param>
194194
/// <param name="obj">The value to set.</param>
195195
/// <param name="when">When to set the value.</param>
196+
/// <param name="serializerOptions">Json serializer options to use for serialization.</param>
196197
/// <returns>The disposition of the command</returns>
197198
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
198-
Task<bool> SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always);
199+
Task<bool> SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always, JsonSerializerOptions? serializerOptions = default);
199200

200201
/// <summary>
201202
/// Set's the key/path to the provided value.
@@ -235,9 +236,10 @@ public interface IJsonCommandsAsync
235236
/// <param name="key">The key.</param>
236237
/// <param name="path">The path to set within the key.</param>
237238
/// <param name="obj">The value to set.</param>
239+
/// <param name="serializerOptions">Json serializer options to use for serialization.</param>
238240
/// <returns>The disposition of the command</returns>
239241
/// <remarks><seealso href="https://redis.io/commands/json.merge"/></remarks>
240-
Task<bool> MergeAsync(RedisKey key, RedisValue path, object obj);
242+
Task<bool> MergeAsync(RedisKey key, RedisValue path, object obj, JsonSerializerOptions? serializerOptions = default);
241243

242244
/// <summary>
243245
/// Set json file from the provided file Path.

src/NRedisStack/Json/JsonCommands.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public RedisResult[] Resp(RedisKey key, string? path = null)
2828
}
2929

3030
/// <inheritdoc/>
31-
public bool Set(RedisKey key, RedisValue path, object obj, When when = When.Always)
31+
public bool Set(RedisKey key, RedisValue path, object obj, When when = When.Always, JsonSerializerOptions? serializerOptions = default)
3232
{
33-
string json = JsonSerializer.Serialize(obj);
33+
string json = JsonSerializer.Serialize(obj, options: serializerOptions);
3434
return Set(key, path, json, when);
3535
}
3636

@@ -53,9 +53,9 @@ public bool Merge(RedisKey key, RedisValue path, RedisValue json)
5353
}
5454

5555
/// <inheritdoc/>
56-
public bool Merge(RedisKey key, RedisValue path, object obj)
56+
public bool Merge(RedisKey key, RedisValue path, object obj, JsonSerializerOptions? serializerOptions = default)
5757
{
58-
string json = JsonSerializer.Serialize(obj);
58+
string json = JsonSerializer.Serialize(obj, options: serializerOptions);
5959
return _db.Execute(JsonCommandBuilder.Merge(key, path, json)).OKtoBoolean();
6060
}
6161

src/NRedisStack/Json/JsonCommandsAsync.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ public async Task<RedisResult[]> RespAsync(RedisKey key, string? path = null)
133133
return (RedisResult[])result!;
134134
}
135135

136-
public Task<bool> SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always)
136+
/// <inheritdoc/>
137+
public Task<bool> SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always, JsonSerializerOptions? serializerOptions = default)
137138
{
138-
string json = JsonSerializer.Serialize(obj);
139+
string json = JsonSerializer.Serialize(obj, options: serializerOptions);
139140
return SetAsync(key, path, json, when);
140141
}
141142

@@ -156,9 +157,9 @@ public async Task<bool> MergeAsync(RedisKey key, RedisValue path, RedisValue jso
156157
}
157158

158159
/// <inheritdoc/>
159-
public async Task<bool> MergeAsync(RedisKey key, RedisValue path, object obj)
160+
public async Task<bool> MergeAsync(RedisKey key, RedisValue path, object obj, JsonSerializerOptions? serializerOptions = default)
160161
{
161-
string json = JsonSerializer.Serialize(obj);
162+
string json = JsonSerializer.Serialize(obj, options: serializerOptions);
162163
return (await _db.ExecuteAsync(JsonCommandBuilder.Merge(key, path, json))).OKtoBoolean();
163164
}
164165

tests/NRedisStack.Tests/Json/JsonTests.cs

+75
Original file line numberDiff line numberDiff line change
@@ -1152,4 +1152,79 @@ public async Task TestGetIssue198_Async()
11521152
Assert.Null(result);
11531153
Assert.Null(result2);
11541154
}
1155+
1156+
[Fact]
1157+
public void TestSetWithSerializationOptions()
1158+
{
1159+
var commands = new JsonCommands(redisFixture.Redis.GetDatabase());
1160+
var keys = CreateKeyNames(1);
1161+
var key = keys[0];
1162+
var jsonOptions = new JsonSerializerOptions { IncludeFields = true };
1163+
var person = new Person { Name = "Developer", Age = 23, Birthday = DateTime.Today };
1164+
1165+
commands.Set(key, "$", person, serializerOptions: jsonOptions);
1166+
Person? result = commands.Get<Person>(key, serializerOptions: jsonOptions);
1167+
1168+
Assert.NotNull(result);
1169+
Assert.Equal(person.Name, result!.Name);
1170+
Assert.Equal(person.Age, result!.Age);
1171+
Assert.NotNull(result!.Birthday);
1172+
Assert.Equal(person.Birthday, result!.Birthday);
1173+
}
1174+
1175+
[Fact]
1176+
public async Task TestSetWithSerializationOptionsAsync()
1177+
{
1178+
var commands = new JsonCommands(redisFixture.Redis.GetDatabase());
1179+
var keys = CreateKeyNames(1);
1180+
var key = keys[0];
1181+
var jsonOptions = new JsonSerializerOptions { IncludeFields = true };
1182+
var person = new Person { Name = "Developer", Age = 23, Birthday = DateTime.Today };
1183+
1184+
await commands.SetAsync(key, "$", person, serializerOptions: jsonOptions);
1185+
Person? result = await commands.GetAsync<Person>(key, serializerOptions: jsonOptions);
1186+
1187+
Assert.NotNull(result);
1188+
Assert.Equal(person.Name, result!.Name);
1189+
Assert.Equal(person.Age, result!.Age);
1190+
Assert.NotNull(result!.Birthday);
1191+
Assert.Equal(person.Birthday, result!.Birthday);
1192+
}
1193+
1194+
[SkipIfRedis("7.1.242")]
1195+
public void MergeWithSerializationOptions()
1196+
{
1197+
string expected = "{\"age\":23,\"birthday\":\"2023-12-31T00:00:00\",\"name\":\"Developer\"}";
1198+
1199+
var commands = new JsonCommands(redisFixture.Redis.GetDatabase());
1200+
var keys = CreateKeyNames(1);
1201+
var key = keys[0];
1202+
commands.Set(key, "$", new { age = 5, birthday = new DateTime(2000, 1, 1) });
1203+
1204+
var jsonOptions = new JsonSerializerOptions { IncludeFields = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
1205+
var person = new Person { Name = "Developer", Age = 23, Birthday = new DateTime(2023, 12, 31) };
1206+
commands.Merge(key, "$", person, serializerOptions: jsonOptions);
1207+
string actual = commands.Get(key).ToString();
1208+
1209+
Assert.Equal(expected, actual);
1210+
}
1211+
1212+
[SkipIfRedis("7.1.242")]
1213+
public async Task MergeWithSerializationOptionsAsync()
1214+
{
1215+
string expected = "{\"age\":23,\"birthday\":\"2023-12-31T00:00:00\",\"name\":\"Developer\"}";
1216+
1217+
var commands = new JsonCommands(redisFixture.Redis.GetDatabase());
1218+
var keys = CreateKeyNames(1);
1219+
var key = keys[0];
1220+
await commands.SetAsync(key, "$", new { age = 5, birthday = new DateTime(2000, 1, 1) });
1221+
1222+
var jsonOptions = new JsonSerializerOptions { IncludeFields = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
1223+
var person = new Person { Name = "Developer", Age = 23, Birthday = new DateTime(2023, 12, 31) };
1224+
await commands.MergeAsync(key, "$", person, serializerOptions: jsonOptions);
1225+
RedisResult rr = await commands.GetAsync(key);
1226+
string actual = rr.ToString();
1227+
1228+
Assert.Equal(expected, actual);
1229+
}
11551230
}

tests/NRedisStack.Tests/Person.cs

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public class Person
44
{
55
public string? Name { get; set; }
66
public int Age { get; set; }
7+
public DateTime? Birthday;
78
}
89
}

0 commit comments

Comments
 (0)