-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMsgPackSerializer.cs
More file actions
47 lines (43 loc) · 1.61 KB
/
MsgPackSerializer.cs
File metadata and controls
47 lines (43 loc) · 1.61 KB
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
namespace Neolution.Extensions.Caching.RedisHybrid
{
using System;
using System.IO;
using Foundatio.Serializer;
using MessagePack;
/// <summary>
/// A MessagePack implementation for <see cref="ISerializer"/>.
/// </summary>
/// <seealso cref="ISerializer" />
public class MsgPackSerializer : ISerializer
{
/// <summary>
/// The options
/// </summary>
private readonly MessagePackSerializerOptions options = MessagePack.Resolvers.ContractlessStandardResolver.Options
.WithCompression(MessagePackCompression.Lz4BlockArray);
/// <summary>
/// Deserializes the specified data.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="objectType">Type of the object.</param>
/// <returns>The deserialized object</returns>
public object Deserialize(Stream data, Type objectType)
{
return MessagePackSerializer.Deserialize(objectType, data, this.options)
?? throw new InvalidOperationException($"Deserialization returned null for type '{objectType}'.");
}
/// <summary>
/// Serializes the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="output">The output.</param>
public void Serialize(object value, Stream output)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
MessagePackSerializer.Serialize(value.GetType(), output, value, this.options);
}
}
}