Language: EN

csharp-message-pack

How to serialize in Message Pack format in C#

MessagePack Sharp is a library for C# and .NET for serialization in 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 strong points of MessagePack compared to other serialization alternatives are the speed and the compact size of the serialized files.

On the one hand, MessagePack serializes objects in a binary format that is more compact than other serialization formats such as JSON or XML.

In addition, it is designed to offer high performance in terms of serialization and deserialization speed. It uses optimization techniques to ensure that data is serialized and deserialized efficiently.

The MessagePack format is not exclusive to C#. It is compatible with more than 50 languages, so serialized data can be shared with other applications.

We can work with the MessagePack format from a C# and .NET application with the MessagePack Sharp library.

messagepack-sharp-benchmark

MessagePack Sharp offers a lot of options and customizations to configure the serialization and deserialization process. Check the library’s documentation for more information.

How to use MessagePack

We can easily add the library to a .NET project through 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 taken from the library’s documentation.

Serialization and deserialization with attributes

The traditional way to use MessagePack Sharp is to decorate our objects with attributes, indicating 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 object, which has three properties: Id, Name, and Price.
  • The object is serialized to a byte array using MessagePackSerializer.Serialize.
  • The byte array is deserialized 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 bit slower than using attributes, but the advantages in terms of cleanliness generally make up for the loss.

MessagePack is Open Source, and all the code and documentation is available in the project’s repository on GitHub - MessagePack-CSharp