GitXplorerGitXplorer
A

Nerdbank.MessagePack

public
68 stars
3 forks
7 issues

Commits

List of commits on branch main.
Verified
5392862233a82fcd8e4293ad9f79ed26fbddaeed

Merge pull request #221 from AArnott/addIndexerTest

AAArnott committed 2 days ago
Verified
a0bb4d0f425a3596f4d88647ee5745a816ac3d54

Add test for class with indexer

AAArnott committed 2 days ago
Verified
9139e34177282d94763edf42444ee5ced1172156

Future-proof msbuild property update policy

AAArnott committed 3 days ago
Verified
be7f6952616c7ba555c64071a875c4ce221f9bd2

Merge pull request #220 from AArnott/dev/andarno/libtemplateUpdate

AAArnott committed 3 days ago
Verified
f160671b3ceee2e9578694e75549a0fc6e58644b

Merge the main branch from https://github.com/aarnott/Library.Template

AAArnott committed 3 days ago
Verified
cbf872076e88d492dfcbe56780dac4ce104e6165

Merge branch 'xunitConfigMigration'

AAArnott committed 3 days ago

README

The README file for this repository.

Nerdbank.MessagePack

A modern, fast and NativeAOT-compatible MessagePack serialization library

NuGet package codecov 🏭 Build

Features

  • Serializes in the compact and fast MessagePack format.
  • Performance is on par with the highly tuned and popular MessagePack-CSharp library.
  • Automatically serialize any type annotated with the PolyType [GenerateShape] attribute.
  • Automatically serialize non-annotated types by adding a 'witness' type with a similar annotation.
  • Fast ref-based serialization and deserialization minimizes copying of large structs.
  • NativeAOT and trimming compatible.
  • Keep memory pressure low by using async serialization directly to/from I/O like a network, IPC pipe or file.
  • Primitive msgpack reader and writer APIs for low-level scenarios.
  • Author custom converters for advanced scenarios.
  • Security mitigations for stack overflows.
  • Optionally serialize your custom types as arrays of values instead of maps of names and value for more compact representation and even higher performance.
  • Support for serializing instances of certain types derived from the declared type and deserializing them back to their original runtime types using unions.

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.

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:

  • MessagePack-CSharp has a long history and breaking changes are difficult to introduce.
  • MessagePack-CSharp was not "Native AOT" compatible nor trim-friendly (although it has a long history of getting mostly there through various tricks).
  • Nerdbank.MessagePack is based on [GenerateShape], so it is far simpler than MessagePack-CSharp to author and maintain.
  • Nerdbank.MessagePack has no mutable statics, with the functional unpredictability that can bring.
  • Nerdbank.MessagePack can dynamically create converters with various options that may vary from other uses within the same process, providing more flexibility than MessagePack-CSharp's strict generic type static storage mechanism.
  • Nerdbank.MessagePack is far simpler to use. One attribute at the base of an object graph is typically all you need. MessagePack-CSharp demands attributes on every single type and every single field or property (even members that will not be serialized).
  • Nerdbank.MessagePack makes adding some long-sought for features from MessagePack-CSharp far easier to implement.

See a feature comparison table that compares the two libraries.

Consuming CI builds

You can acquire CI build packages (with no assurance of quality) to get early access to the latest changes without waiting for the next release to nuget.org.

There are two feeds you can use to acquire these packages: