MessagePack Sharp is a library for C# and .NET for *serialization in the MessagePack format, a high-speed, very compact binary system.
As the authors of MessagePack say in the project description, “Message Pack, It’s like JSON. but fast and small” 😉
The two main strengths of MessagePack compared to other serialization alternatives are speed and the compact size of the serialized files.
On one hand, MessagePack serializes objects into a binary format that is more compact than other serialization formats like JSON or XML.
Furthermore, it is designed to offer high performance in terms of serialization and deserialization speed. It uses optimization techniques to ensure data is serialized and deserialized efficiently.
The MessagePack format is not exclusive to C#. It is compatible with over 50 languages, so serialized data can be shared with other applications.
We can work with the MessagePack format from a C# and .NET application using the MessagePack Sharp library.

MessagePack Sharp offers a lot of options and customizations to configure the serialization and deserialization process. Consult the library documentation for more information.
How to Use MessagePack
We can add the library to a .NET project easily, via the corresponding Nuget package.
Install-Package MessagePack
Once added, the library can be used to serialize and deserialize objects.
Here are some examples of how to use MessagePack Sharp, extracted from the library documentation.
Serialization and Deserialization with Attributes
The traditional way to use MessagePack Sharp is to decorate our objects with attributes, which indicate how the binary file should be encoded.
using MessagePack;
[MessagePackObject]
public class ExampleObject
{
[Key(0)]
public int Id { get; set; }
[Key(1)]
public string Name { get; set; }
[Key(2)]
public decimal Price { get; set; }
}
// Create object
var object = new ExampleObject()
{
Id = 1,
Name = "Example Product",
Price = 10.99,
};
// Serialize object to byte[]
var bytes = MessagePackSerializer.Serialize(object);
// Deserialize byte[] to object
var deserializedObject = MessagePackSerializer.Deserialize<ExampleObject>(bytes);
In this example
- We create an
ExampleObject, which has three properties:Id,Name, andPrice. - The object is serialized to a byte array using
MessagePackSerializer.Serialize. - The byte array is deserialized back to an object using
MessagePackSerializer.Deserialize.
Contractless Serialization and Deserialization
Putting attributes on our classes is not always possible or desirable. So MessagePack CSharp offers the “ContractLess” mode.
public class ExampleObject
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
var object = new ExampleObject { MyProperty1 = 99, MyProperty2 = 9999 };
// Create object
var object = new ExampleObject()
{
Id = 1,
Name = "Example Product",
Price = 10.99,
};
// Serialize object to byte[]
var bytes = MessagePackSerializer.Serialize(object, MessagePack.Resolvers.ContractlessStandardResolver.Options);
// Deserialize byte[] to object
var deserializedObject = MessagePackSerializer.Deserialize<ExampleObject>(bytes);
The ContractLess process is a little slower than using attributes, but the advantages in terms of code cleanliness will generally compensate for the loss.
MessagePack is Open Source, and all the code and documentation is available in the project repository at GitHub - MessagePack-CSharp

