serialize in .NET, deserialize in C++

前端 未结 7 881
死守一世寂寞
死守一世寂寞 2021-02-13 20:03

I have a .NET application which serializes an object in binary format. this object is a struct consisting of a few fields.

I must deserialize and use this object in a C+

相关标签:
7条回答
  • 2021-02-13 20:27

    If you are using BinaryFormatter, then it will be virtually impossible. Don't go there...

    Protocol buffers is designed to be portable, cross platform and version-tolerant (so it won't explode when you add new fields etc). Google provide the C++ version, and there are several C# versions freely available (including my own) - see here for the full list.

    Small, fast, easy.

    Note that the v1 of protobuf-net won't handle structs directly (you'll need a DTO class), but v2 (very soon) does have tested struct support.

    0 讨论(0)
  • 2021-02-13 20:30

    Use XML Serialization its the best way to go, in fact is the cleanest way to go.

    XmlSerializer s = new XmlSerializer( typeof( YourClassType ) );
    TextWriter w = new StreamWriter( @"c:\list.xml" );
    s.Serialize( w, yourClassListCollection );
    w.Close();
    
    0 讨论(0)
  • 2021-02-13 20:33

    Both boost and Google have libraries for serialization. However, if your struct is pretty trivial, you might consider managing the serialization yourself by writing bytes out from C# and then reading the data in C++ with fread.

    0 讨论(0)
  • 2021-02-13 20:34

    Agree with others. You are making your app very vulnerable by doing this. Consider the situation if one of the classes you're serializing is changed in any way or built on a later version of the C# compiler: Your serialized classes could potentially change causing them to be unreadable.

    An XML based solution might work well. Have you considered SOAP? A little out of fashion now but worth a look. The main issue is to decouple the implementation from the data. You can do this in binary if speed / efficiency is an issue, although in my experience, it rarely is.

    0 讨论(0)
  • 2021-02-13 20:36

    Serializing in a binary format and expecting an application in another language to read the binary is a very brittle solution (ie it will tend to break on the smallest change to anything).

    It would be more stable to serialize the data in a common standard format.

    0 讨论(0)
  • 2021-02-13 20:46

    Do you have the option of changing the format? If so, consider choosing a non-binary format for greater interoperability. There are plenty of libraries for reading and writing XML. Json is popular as well.

    Binary formats are efficient, but vulnerable to implementation details (does your C++ compiler pack data structures? how are ints and floats represented? what byte ordering is used?), and difficult to adjust if mangled. Text based formats are verbose, but tend to be much more robust. If you are uncertain about binary representations, text representations tend to be easier to understand (apart from challenges such as code pages and wide/narrow characters...).

    For C++ XML libraries, the most capable (and perhaps also most complex) would still seem to be the Xerces library. But you should decide for yourself which library best fits your needs and skills.

    0 讨论(0)
提交回复
热议问题