Skip to content

Commit 7a648d6

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

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-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

+34
Original file line numberDiff line numberDiff line change
@@ -1152,4 +1152,38 @@ public async Task TestGetIssue198_Async()
11521152
Assert.Null(result);
11531153
Assert.Null(result2);
11541154
}
1155+
1156+
[Fact]
1157+
public void TestSetWithSerializationOption()
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+
commands.Set(key, "$", person, serializerOptions: jsonOptions);
1165+
Person? result = commands.Get<Person>(key, serializerOptions: jsonOptions);
1166+
Assert.NotNull(result);
1167+
Assert.Equal(person.Name, result!.Name);
1168+
Assert.Equal(person.Age, result!.Age);
1169+
Assert.NotNull(result!.Birthday);
1170+
Assert.Equal(person.Birthday, result!.Birthday);
1171+
}
1172+
1173+
[Fact]
1174+
public async Task TestSetAsyncWithSerializationOption()
1175+
{
1176+
var commands = new JsonCommands(redisFixture.Redis.GetDatabase());
1177+
var keys = CreateKeyNames(1);
1178+
var key = keys[0];
1179+
var jsonOptions = new JsonSerializerOptions { IncludeFields = true };
1180+
var person = new Person { Name = "Developer", Age = 23, Birthday = DateTime.Today };
1181+
await commands.SetAsync(key, "$", person, serializerOptions: jsonOptions);
1182+
Person? result = await commands.GetAsync<Person>(key, serializerOptions: jsonOptions);
1183+
Assert.NotNull(result);
1184+
Assert.Equal(person.Name, result!.Name);
1185+
Assert.Equal(person.Age, result!.Age);
1186+
Assert.NotNull(result!.Birthday);
1187+
Assert.Equal(person.Birthday, result!.Birthday);
1188+
}
11551189
}

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)