Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary serialization/de-serialization in C++ and C#

I am working on a distributed application which has two components. One is written in standard C++ (not managed C++ and running on a Linux platform) and the other one is written in C#. Both are communicating via a message bus.

I have a situation in which I need to pass objects from C++ to C# application and for this I need to serialize those objects in C++ and de-serialize them in C# (something like marshaling/un-marshaling in .NET). I need to perform this serialization in binary and not in XML (due to performance reasons).

I have used Boost.Serialization to do this when both ends were implemented in C++ but now that I have a .NET application on one end, Boost.Serialization is not a viable solution.

I am looking for a solution that allows me to perform (de)serialization across C++ and .NET boundary i.e., cross platform binary serialization.

I know I can implement the (de)serialization code in a C++ dll and use P/Invoke in the .NET application, but I want to keep that as a last resort.

Also, I want to know if I use some standard like gzip, will that be efficient? Are there any other alternatives to gzip? What are the pros/cons of them?

Thanks

like image 354
Aaron S Avatar asked Jan 13 '11 07:01

Aaron S


2 Answers

I would recommend Protocol Buffers, which is Googles own serialization library. It has both .Net, C++ and Java serializers. Most implementations are also quite fast.

http://code.google.com/p/protobuf/

like image 80
jgauffin Avatar answered Sep 29 '22 03:09

jgauffin


gzip won't directly help with serialization - it will just (attempt to) shrink a stream. This may help, or not, depending on the amount of duplicated data in the stream. For dense data with little text, I've seen gzip increase the size of the payload.

Personally I would look at protocol buffers here (but I'm biased, as I'm one of the authors of the many extensions). You typically (but not always) define the messages in a basic language (a .proto file), and run language-specific tools to generate the classes. Performance is very good - focusing on .NET it can far exceed the inbuilt serializers (1 2 3 )

like image 45
Marc Gravell Avatar answered Sep 29 '22 04:09

Marc Gravell