MPack 2.1.1

dotnet add package MPack --version 2.1.1                
NuGet\Install-Package MPack -Version 2.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MPack" Version="2.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MPack --version 2.1.1                
#r "nuget: MPack, 2.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MPack as a Cake Addin
#addin nuget:?package=MPack&version=2.1.1

// Install MPack as a Cake Tool
#tool nuget:?package=MPack&version=2.1.1                

MPack

This library is a lightweight implementation of the MessagePack binary serialization format. MessagePack is a 1-to-1 binary representation of JSON, and the official specification can be found here: https://github.com/msgpack....

Implementation Notes

  • This library is designed to be super light weight.
  • Its easiest to understand how this library works if you think in terms of json. The type MDict represents a dictionary, and the type MArray represents an array.
  • Create MPack values with the static method MToken.From(object);. You can pass any simple type (such as string, integer, etc), or any Array composed of a simple type. MPack also has implicit conversions from most of the basic types built in.
  • Transform an MPack object back into a CLR type with the static method MToken.To<T>(); or MToken.To(type);. MPack also has explicit converions going back to most basic types, you can do string str = (string)mpack; for instance.
  • MPack now supports native asynchrounous reading and cancellation tokens. It will not block a thread to wait on a stream.

NuGet

MPack is available as a NuGet package!

PM> Install-Package MPack

Usage


Create a object model that can be represented as MsgPack. Here we are creating a dictionary, but really it can be anything:

using MPack;

var dictionary = new MDict
{
    {
        "array1", MToken.From(new[]
        {
            "array1_value1",  // implicitly converted string
            MToken.From("array1_value2"),
        })
    },
    {"bool1", MToken.From(true)}, //boolean
    {"double1", MToken.From(50.5)}, //single-precision float
    {"double2", MToken.From(15.2)},
    {"int1", 50505}, // implicitly converted integer
    {"int2", MToken.From(50)} // integer
};

Serialize the data to a byte array or to a stream to be saved, transmitted, etc:

byte[] encodedBytes = dictionary.EncodeToBytes();
// -- or --
dictionary.EncodeToStream(stream);

Parse the binary data back into a MPack object model (you can also cast back to an MPackMap or MPackArray after reading if you want dictionary/array methods):

var reconstructed = MToken.ParseFromBytes(encodedBytes);
// -- or --
var reconstructed = MToken.ParseFromStream(stream);

Turn MPack objects back into types that we understand with the generic To<>() method. Since we know the types of everything here we can just call To<bool>() to reconstruct our bool, but if you don't know you can access the instance enum MToken.ValueType to know what kind of value it is:

bool bool1 = reconstructed["bool1"].To<bool>();
var array1 = reconstructed["array1"] as MArray;
var array1_value1 = array1[0];
double double1 = reconstructed["double1"].To<double>();
//etc...

Complex Types

Beyond converting basic/primitive types, there is a very basic object serializer built in.

If your object has the [DataContract] attribute, all properties/fields with the [DataMember] attribute will be serialized.

[DataContract]
class MyObject
{
	[DataMember]
	public string Name { get; set; }

	[DataMember]
	public int Age { get; set; }

    // following will NOT be serialized, because missing [DataMember] attribute
	public List<string> Friends { get; set; } 
}

// ...
var token = MToken.From(new MyObject { Name = "John", Age = 25 });
var bytes = token.EncodeToBytes();
var reconstructed = MToken.ParseFromBytes(bytes);
var reconstructedObj = reconstructed.To<MyObject>();

If your object does not have the [DataContract] attribute, all public properties will be serialized, and you can use the [IgnoreDataMember] attribute to exclude properties from serialization.

class MyObject
{
	public string Name { get; set; }

	public int Age { get; set; }

	[IgnoreDataMember] // will not be serialized
	public List<string> Friends { get; set; }
}

Disclaimer

This is a very basic/simple implementation of the MessagePack format. This library is not optimized for performance, and the object serialization is very basic. It is not recommended for use in high-performance, high-throughput scenarios, or for serializing complex object models. For those scenarios, consider using a more robust library such as MessagePack-CSharp.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MPack:

Package Downloads
SharpWood

SharpWood - A C# Wrapper for Mafia: Oakwood

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.1 387 3/13/2024
2.0.4 182 1/24/2024
1.1.0.10 6,703 2/21/2016
1.1.0 1,371 2/20/2016
1.0.16 1,682 2/18/2016
1.0.15 1,783 2/11/2016
1.0.14 1,368 1/28/2016
1.0.12 1,612 1/26/2016
1.0.11 1,292 1/26/2016
1.0.10 1,309 1/19/2016
1.0.9 1,305 1/19/2016
1.0.7 1,312 1/8/2016
1.0.6 1,391 12/8/2015
1.0.5 1,422 12/8/2015
1.0.4 1,273 12/8/2015
1.0.3 1,340 12/8/2015