Home

Awesome

Nerdbank.MessagePack

A modern, fast and NativeAOT-compatible MessagePack serialization library

NuGet package codecov 🏭 Build

Features

Usage

Given a type annotated with [GenerateShape] like this:

[GenerateShape]
public partial record ARecord(string AString, bool ABoolean, float AFloat, double ADouble);

You can serialize and deserialize it like this:

// Construct a value.
var value = new ARecord("hello", true, 1.0f, 2.0);

// Create a serializer instance.
MessagePackSerializer serializer = new();

// Serialize the value to the buffer.
byte[] msgpack = serializer.Serialize(value);

// Deserialize it back.
var deserialized = serializer.Deserialize<ARecord>(msgpack);

Only the top-level types that you serialize need the attribute. All types that they reference will automatically have their 'shape' source generated as well so the whole object graph can be serialized.

<a name="perf"></a>Performance

This library has superior startup performance compared to MessagePack-CSharp due to not relying on reflection and Ref.Emit. Throughput performance is on par with MessagePack-CSharp.

When using AOT source generation from MessagePack-CSharp and objects serialized with maps (as opposed to arrays), MessagePack-CSharp is slightly faster at deserialization. We may close this gap in the future by adding AOT source generation to this library as well.

Why another MessagePack library?

MessagePack-CSharp is a great library, and in fact is chiefly maintained by the same author as this library. Here are some reasons a new library was created:

See a feature comparison table that compares the two libraries.