4

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.).

venusisle
  • 113
  • 2
  • 10

3 Answers3

5

It does work. It just doesn't create compatible archives. If you want that you should look at the archive implementation that EOS made:

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`

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks!! I add the files to boost\archive and #include , however, it still shows error C2039: 'portable_oarchive' : is not a member of 'boost::archive', any ideas? – venusisle Jun 03 '15 at 22:34
  • A very casual glance at the header tells you the namespace is `eos`, not `boost::archive`. _[After all, it's pretty obvious it is not part of boost, right?]_ – sehe Jun 03 '15 at 23:35
  • 1
    This solution was fantastic - I realised I had to spend many days developing a cross-platform w32 win/x64 Mac archive solution for my project. I used boost serialise functions, and I got signature exceptions, and with this, I just had to include 5 hpp files in my project, change two lines, and add two includes. That was all, and it worked immediately. Thank you for your tips about this. – olekeh Feb 12 '19 at 18:48
  • @ olekeh Cheers. Hat tip to @je4d who told me about this in 2013, at Meeting C++ in Düsseldorf. I made a strong mental note at the time :) – sehe Feb 12 '19 at 19:19
1

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.

Josh Edwards
  • 369
  • 1
  • 4
-1

I'd recommend using 'cereal' for this purpose which could provide JSON/XML serialization.

atari83
  • 489
  • 1
  • 5
  • 15
  • cereal will generally work for the binary archives so long as you are careful to serialize every type with its size explicitly stated (e.g. use `std::int32_t` instead of `int`, etc.). cereal does this internally so the onus is on the user to ensure their types also comply. – Azoth Nov 15 '16 at 22:51
  • however, cereal as of this writing does not deal with binary differences between 32-bit and 64-bit pointers, smart or otherwise. – johnwbyrd Feb 23 '17 at 05:15