Does Boost.Serialization serialize differently on different platforms?

≯℡__Kan透↙ 提交于 2019-12-18 03:58:36

问题


I use Boost.Serialization to serialize a std::map. The code looks like this

void Dictionary::serialize(std::string & buffer)
{
  try {
    std::stringstream ss;
    boost::archive::binary_oarchive archive(ss);
    archive << dict_; 
    buffer = ss.str();
  } catch (const std::exception & ex) {
    throw DictionaryException(ex.what());
  }
}

void Dictionary::deserialize(const char * const data, int length)
{
  try {
    namespace io = boost::iostreams;
    io::array_source source(data, length);
    io::stream<io::array_source> in(source);
    boost::archive::binary_iarchive archive(in);
    archive >> dict_;
  } catch (const std::exception & ex) {
    throw DictionaryException(ex.what());
  }
}

I compiled and tested the code on a Mac Snow Leopard and on Ubuntu Lucid 10.04. There is Boost 1.40 on both systems. On the Mac I built the code myself. On the Ubuntu box I got the binaries via aptitude.

Problem: When I serialize the map on the Mac I can't deserialize it on the Ubuntu box. I get an invalid signature exception if I try.


回答1:


try using a text_iarchive and text_oarchive instead of binary archives. From the documentation

In this tutorial, we have used a particular archive class - text_oarchive for saving and text_iarchive for loading. text archives render data as text and are portable across platforms. In addition to text archives, the library includes archive class for native binary data and xml formatted data. Interfaces to all archive classes are all identical. Once serialization has been defined for a class, that class can be serialized to any type of archive.




回答2:


boost:archive::binary_xarchive are currently not portable

With my interpretation that means that there can be differences on different platforms. The text archives gives you the same input/output behaviour on all systems.
Also there is a related TODO entry which tries to solve the portability issue of the binary archives: TODO Entry




回答3:


The performance with text_archives is magnitudes slower than binary_archive. If performance is your thing, you may try out an unofficial portable binary archive eos_portable_archive. I've used it to serialize data across 32 bit and 64 bit on Windows with success. You may give it a go.

Just need to put the files in your serialization directory. The files there are not up to date with the latest boost version (1.44.0) but you just need to make 2 very trivial adjustments to make it work (your compiler will tell you with very obvious error message).




回答4:


I agree with the answers, but wanted to add a clarifying note. You might think this is an annoying oversight, but in fact coming up with and implementing a portable binary format is not such a trivial task. The only standard that I'm aware of that effectively tackles the problem in binary is ASN.1.

XML pruports to tackle the same problem, but generally does it in text. There's a piggyback standard for XML called Fast Infoset that allows XML to encode the data in binary form instead, but it uses ASN.1.



来源:https://stackoverflow.com/questions/3708842/does-boost-serialization-serialize-differently-on-different-platforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!