How to use MessagePack in C#?
Asked Answered
I

5

11

I read the msgpack-cli quick start documentation.

I also got the C# (CLI) NuGet package (v0.3).

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.

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.

Illdefined answered 6/4, 2013 at 2:29 Comment(0)
I
10

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);
    }
}

The entire R&D type project, with results is at https://github.com/sidshetye/SerializersCompare and the specific function calls are here.

Illdefined answered 25/12, 2013 at 21:25 Comment(3)
Another option to consider: fastBinaryJSON (codeproject.com/Articles/345070/fastBinaryJSON)Handal
'MessagePackSerializer.Create<T>()' is Obsolete: 'Use Get<T>() instead.'Danadanae
This is very old and needs an update. MessagePack smokes.Upanchor
M
4

I am surprised, nobody understood what the user asked. There are so many nuget packages for msgpack and it's really confusing which one to use and how to include in the projects. Im assuming the user wanted to know how to include msgpack in the .net project just like me.

Install MessagePack for CLI from Nuget packages

and then include like this

using MsgPack.Serialization;
Muoimuon answered 12/5, 2016 at 7:32 Comment(0)
W
3

Much has changed in 10 years. MessagePack is fast and small, contrary to the accepted answer. We use MessagePack-CSharp library now.

You can see how it is faster and smaller than the old msgpack-cli here:

MessagePack-CSharp speed and size chart

It's easy to use.

Pack:

MyObjectType myObject = new();
byte[] packed = MessagePackSerializer.Serialize(myObject);

Unpack:

MyObjectType unPacked = MessagePackSerializer.Deserialize<MyObjectType>(packed);
Washin answered 5/1 at 23:17 Comment(0)
G
0

In the latest versions of msgpack-cli the Create method as in MessagePackSerializer.Create<T>(); is marked as obsolete.

Example usage as given in the project's github page may be:

// Creates serializer.
var serializer = SerializationContext.Default.GetSerializer<T>();
// Pack obj to stream.
serializer.Pack(stream, obj);
// Unpack from stream.
var unpackedObject = serializer.Unpack(stream);
Gradely answered 10/5, 2016 at 10:26 Comment(0)
H
-2

Install the package named MsgPack from NuGet. See the pic here :

enter image description here

Hydra answered 23/10, 2013 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.