问题
I am trying to use the boost library to serialize on 64bit machine and de-serialize on 32bit machine. However, it seems it does not work. (I use 1.57.0).
If I run the following code
boost::asio::streambuf buf;
std::ostream os(&buf);
boost::archive::binary_oarchive oa(os);
printf("Buffer size %d\n",(int)buf.size());
The output of 32bit machine is 37 and the output of 64bit machine is 41.
Is there any other good serialize library I can Use? How about cereal?
It's great if the library can do compression as well (zlib/gzip etc.).
回答1:
It does work. It just doesn't create compatible archives. If you want that you should look at the archive implementation that EOS made:
- EOS Portable Archive
You can drop-in replace Boost's binary_[io]archive with it. No need to change anything else.
PS. Of course, spell out your types in an architecture-independent way too, of course. So uint32_t
, not ``size_t`
回答2:
The binary archives created by boost::serialization will not work if you change the architecture of the machine. The text archives are a good option in this scenario. Boost::archive::text_oarchive and boost::archive::text_iarchive can be used the exact same way but are safe across architectures and platforms. The data is written in an ascii format instead of a binary format so there are trade offs there that will need to be addressed for your purpose.
回答3:
I'd recommend using 'cereal' for this purpose which could provide JSON/XML serialization.
来源:https://stackoverflow.com/questions/30631630/boost-serialization-does-not-work-between-32bit-and-64bit-machine-any-other-ser