-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessagePack.Array.cs
More file actions
50 lines (47 loc) · 1.65 KB
/
MessagePack.Array.cs
File metadata and controls
50 lines (47 loc) · 1.65 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
48
49
50
using System;
using System.IO;
using System.Collections.Concurrent;
using MsgPack.Serialization;
using CacheUtils;
namespace Enyim.Caching.Memcached
{
public class MessagePackArrayTranscoder : DefaultTranscoder
{
static readonly ConcurrentDictionary<string, Type> readCache = new ConcurrentDictionary<string, Type>();
static readonly ConcurrentDictionary<Type, string> writeCache = new ConcurrentDictionary<Type, string>();
static readonly SerializationContext defaultContext = new SerializationContext();
protected override ArraySegment<byte> SerializeObject(object value)
{
var type = value.GetType();
var typeName = writeCache.GetOrAdd(type, TranscodersHelper.BuildTypeName);
using (var stream = Helper.CreateMemoryStream())
{
var packer = MsgPack.Packer.Create(stream);
packer.PackArrayHeader(2);
packer.PackString(typeName);
MessagePackSerializer.Get(type, defaultContext).PackTo(packer, value);
return new ArraySegment<byte>(stream.ToBytes());
}
}
protected override object DeserializeObject(ArraySegment<byte> value)
{
using (var stream = Helper.CreateMemoryStream(value.Array, value.Offset, value.Count))
{
var unpacker = MsgPack.Unpacker.Create(stream);
unpacker.Read();
if (unpacker.IsArrayHeader)
{
unpacker.Read();
var typeName = (string)unpacker.LastReadData;
var type = readCache.GetOrAdd(typeName, x => Type.GetType(x, throwOnError: true));
unpacker.Read();
return MessagePackSerializer.Get(type, defaultContext).UnpackFrom(unpacker);
}
else
{
throw new InvalidDataException("MessagePackTranscoder only supports [\"TypeName\", object]");
}
}
}
}
}