/
MsgPackSerializer.cs
56 lines (49 loc) · 1.55 KB
/
MsgPackSerializer.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
using MessagePack;
using StackExchange.Redis;
namespace CachingFramework.Redis.MsgPack
{
/// <summary>
/// Class MsgPackSerializer.
/// </summary>
public class MsgPackSerializer : Contracts.SerializerBase
{
private readonly MessagePackSerializerOptions _options;
public MsgPackSerializer(MessagePackSerializerOptions options = null)
{
if (options == null)
{
options = MessagePackSerializerOptions.Standard
.WithSecurity(MessagePackSecurity.UntrustedData);
}
_options = options;
}
/// <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)
{
if (value == null)
{
return RedisValue.Null;
}
return MessagePackSerializer.Serialize(value, _options);
}
/// <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)
{
if (value.IsNull)
{
return default;
}
return MessagePackSerializer.Deserialize<T>(value, _options);
}
}
}