Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JsonSerializerOptions parameter to Set and SetAsync methods. #223

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/NRedisStack/Json/IJsonCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@ public interface IJsonCommands
/// <param name="path">The path to set within the key.</param>
/// <param name="obj">The value to set.</param>
/// <param name="when">When to set the value.</param>
/// <param name="serializerOptions">Json serializer options to use for serialization.</param>
/// <returns>The disposition of the command</returns>
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
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);

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

/// <summary>
/// Sets or updates the JSON value of one or more keys.
Expand Down
6 changes: 4 additions & 2 deletions src/NRedisStack/Json/IJsonCommandsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@ public interface IJsonCommandsAsync
/// <param name="path">The path to set within the key.</param>
/// <param name="obj">The value to set.</param>
/// <param name="when">When to set the value.</param>
/// <param name="serializerOptions">Json serializer options to use for serialization.</param>
/// <returns>The disposition of the command</returns>
/// <remarks><seealso href="https://redis.io/commands/json.set"/></remarks>
Task<bool> SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always);
Task<bool> SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always, JsonSerializerOptions? serializerOptions = default);

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

/// <summary>
/// Set json file from the provided file Path.
Expand Down
8 changes: 4 additions & 4 deletions src/NRedisStack/Json/JsonCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public RedisResult[] Resp(RedisKey key, string? path = null)
}

/// <inheritdoc/>
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);
}

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

/// <inheritdoc/>
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();
}

Expand Down
9 changes: 5 additions & 4 deletions src/NRedisStack/Json/JsonCommandsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ public async Task<RedisResult[]> RespAsync(RedisKey key, string? path = null)
return (RedisResult[])result!;
}

public Task<bool> SetAsync(RedisKey key, RedisValue path, object obj, When when = When.Always)
/// <inheritdoc/>
public Task<bool> 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);
}

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

/// <inheritdoc/>
public async Task<bool> MergeAsync(RedisKey key, RedisValue path, object obj)
public async Task<bool> 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();
}

Expand Down
78 changes: 78 additions & 0 deletions tests/NRedisStack.Tests/Json/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person>(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<Person>(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);
}
}
1 change: 1 addition & 0 deletions tests/NRedisStack.Tests/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class Person
{
public string? Name { get; set; }
public int Age { get; set; }
public DateTime? Birthday;
}
}