Cross-platform and language (de)serialization

后端 未结 7 1099
一个人的身影
一个人的身影 2021-02-07 22:13

I\'m looking for a way to serialize a bunch of C++ structs in the most convenient way so that the serialization is portable across C++ and Java (at a minimum) and across 32bit/6

7条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 22:43

    I stumbled here, having a very similar question. 6 years later, this might not be useful to you, but hopefully it will be to others.

    There are a lot of alternatives, unfortunately with no clear winner (although one could argue that JSON is the clear winner). Even Google has released multiple competing technologies (all of them apparently being used internally):

    • FlatBuffers: this one seems to meet the requirements from the original question, has interesting benchmarks and supports some form of IDL (I'm personally not familiar with IDL)
    • Protocol Buffers: mentioned previously.
    • XFJSON: 5%-12% smaller than JSON.

    Not to forget the alternatives posted in the other answers. Here are a few more:

    • YAML: JSON minus all the double quotes, but using indentation instead. It's more human readable, but probably less efficient, especially as it gets larger.
    • BSON (Binary JSON)
    • MessagePack (Another compacted JSON)

    With so many variations, JSON is clearly the winner in terms of simplicity/convenience and cross-platform access. It has gained even more popularity in the last couple years, with the rise of JavaScript. A lot of people probably use that as a de-facto solution, without giving it much thought (that's what I originally did :P).

    However, if size becomes an issue, but you prefer to keep things simple and not use one of the more advanced libraries, you could just compress JSON using zlib (that's what I'm doing now), or some other cross-platform algorithm (but that's a whole other topic).

    To speed up JSON handling in C++, you could also use RapidJSON.

提交回复
热议问题