Sergio0694 / BinaryPack

The fastest and most memory efficient binary serialization library for .NET Standard 2.1, powered by dynamic IL generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to Serialize/Deserialize Collections et al (i.e. Dictionary<TKEY, TVALUE>)

lm-adruschel opened this issue · comments

I am trying to use this package for Binary Serialization & Deserialization in connection with IPC using MemoryMappedFile.

It's a new approach for me and I'm stuck with simple Dictionary<TKey, TValue> objects to be serialized like Dictionary<Guid, long> or alike. No List or any other generic collection is working.

Sample:

var myObj = new Dictionary<Guid, long>()
{
    { new Guid( "3F9A1380-2331-4405-A68A-57E2580362EA" ), 11111111 },
    { new Guid( "3F9A1380-2331-4405-A68A-57E2580362EB" ), 22222222 }
};
var g1 = BinaryPack.BinaryConverter.Serialize( myObj );

Here I am getting a "System.InvalidProgramException" ("Common Language Runtime detected an invalid program.").

I might have overlooked any instructions or missed any samples.

Can you, please, help out?

Hey @LMerpDev, the reason why you're seeing that error is that BinaryPack is meant to be used to serialize models, not collections directly. Think of it as a fast, binary JSON serializer. The serializers are build to handle fields/properties of those types, but the root object needs to be a user defined class or struct. For instance, to make the code above work you'd simply need to define a model:

public class MyModel
{
    public Dictionary<Guid, long> Dictionary { get; set; }
}

And then serialize/deserialize the object like so:

var myObj = new Dictionary<Guid, long>()
{
    { new Guid( "3F9A1380-2331-4405-A68A-57E2580362EA" ), 11111111 },
    { new Guid( "3F9A1380-2331-4405-A68A-57E2580362EB" ), 22222222 }
};

var model = new MyModel { Dictionary = myObj };

var bytes = BinaryConverter.Serialize(model);

var result = BinaryConverter.Deserialize<MyModel>(bytes);

Hope this helps! 😊

You can binary serialize/deserialize to any type without requiring a root object. My binary serializer - which is dynamically generated as well and much faster than BinaryPack - allows this, and JSON also allows this. To say "think of it as a fast JSON serializer" is just flat out wrong. When JSON allows serialization/deserialization directly to collections/non object roots. It's very easy to allow serialization/deserialization to even the most primitive types. A proper serializer will you to serialize something as little as a byte all the way up to a Dictionary<K, V>.

Glossing over the fact that commenting a months-old closed issue in a random repository just to downvote a message where I was helping a user and claim your own library (which is not even listed in your GitHub profile anyway) is better is... Pretty weird.

But regardless, I feel like there's some confusion about what this library is actually meant for. This was basically just a small side project I worked on for like a couple weeks, mostly just to learn about IL and dynamic code. It's not meant to be used in production, it's not feature complete, and I'm not even actively maintaining it. I never claimed it's not possible to deserialize collections directly, it's literally just that I haven't bothered to implement that functionality into BinaryPack. This project was just a learning exercise for me, it's not meant to compete with other popular library such as MessagePack etc., and that's also why it's simply provided as is.

If you think your own serializer is better, then by all means use that then, that's not a concern for me 😄