Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 4.24 KB

File metadata and controls

60 lines (45 loc) · 4.24 KB

Nerdbank.MessagePack

A modern, fast and NativeAOT-compatible MessagePack serialization library

NuGet package Docs codecov 🏭 Build

Features

See a side-by-side feature comparison across popular libraries.

  • Serializes in the compact and fast MessagePack format.
  • No attributes required on your data types. Optional attributes allow customization and improve performance.
  • Supports the latest C# syntax including required and init properties, record classes and structs, and primary constructors.
  • This library is perf-optimized and is among the fastest MessagePack serialization libraries available for .NET.
  • Works great in your NativeAOT, trimmed, SignalR or ASP.NET Core MVC applications or Unity games.
  • Many C# analyzers to help you avoid common mistakes.
  • Great security for deserializing untrusted data.
  • Polymorphic deserialization lets you deserialize derived types.
  • True async and streaming deserialization for large or over-time sequences keeps your apps responsive and memory pressure low.
  • Preserve reference equality across serialization/deserialization (optional).
  • Forward compatible data retention allows you to deserialize and re-serialize data without dropping properties you didn't know about.
  • Structural equality checking and hashing for arbitrary types gives you deep by-value equality semantics without hand-authoring Equals and GetHashCode overrides.
  • No mutable statics ensures your code runs properly no matter what other code might run in the same process.
  • Only serialize properties with non-default values (optional).
  • Primitive msgpack reader and writer APIs for low-level scenarios.
  • Author custom converters for advanced scenarios.

Usage

Given a data type 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);

The [GenerateShape] attribute is highly encouraged because it boosts startup performance and ensures NativeAOT and trim safety. Only the top-level type that you serialize needs the attribute. All types that it references will automatically have their 'shape' source generated as well so the whole object graph can be serialized quickly and safely.

Learn more in our getting started doc.