Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to protobuf-net - size and time efficient serializer to work with objects graph

Google protobuf is great protocol to serialize objects efficient, but it only supports serialization of trees, not graphs (lack of full Object Reference Tracking).

There are few implementation of Google idea in .NET. (i.e. protobuf-csharp-port, or protosharp), but most interesting is protobuf-net.

Protobuf-net is better because its architecture fits, to .NET world, and has several add-ins (not always compatible with original protobuf, but very useful).

One of that add-in is possibility to turn on reference tracking (AsReference option), which allows to serialize complex graphs even with cycles.

Unnecessary Protobuf-net v2 is in beta version, and AsReference option not works at real world scenarios. (without this option everything works well, but without Reference Tracikng protobuf-net is not able to serialize graphs and is not longer attractive serializer).

It still have some bugs:

  • Issue 196
  • Issue 213
  • Issue 232
  • Issue 242
  • SO 6294295
  • SO 7219959

So, I can't use this great tool, and I'm looking for alternative serializer which:

  • is at least as fast, and produces small output as protobuf
  • is easy to adopt to current project like protobuf-net
  • allows to serialize graph like DataContractSerializer with PreserveObjectReferences set to true
  • is stable not only for simple objects, but for complex real world scenarios
like image 959
Bartosz Pierzchlewicz Avatar asked Oct 20 '11 09:10

Bartosz Pierzchlewicz


2 Answers

Bartosz, while this question is quite old, I might recommend you and whoever stumbles upon it to use Migrant, available both from source and NuGet package. I'm one of the co-authors.

It can be easily adopted to even complicated scenarios, we tried to make it as easy to use as possible.

The output size is reasonably small. It depends on your object tree, of course, but it can be comparable with protobuf-net. Like protobuf, it uses Varint and ZigZag encoding.

Of course Migrant resolves the problems you've mentioned - It keeps the whole graphs of objects, handles inheritance, complex collections, version tolerance (up to some point) etc.

In terms of speed we try to be comparable with protobuf-net. It supports de/serialization using dynamically generated methods, which is much faster then classical reflection-based solutions.

Simple usage scenarios are available on the site I've linked to, but simple object cloning is straight-forward.

var myComplexObject = new MyComplexType(complexParameters);
var myObjectCopy = serializer.DeepClone(myComplexObject);

Writing to stream is as easy.

Of course, for very complicated types, there is a set of class decorators (attributes, hooks) to make things even smarter ;-)

like image 162
Piotr Zierhoffer Avatar answered Sep 28 '22 17:09

Piotr Zierhoffer


We are considering MessagePack. They claim to be 4x faster than ProtoBuf. However I don't know if it supports full object graphs. (we don't have that requirement, we actually flatten the objects we use for communication) In my scenario I would use it for communication between a .Net and a delphi layer (and that's also what's holding me back, no delphi support :) )

like image 39
Cohen Avatar answered Sep 28 '22 17:09

Cohen