Protobuf-pure documentation or alternatives

Protobuf-net is apparently the fastest, and for high performance, the most recommended serialization library for .NET is required. I really want to use it because I need to send hundreds of thousands of objects by cable.

However, I had problems getting started. The documentation (wiki on github) is pretty scarce, especially for v2.

Somehow you guys seem to be able to go with lib. How? Reading the sources? Trial and error? Or are there some API documents / tutorials that I don't know about? (I only know the GitHib page.)

Thank you and welcome

Jan

PS: I need to go with RuntimeTypeModel (POCO without attributes).

+6
source share
3 answers

In protobuf, each type member requires an identification number because protobuf is numeric (it does not send names). So the trick is simple: tell me which numbers to use. For instance:

class Customer { public int Id {get;set;} public string Name {get;set;} } 

The easiest way to specify a contract for this:

 RuntimeTypeModel.Default.Add(typeof(Customer), false).Add("Id", "Name"); 

which will associate Id with 1 and Name with 2. When using attributes, there is a built-in "make it your yourself" code that I really need to expose in an non-attribute API - things like:

  • serialize all public fields + properties in alphabetical order
  • serialize all fields (public or non-public) in alphabetical order

However: both are fairly easy to handle. Note that in any case, using reflection is a good way to run into problems if the types can change at some point.

Additional features that may help, and which I can provide more detailed information about:

  • a factory type can be specified (globally or each instance), which can be useful for prefilling values ​​or using a pool of available objects
  • surrogate types can be written for complex scenarios - this is useful when most of the model works fine, but one type is too esoteric to be suitable for serialization - but an alternative layout can be developed for which you can write the conversion code in both directions
  • many things that look like a "tuple" will be handled by default - in particular, if it is publicly immutable and has a constructor that accepts parameters that correspond to all public members - it involves serializing the elements in the order specified by the constructor
+1
source

Since you also asked about alternatives ...

There was no need to decorate attributes, it was one of the reasons for creating Migrant , a fast serialization library with a simple API. There are some ideas in the library that are also present in protobuf (so we are more or less united in terms of speed and size), but at the same time trying to solve different problems. Among the features other than protobuf, there is a difference between empty and null sets, and the whole serialization is a link based on links and a value based on values ​​(well, you can also consider a link as a special kind of value). README on github should be able to answer most of your questions; if more information is required, just ask.

A simple script to serialize custom objects:

 var stream = new MyCustomStream(); var myComplexObject = new MyComplexType(complexParameters); var serializer = new Serializer(); serializer.Serialize(myComplexObject, stream); stream.Seek(0, SeekOrigin.Begin); var myDeserializedObject = serializer.Deserialize<MyComplexType>(stream); 

Note that the expected type in Deserialize is only used to have a good compile time type for a deserialized object, you can also use a generic object .

Disclaimer: I am one of the developers.

+6
source

Another option is Dasher (available through NuGet ).

This is a simple, fast and easy .NET serializer and deserializer that works on C # types without any attributes or other decorations.

 var stream = new MemoryStream(); // serialise to stream new Serialiser<Holiday>().Serialise(stream, christmas); stream.Position = 0; // deserialise from stream var christmas = new Deserialiser<Holiday>().Deserialise(stream); 

It uses emit reflection to create highly optimized serialization and deserialization functions at runtime. The underlying encoding is MsgPack, which itself describes, so little more than protobuf on the wire, but that means you can decode any message received, including the names, types and values ​​of properties.

Disclaimer: I wrote a library.

0
source

Source: https://habr.com/ru/post/953873/


All Articles