简体   繁体   中英

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 ie, 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

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/

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 )

Another possibility would be Thrift , it has even more backends and if necessary provides a good part of the code required for network communication - in case you want to scale out.

If you only want easy object serialization I would have a look at json.org There are plenty of C++ / .NET implementations around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM