Home > Blockchain >  Does using different Boost versions affect serialization and deserialization?
Does using different Boost versions affect serialization and deserialization?

Time:10-30

I'm working on some project in C where I'm using Boost for binary serialization and deserialization. Serialization feature is already present with Boost version 1.61 I made my whole deserialization add on feature using Boost version 1.77 and now I'm facing problem while reading binary files. So, my question is how does this difference in version for deserialization affects the process? Because I'm unable to properly read the binary file.

Code using Boost version 1.61 for serialization

#include <boost/archive/binary_oarchive.hpp>
#include <fstream>
#include <string>

class Frame{
public:
    std::string str;
};
template <typename Archive>
void serialize(Archive& ar, Frame& f, const unsigned int version) {
    ar& f.str;
}

uint32_t main () {
    Frame f={"Frame example"};
    std::ofstream ofs;
    ofs.open("BinaryFile.bin",std::ios::out, std::ios::binary);
    boost::archive::text_oarchive write(ofs,boost::archive::no_header);
    write << f;
    ofs.close();
}

Code using boost version 1.77 for deserialization

#include <boost/archive/binary_iarchive.hpp>
#include <fstream>
#include <string>

class Frame{
public:
    std::string str;
};
template <typename Archive>
void serialize(Archive& ar, Frame& f, const unsigned int version) {
    ar& f.str;
}

uint32_t main () {
    Frame f;
    std::ofstream ofs;
    ofs.open("BinaryFile.bin",std::ios::in, std::ios::binary);
    boost::archive::text_oarchive read(ofs,boost::archive::no_header);
    read >> f;
    ofs.close();
}

This is just an example code the frame I am using is different but approach is same.

CodePudding user response:

Encoding stability is not a property of boost serialization itself, but is instead handled on a per-archive basis.

You can use get_library_version() on the archive to check for compatibility across boost versions.

Returns an unsigned integer containing the current version number of the serialization library. This number will be incremented each time the library is altered in such a way that serialization could be altered for some type. [...]

CodePudding user response:

There is backwards compatibility. But it requires the archive headers to be present so the library can detect the version support required for de-serialization.

Without the header, there's no way to tell, so the library must assume it's the most current version.

Removing that should work, see it live:

On my machine the read side worked with the same file in Boost 1.77.0 (not available on wandbox):

Success: Frame example

Summarizing

You should probably switch the file format to use the headers. This will probably require some kind of conversion tool. If all else fails you might opt to distinguish the versions by their filename/extension. That's brittle, but if it's the only thing you can control it might save you.

  • Related