nf.MessagePack.Net 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package nf.MessagePack.Net --version 1.0.1
                    
NuGet\Install-Package nf.MessagePack.Net -Version 1.0.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="nf.MessagePack.Net" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nf.MessagePack.Net" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="nf.MessagePack.Net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add nf.MessagePack.Net --version 1.0.1
                    
#r "nuget: nf.MessagePack.Net, 1.0.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.
#addin nuget:?package=nf.MessagePack.Net&version=1.0.1
                    
Install nf.MessagePack.Net as a Cake Addin
#tool nuget:?package=nf.MessagePack.Net&version=1.0.1
                    
Install nf.MessagePack.Net as a Cake Tool

Quality Gate Status Reliability Rating NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework MessagePack repository

This repository contains the MessagePack library for the .NET nanoFramework. It provides high-performance serialization and deserialization with the smallest possible payload, MessagePack is an object serialization specification like JSON.

Build status

Component Build Status NuGet Package
nanoFramework.MessagePack Build Status NuGet

nanoFramework.MessagePack

MessagePack is a simple, lightweight serialization library, inspired by MsgPack.Light, that can be used in .NET nanoFramework projects.

Usage

Serialization to byte array

var value = new TestClass();
var bytes = MessagePackSerializer.Serialize(value);

Deserialization

var result = (TestClass)MessagePackSerializer.Deserialize(typeof(TestClass), bytes);

Your type serialization/deserialization

If you want to work with your own types, first thing you need to add is a type converter. Although the library provides almost complete coverage of the serialization/deserialization of any objects, there are certain cases where it is difficult to do without a custom converter.

The general principle of implementing a custom converter
  1. In the code of your project, you need to create a converter class that inherits the IConverter interface and implement the Read and Write interface methods:

         public class SimpleCustomConverter : IConverter
         {
             #nullable enable
             public void Write(object? value, [NotNull] IMessagePackWriter writer)
             {
                 //TODO Your code is here
             }
    
             public object? Read([NotNull] IMessagePackReader reader)
             {
                var yourObject = new YourObject();
                //TODO Your code is here
                return yourObject;
             }
         }
    
  2. Register your custom converter in the context of serialization:

          public class Program
          {
              public static void Main()
              {
                    var simpleCustomConverter = new SimpleCustomConverter();
                    ConverterContext.Add(typeof(YourObject), simpleCustomConverter);
              }
          }
    

After completing these steps, the serialization/deserialization of the object for which your converter is added will occur in the methods of the custom converter.

A few examples of the implementation of custom converters
  1. Case with enumeration elements as strings:

        namespace samples
        {
            public enum FieldType
            {
                _ = -1,
                Str,
                Num,
                Any
            }
    
            public class FieldTypeConverter : IConverter
            {
                internal FieldType Read(IMessagePackReader reader)
                {
                    var stringConverter = ConverterContext.GetConverter(typeof(string));
    
                    var enumString = (string)stringConverter.Read(reader);
    
                    return enumString switch
                    {
                        "Str" => FieldType.Str,
                        "Num" => FieldType.Num,
                        "*" => FieldType.Any,
                        _ => throw new Exception($"Unexpected enum {typeof(FieldType)} underlying type: {enumString}"),
                    };
                }
    
                internal void Write(FieldType value, [NotNull] IMessagePackWriter writer)
                {
                    var stringConverter = ConverterContext.GetConverter(typeof(string));
    
                    switch (value)
                    {
                        case FieldType.Str:
                            stringConverter.Write("Str", writer);
                            break;
                        case FieldType.Num:
                            stringConverter.Write("Num", writer);
                            break;
                        case FieldType.Any:
                            stringConverter.Write("*", writer);
                            break;
                        default:
                            throw new Exception($"Enum {value.GetType()} value: {value} expected");
                    }
                }
    
        #nullable enable
                object? IConverter.Read([NotNull] IMessagePackReader reader)
                {
                    return Read(reader);
                }
    
                public void Write(object? value, [NotNull] IMessagePackWriter writer)
                {
                    Write((FieldType)value!, writer);
                }
            }
        }
    
  2. Case compression or concealment of transmitted strings between sender and recipient if sender and recipient share the same vocabulary of words:

        namespace samples
        {
            public static class SharedWordDictionary
            {
                static SharedWordDictionary()
                {
                    WordDictionary = new ArrayList
                    {
                        "MessagePack",
                        "Hello",
                        "at",
                        "nanoFramework!",
                        " "
                    };
                }
    
                public static ArrayList WordDictionary { get; }
            }
    
            public class SecureMessage
            {
                public SecureMessage(string message)
                {
                    Message = message;
                }
    
                public string Message { get; private set; }
            }
    
            public class SecureMessageConverter : IConverter
            {
                public SecureMessage Read([NotNull] IMessagePackReader reader)
                {
                    StringBuilder sb = new();
                    var length = reader.ReadArrayLength();
                    var intConverter = ConverterContext.GetConverter(typeof(int));
    
                    for (int i = 0; i < length; i++)
                    {
                        int wordIndex = (int)intConverter.Read(reader)!;
                        sb.Append(SharedWordDictionary.WordDictionary[wordIndex]);
                        sb.Append(' ');
                    }
                    if (sb.Length > 0)
                        sb.Remove(sb.Length - 1, 1);
    
                    return new SecureMessage(sb.ToString());
                }
    
                public void Write(SecureMessage value, [NotNull] IMessagePackWriter writer)
                {
                    var messageWords = value.Message.Split(' ');
    
                    uint length = (uint)messageWords.Length;
                    writer.WriteArrayHeader(length);
    
                    var intConverter = ConverterContext.GetConverter(typeof(int));
    
                    foreach (var word in messageWords)
                    {
                        int wordIndex = SharedWordDictionary.WordDictionary.IndexOf(word);
                        intConverter.Write(wordIndex, writer);
                    }
                }
    
        #nullable enable
                object? IConverter.Read([NotNull] IMessagePackReader reader)
                {
                    return Read(reader);
                }
    
                public void Write(object? value, [NotNull] IMessagePackWriter writer)
                {
                    Write((SecureMessage)value!, writer);
                }
            }
    
            public class Program
            {
                public static void Main()
                {
                    var secureMessageConverter = new SecureMessageConverter();
                    ConverterContext.Add(typeof(SecureMessage), secureMessageConverter);
    
                    var secureMessage = new SecureMessage("Hello MessagePack at nanoFramework!");
    
                    //At sender
                    var buffer = MessagePackSerializer.Serialize(secureMessage);
                    Debug.WriteLine($"The message:\n{secureMessage.Message}\nbeing sent has been serialized into {buffer.Length} bytes.");
                    //and sent to recipient
                    //
                    //..........................
                    Debug.WriteLine("=============================================");
    
                    //At recipient, after receiving the byte array
                    Debug.WriteLine($"Received {buffer.Length} bytes");
    
                    var recipientSecureMessage = (SecureMessage)MessagePackSerializer.Deserialize(typeof(SecureMessage), buffer)!;
    
                    Debug.WriteLine($"Message received:\n{recipientSecureMessage.Message}");
                }
            }
        }
    

Benchmarks

The measurements were carried out on the developer's local computer in a virtual nanoDevice:

===============================================================
==========         Comparative benchmarks data       ==========
==========                                           ==========
========== Json string size:           3957 bytes    ==========
========== BinaryFormatter array size: 1079 bytes    ==========
========== MessagePack array size:     2444 bytes    ==========
==========                                           ==========
===============================================================

Console export: ComparativeDeserializationBenchmark benchmark class.

| ------------------------------------------------------------------------------ |
| MethodName                          | IterationCount | Mean    | Min   | Max   |
| ------------------------------------------------------------------------------ |
| JsonDeserializationBenchmark        | 10             | 27.5 ms | 22 ms | 37 ms |
| BinaryDeserializationBenchmark      | 10             | 0.1 ms  | 0 ms  | 1 ms  |
| MessagePackDeserializationBenchmark | 10             | 23.7 ms | 19 ms | 34 ms |
| ------------------------------------------------------------------------------ |

Console export: ComparativeSerializationBenchmark benchmark class.


| ---------------------------------------------------------------------------- |
| MethodName                        | IterationCount | Mean    | Min   | Max   |
| ---------------------------------------------------------------------------- |
| JsonSerializationBenchmark        | 10             | 18.9 ms | 16 ms | 25 ms |
| BinarySerializationBenchmark      | 10             | 0.1 ms  | 0 ms  | 1 ms  |
| MessagePackSerializationBenchmark | 10             | 9.8 ms  | 9 ms  | 14 ms |
| ---------------------------------------------------------------------------- |

As it can be seen from the benchmark results above, in what concerns speed and compaction, MessagePack performs better than the Json serializer. Comming at no surprise, Binary serialization is the most performant one.

Acknowledgements

The initial version of the MessagePack library was coded by Spirin Dmitriy, who has kindly handed over the library to the .NET nanoFramework project.

Feedback and documentation

For documentation, providing feedback, issues, and finding out how to contribute, please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework WebServer library is licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 201 5/12/2025
1.0.2 132 5/7/2025
1.0.1 126 5/7/2025
1.0.0 134 4/30/2025