-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow for specifying custom json serialization
- Loading branch information
1 parent
1e32d38
commit 90123e6
Showing
11 changed files
with
118 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
LitRedis.Core/Implementations/DefaultLitRedisSystemTextJsonOptionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Text.Json; | ||
using LitRedis.Core.Interfaces; | ||
|
||
namespace LitRedis.Core.Implementations; | ||
|
||
public class DefaultLitRedisSystemTextJsonOptionsProvider(JsonSerializerOptions options) | ||
: ILitRedisSystemTextJsonOptionsProvider | ||
{ | ||
public JsonSerializerOptions GetOptions() => options; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
LitRedis.Core/Implementations/LitRedisSystemTextJsonSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json; | ||
using LitRedis.Core.Interfaces; | ||
|
||
namespace LitRedis.Core.Implementations; | ||
|
||
public class LitRedisSystemTextJsonSerializer : ILitRedisJsonSerializer | ||
{ | ||
private readonly ILitRedisSystemTextJsonOptionsProvider _optionsProvider; | ||
|
||
public LitRedisSystemTextJsonSerializer(ILitRedisSystemTextJsonOptionsProvider optionsProvider) | ||
{ | ||
_optionsProvider = optionsProvider; | ||
} | ||
|
||
public string Serialize<T>(T value) => JsonSerializer.Serialize(value, _optionsProvider.GetOptions()); | ||
|
||
public T Deserialize<T>(string value) => JsonSerializer.Deserialize<T>(value, _optionsProvider.GetOptions()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace LitRedis.Core.Interfaces; | ||
|
||
public interface ILitRedisJsonSerializer | ||
{ | ||
string Serialize<T>(T value); | ||
|
||
T Deserialize<T>(string value); | ||
} |
8 changes: 8 additions & 0 deletions
8
LitRedis.Core/Interfaces/ILitRedisSystemTextJsonOptionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Text.Json; | ||
|
||
namespace LitRedis.Core.Interfaces; | ||
|
||
public interface ILitRedisSystemTextJsonOptionsProvider | ||
{ | ||
JsonSerializerOptions GetOptions(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
LitRedis.Tests/Core/Implementations/JsonSerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Drawing; | ||
using System.Dynamic; | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
using LitRedis.Core.Builders; | ||
using LitRedis.Core.Interfaces; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace LitRedis.Tests.Core.Implementations; | ||
|
||
class SampleClass | ||
{ | ||
public string Name { get; set; } | ||
public int Age { get; set; } | ||
public ExpandoObject Custom { get; set; } | ||
} | ||
|
||
[TestClass] | ||
public class JsonSerializationTests | ||
{ | ||
[TestMethod] | ||
public void Json_Serializer_Should_Handle_Expando() | ||
{ | ||
dynamic custom = new ExpandoObject(); | ||
custom.Color = "Red"; | ||
custom.Size = "Large"; | ||
|
||
var sample = new SampleClass | ||
{ | ||
Name = "John Doe", | ||
Age = 30, | ||
Custom = custom | ||
}; | ||
|
||
var sp = new LitRedisServiceCollectionBuilder(new ServiceCollection()).ServiceCollection.BuildServiceProvider(); | ||
var options = sp.GetRequiredService<ILitRedisSystemTextJsonOptionsProvider>().GetOptions(); | ||
var serialized = JsonSerializer.Serialize(sample, options); | ||
var deserialized = JsonSerializer.Deserialize<SampleClass>(serialized, options); | ||
var serializedAgain = JsonSerializer.Serialize(deserialized, options); | ||
serializedAgain.Should() | ||
.Be("{\"name\":\"John Doe\",\"age\":30,\"custom\":{\"Color\":\"Red\",\"Size\":\"Large\"}}"); | ||
dynamic c = deserialized.Custom; | ||
string color = c.Color.ToString(); | ||
color.Should().Be("Red"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters