MessagePack serialization tools/snippets
$ dotnet add package OutWit.Common.MessagePackA utility library for the MessagePack-CSharp serializer. It simplifies common serialization tasks, provides helpful extension methods, and includes a set of pre-configured, serializable data structures for common use cases.
ToMessagePackBytes, FromMessagePackBytes) to serialize and deserialize any object with a single line of code..MessagePackClone() extension method to perform a deep copy of any serializable object.MessagePackUtils for registering custom formatters and resolvers globally.TypeFormatter: Serializes System.Type objects.PropertyChangedEventArgsFormatter: Serializes System.ComponentModel.PropertyChangedEventArgs.Install the package via the .NET CLI:
dotnet add package OutWit.Common.MessagePack
The library is configured with sensible defaults and is ready to use immediately after installation.
The MessagePackUtils class provides extension methods for any object.
using OutWit.Common.MessagePack;
public class MyData
{
public int Id { get; set; }
public string Name { get; set; }
}
var myObject = new MyData { Id = 1, Name = "Test" };
// Serialize the object to a byte array (with LZ4 compression by default)
byte[] msgPackBytes = myObject.ToMessagePackBytes();
// Deserialize it back
MyData deserializedObject = msgPackBytes.FromMessagePackBytes<MyData>();
To serialize without compression, pass false to the withCompression parameter.
// Serialize without compression
byte[] plainBytes = myObject.ToMessagePackBytes(withCompression: false);
// Deserialize without compression
MyData deserializedObject = plainBytes.FromMessagePackBytes<MyData>(withCompression: false);
You can create a deep clone of any serializable object using the MessagePackClone extension method.
var originalObject = new MyData { Id = 10, Name = "Original" };
var clonedObject = originalObject.MessagePackClone();
// clonedObject is a new instance with the same values
// but is not the same reference as originalObject.
The library uses a central resolver. You can easily register your own formatters at application startup.
using OutWit.Common.MessagePack;
using MessagePack;
using MessagePack.Formatters;
// 1. Define your custom formatter
public class MyCustomObjectFormatter : IMessagePackFormatter<MyCustomObject>
{
public void Serialize(ref MessagePackWriter writer, MyCustomObject value, MessagePackSerializerOptions options)
{
// ... serialization logic
}
public MyCustomObject Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
// ... deserialization logic
}
}
// 2. Register it at startup
public static class Program
{
public static void Main(string[] args)
{
// Register the formatter for MyCustomObject
MessagePackUtils.Register<MyCustomObjectFormatter>();
// ... rest of your application
}
}
Licensed under the Apache License, Version 2.0. See LICENSE.
If you use OutWit.Common.MessagePack in a product, a mention is appreciated (but not required), for example: "Powered by OutWit.Common.MessagePack (https://ratner.io/)".
"OutWit" and the OutWit logo are used to identify the official project by Dmitry Ratner.
You may:
You may not: