开发者

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.

开发者_如何学Python

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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜