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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With