简体   繁体   English

C ++和C#中的二进制序列化/反序列化

[英]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#. 一个用标准C ++编写( 不是托管C ++ ,在Linux平台上运行),另一个用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). 我有一种情况需要将对象从C ++传递到C#应用程序,为此我需要在C ++中序列化这些对象并在C#中对它们进行反序列化(类似于.NET中的编组/解组)。 I need to perform this serialization in binary and not in XML (due to performance reasons). 我需要以二进制而不是XML(由于性能原因)执行此序列化。

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. 我已经使用Boost.Serialization来实现这一点,当两端都是用C ++实现的,但现在我的一端有一个.NET应用程序, Boost.Serialization不是一个可行的解决方案。

I am looking for a solution that allows me to perform (de)serialization across C++ and .NET boundary ie, cross platform binary serialization . 我正在寻找一种解决方案,允许我跨C ++和.NET边界执行(反)序列化,即跨平台二进制序列化

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. 我知道我可以在C ++ DLL中实现(de)序列化代码并在.NET应用程序中使用P/Invoke ,但我想将其作为最后的手段。

Also, I want to know if I use some standard like gzip, will that be efficient? 另外,我想知道我是否使用像gzip这样的标准,这会有效吗? Are there any other alternatives to gzip? 除了gzip还有其他选择吗? What are the pros/cons of them? 它们的优点/缺点是什么?

Thanks 谢谢

I would recommend Protocol Buffers, which is Googles own serialization library. 我建议使用Protocol Buffers,这是Googles自己的序列化库。 It has both .Net, C++ and Java serializers. 它有.Net,C ++和Java序列化器。 Most implementations are also quite fast. 大多数实现也非常快。

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

gzip won't directly help with serialization - it will just (attempt to) shrink a stream. gzip不会直接帮助序列化 - 它只会(尝试)缩小流。 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. 对于文本很少的密集数据,我看到gzip 增加了有效载荷的大小。

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. 您通常(但不总是)使用基本语言(.proto文件)定义消息,并运行特定于语言的工具来生成类。 Performance is very good - focusing on .NET it can far exceed the inbuilt serializers ( 1 2 3 ) 性能非常好 - 专注于.NET,它可以远远超过内置的序列化器( 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. 另一种可能性是Thrift ,它有更多的后端,如果需要,它提供了网络通信所需的大部分代码 - 如果你想扩展的话。

If you only want easy object serialization I would have a look at json.org There are plenty of C++ / .NET implementations around. 如果你只想要简单的对象序列化,我会看一下json.org。有很多C ++ / .NET实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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