Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost serialization: how to choose version at runtime?

I am using Boost Serialization to serialize data members for transport from one component to another. The documentation discusses how to use the class version mechanism to unserialize both older and newer versions of a class.

My use case is slightly different. In some cases, a newer version of the code will be sending objects to an older version. The older version won't have the new definition, and so it won't be able to use that mechanism to read the newer version. Instead, I'd like to programmatically set the Boost class version back and generate the old version of the class when connected to older clients. The problem is that it appears that the version is set at compile-time as a static const.

Can I accomplish this with the tools supplied by Boost, or do I have to add my own version information into each instance of the class?

like image 862
shmuelp Avatar asked Oct 28 '25 06:10

shmuelp


2 Answers

There is no way to dynamically influence the type version used by Boost.Serialization as it's a compile time constant (as you mentioned). You will need to add your own versioning scheme to handle this particular case.

like image 148
hkaiser Avatar answered Oct 29 '25 20:10

hkaiser


What you are talking is forward (or upward) compatibility. Even if everything you do is adding new class members, then you still can not get forward compatibility using standard boost archives.

There is a patch for boost xml archive which will skip new unknown fields. This way you will get forward compatibility when adding new class members.

Another is 3rdparty ptree archive. It stores data to boost::property_tree::ptree which you can write as json. It will ignore unknown new fields when loading.

One can get forward compatibility out of a box in Protocol Buffers

like image 30
sena Avatar answered Oct 29 '25 22:10

sena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!