/
JsonSerializer.cs
59 lines (54 loc) · 1.79 KB
/
JsonSerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Text.Json;
using StackExchange.Redis;
namespace CachingFramework.Redis.Serializers
{
/// <summary>
/// Class JsonSerializer.
/// </summary>
public class JsonSerializer : Contracts.SerializerBase
{
/// <summary>
/// The _settings
/// </summary>
private readonly JsonSerializerOptions _settings;
/// <summary>
/// Initializes a new instance of the <see cref="JsonSerializer"/> class.
/// </summary>
public JsonSerializer()
{
_settings = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
};
_settings.Converters.Add(new HandleSpecialDoublesAsStrings());
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonSerializer"/> class.
/// </summary>
/// <param name="settings">The settings.</param>
public JsonSerializer(JsonSerializerOptions settings)
{
_settings = settings;
}
/// <summary>
/// Serializes the specified value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns>System.Byte[].</returns>
public override RedisValue Serialize<T>(T value)
{
return System.Text.Json.JsonSerializer.Serialize(value, _settings);
}
/// <summary>
/// Deserializes the specified value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns>T.</returns>
public override T Deserialize<T>(RedisValue value)
{
return System.Text.Json.JsonSerializer.Deserialize<T>(value, _settings);
}
}
}