Home

Awesome

BinaryPackIcon NuGet NuGet AppVeyor

BinaryPack is a binary serialization library inspired by MessagePack, but even faster, more efficient and producing smaller files. The goal of this project is to be able to use BinaryPack as a drop-in replacement for JSON, XML, MessagePack or BinaryFormatter serialization, when the serialized models don't need to be shared with other applications or with web services. BinaryPack is built to be as fast and memory efficient as possible: it uses virtually no memory allocations, and the serialized data is packed to take up as little space as possible. Whenever you're using either JSON, MessagePack, XML or some other format to cache data for your apps, to send data between clients or to save data that is not critical, you can try using BinaryPack over your previous serialization library - it will provide the same basic functionalities for models serialization, but with much higher performance and less memory usage.

DISCLAIMER: this library is provided as is, and it's no longer being actively maintained. BinaryPack was developed just for fun and it has not been tested in critical production environments. It does work fine in the scenarios described in this document (eg. I'm using this library to handle local cache files in some of my apps), but if you're looking for a more widely used and well tested library for fast binary serialization (that also offers better flexibility and customization), I'd recommend looking into MessagePack-CSharp first.

BinaryPack-benchmark

This benchmark was performed with the JsonResponseModel class available in the repository, which contains a number of string, int, double and DateTime properties, as well as a collection of other nested models, representing an example of a JSON response from a REST API. This README also includes a number of benchmarks that were performed on a number of different models. The benchmark code and all the models used can be found in this repository as well. To summarize:

Table of Contents

Installing from NuGet

To install BinaryPack, run the following command in the Package Manager Console

Install-Package BinaryPack

More details available here.

Quick start

BinaryPack exposes a BinaryConverter class that acts as entry point for all public APIs. Every serialization API is available in an overload that works on a Stream instance, and one that instead uses the new Memory<T> APIs.

The following sample shows how to serialize and deserialize a simple model.

// Assume that this class is a simple model with a few properties
var model = new Model { Text = "Hello world!", Date = DateTime.Now, Values = new[] { 3, 77, 144, 256 } };

// Serialize to a memory buffer
var data = BinaryConverter.Serialize(model);

// Deserialize the model
var loaded = BinaryConverter.Deserialize<Model>(data);

Supported properties

Here is a list of the property types currently supported by the library:

✅ Primitive types (except object): string, bool, int, uint, float, double, etc.

✅ Nullable value types: Nullable<T> or T? for short, where T : struct

✅ Unmanaged types: eg. System.Numerics.Vector2, and all unmanaged value types

✅ .NET arrays: T[], T[,], T[,,], etc.

✅ .NET collections: List<T>, IList<T>, ICollection<T>, IEnumerable<T>, etc.

✅ .NET dictionaries: Dictionary<TKey, TValue>, IDictionary<TKey, TValue> and IReadOnlyDictionary<TKey, TValue>

✅ Other .NET types: BitArray

Attributes

BinaryPack has a series of attributes that can be used to customize how the BinaryConverter class handles the serialization of input objects. By default, it will serialize all public properties of a type, but this behavior can be changed by using the BinarySerialization attribute. Here's an example:

[BinarySerialization(SerializationMode.Properties | SerializationMode.NonPublicMembers)]
public class MyModel
{
    internal string Id { get; set; }    
    
    public int Valud { get; set; }    
    
    [IgnoredMember]
    public DateTime Timestamp { get; set; }
}

Similarly, there's also a SerializableMember that can be used when the mode is set to SerializationMode.Explicit.

FAQ

Why is this library faster than the competition?

There are a number of reasons for this. First of all, BinaryPack dynamically generates code to serialize and deserialize every type you need. This means that it doesn't need to inspect types using reflection while serializing/deserializing, eg. to see what fields it needs to read etc. - it just creates the right methods once that work directly on instances of each type, and read/write members one after the other exactly as you would do if you were to write that code manually. This also allows BinaryPack to have some extremely optimized code paths that would otherwise be completely impossible. Then, unlike the JSON/XML/MessagePack formats, BinaryPack doesn't need to include any additional metadata for the serialized items, which saves time. This allows it to use the minimum possible space to serialize every value, which also makes the serialized files as small as possible.

Are there some downsides with this approach?

Yes, skipping all the metadata means that the BinaryPack format is not partcularly resilient to changes. This means that if you add or remove one of the serialized members of a type, it will not be possible to read previously serialized instances of that model. Because of this, BinaryPack should not be used with important data and is best suited for caching models or for quick serialization of data being exhanged between different clients.

Is this compatible with UWP?

Unfortunately not at the moment, UWP is still on .NET Standard 2.0 and doesn't support dynamic code generation due to how the .NET Native compiler is implemented. Hopefully it will be possible to use BinaryPack on UWP when it moves to .NET 5 and the new MonoAOT compiler in the second half of 2020.

Benchmarks

Here are three full benchmarks executed with the benchmark sample included in this repository. The error and standard deviation columns have been removed to fit each table in the horizontal space available for the README file reader on GitHub. As mentioned before, the JSON response model used in the first two benchmarks is the JsonResponseModel class. The class used in the last benchmark is instead NeuralNetworkLayerModel.

JSON response

MethodCategoriesMeanRatioGen 0Gen 1Gen 2Allocated
NewtonsoftJsonSerialization1.083.1 us1.00156.2500121.0938107.4219205083 B
BinaryFormatterSerialization1.446.6 us1.34132.812568.359437.1094402558 B
NetCoreJsonSerialization1.147.0 us1.06199.2188142.5781140.6250252407 B
XmlSerializerSerialization1.274.5 us1.18250.0000146.4844107.4219604205 B
Utf8JsonSerialization744.4 us0.69140.6250140.6250140.6250495 B
MessagePackSerialization217.3 us0.2061.035261.035261.0352432 B
BinaryPackSerialization168.1 us0.1626.611326.611326.6113108 B
NewtonsoftJsonDeserialization2.092.1 us1.0066.406319.5313-304320 B
BinaryFormatterDeserialization1.466.9 us0.70130.859448.8281-676136 B
NetCoreJsonDeserialization1.964.5 us0.9450.781315.6250-220856 B
XmlSerializerDeserialization2.098.6 us1.00132.812570.312535.1563461000 B
Utf8JsonDeserialization887.0 us0.42165.0391131.8359109.3750237159 B
MessagePackDeserialization337.0 us0.1687.402353.222735.1563241462 B
BinaryPackDeserialization168.8 us0.0846.630913.9160-215192 B

Neural network layer model

MethodCategoriesMeanRatioGen 0Gen 1Gen 2Allocated
NewtonsoftJsonSerialization26,552.5 us1.0001281.250093.750062.50005217488 B
BinaryFormatterSerialization175.5 us0.00770.556633.935533.6914155111 B
NetCoreJsonSerialization36,868.3 us1.3891071.428671.428671.42864248542 B
XmlSerializerSerialization31,354.5 us1.1811250.0000468.7500437.50003452059 B
Utf8JsonSerialization13,936.4 us0.525109.3750109.3750109.375072 B
MessagePackSerialization421.0 us0.016111.3281111.3281111.3281762 B
BinaryPackSerialization108.9 us0.00410.009810.009810.009896 B
NewtonsoftJsonDeserialization25,525.0 us1.0001687.500062.500031.25006994104 B
BinaryFormatterDeserialization126.4 us0.00513.916010.742210.742213247 B
NetCoreJsonDeserialization16,771.4 us0.65762.500031.250031.2500133380 B
XmlSerializerDeserialization38,382.1 us1.5031071.4286428.5714285.71433575022 B
Utf8JsonDeserialization17,709.0 us0.694187.5000187.5000187.5000101100 B
MessagePackDeserialization978.2 us0.03995.703195.703195.70311258 B
BinaryPackDeserialization111.8 us0.00410.00989.88779.8877826 B

Requirements

The BinaryPack library requires .NET Standard 2.1 support and it has no external dependencies.

Additionally, you need an IDE with .NET Core 3.0 and C# 8.0 support to compile the library and samples on your PC.

Special thanks

Icon made by freepik from www.flaticon.com.