问题描述
我阅读了 msgpack-cli快速入门文档.
我还获得了C#(CLI)NuGet软件包(v0.3).
I also got the C# (CLI) NuGet package (v0.3).
NuGet包(!!)中没有官方文档中提到的所有类(例如BoxingPacker
,CompiledPacker
或ObjectPacker
).我认为文档已被孤立.
None of the classes (eg BoxingPacker
, CompiledPacker
or ObjectPacker
) mentioned in the official documentation exist in the NuGet package (!!). I'm presuming the documentation has been orphaned.
那么没有人有示例如何在C#中对MessagePack进行序列化/反序列化吗?我正在尝试为一个对象执行此操作,并且对序列化程序的二进制性质感兴趣.
So does anyone have examples how to serialize/deserialize to/from MessagePack within C#? I'm trying to do this for an object and am interested in the binary nature of the serializer.
推荐答案
致将来的读者:我会使用基于这些结果 ...
To future readers: I'd go with Avro or Protocol Buffers or even Thrift over MessagePack based on these results ...
出于特定的问题,关键部分是:
For the sake of the specific question, the key portions are:
public byte[] Serialize<T>(T thisObj)
{
var serializer = MessagePackSerializer.Create<T>();
using (var byteStream = new MemoryStream())
{
serializer.Pack(byteStream, thisObj);
return byteStream.ToArray();
}
}
public T Deserialize<T>(byte[] bytes)
{
var serializer = MessagePackSerializer.Create<T>();
using (var byteStream = new MemoryStream(bytes))
{
return serializer.Unpack(byteStream);
}
}
整个R& D类型项目,其结果位于 https://github.com/sidshetye/SerializersCompare ,具体的函数调用在此处.
The entire R&D type project, with results is at https://github.com/sidshetye/SerializersCompare and the specific function calls are here.
这篇关于如何在C#中使用MessagePack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!