boost text serialization version (15 vs 18)

Viewed 266

I have a custom class containing only a std::map<std::string, int>. I'm trying to serialize that using boost::serialization.

I create an object with the data {"foor":13} (this is the map in my custom object). Boost serializes that as

22 serialization::archive 15 22 serialization::archive 15 0 0 0 0 1 0 0 0 4 foor 13

So far, so good (?).

Now, I take the same source (with few adaptations), I compile as wasm instead of x86. The same object is now serialized as

22 serialization::archive 18 0 0 0 0 1 0 0 0 4 foor 13

So, for some reasons, in one case, Boost chooses to use version 15 of the archive while in the other case, it chooses version 18. (obviously, unserialization of the x86-generated does not work in the wasm program)

How can I "force" Boost to use version 15 of the archive ?

2 Answers

The first serialized sample looks to be invalid alltogether.

Secondly, let's check that the 15 and or 18 are the archive version numbers:

#include <boost/archive/text_oarchive.hpp>
#include <iostream>
int main() {
    unsigned v;
    {
        boost::archive::text_oarchive oa(std::cout);
        v = oa.get_library_version();
    }

    std::cout << "# get_library_version() -> " << v << "\n";
}

Prints (see live)

22 serialization::archive 17
# get_library_version() -> 17

So, yes, that look accurate

Since they are hardcoded in the archive implementation, you'll have to use the corresponding Boost library version on both sides.

Relevant doc snippet:

get_library_version()

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. For example, suppose the type used for a count of collection members is changed. The code that loads collections might be conditioned on the library version to make sure that libraries created by previous versions of the library can still be read.

The tricky point was to build Boost::serialization for wasm and x86.

Clone boost, enter the cloned directory.

wasm

I ended up with this line (patchwork of many researches on the web; I'm not even sure of the meaning/usefulness of each argument)

./b2 toolset=emscripten link=static variant=release threading=single --prefix=build_wasm --build-dir=build_wasm runtime-like=static serialization

x86

./bootstrap.sh --prefix=build_x86
./b2

next

At this point, we have two separated directories build_wasm and build_x86 to link to.

At this point, I do not remember how I managed to get the file libboost_serialization.bc.

cmake

In my CMakeLists.txt, the key lines are:

set(BoostWasm_DIR "/absolute/path/to/build_wasm")
set(BoostWasm_emm "${BoostWasm_DIR}/boost/bin.v2/libs/serialization/build/emscripten-1.39.13/release/link-static/visibility-hidden")

include_directories(${BoostWasm_DIR}/include)

file(GLOB boost_js "${BoostWasm_emm}/libboost_serialization.bc")
target_link_libraries( write_normalized ${boost_js} )

Since it is a work related project, I do not have a link to a full working example.

build

emcmake cmake .
emmake make
Related