Boost序列化:如何在运行时选择版本?
我正在使用 Boost Serialization 来序列化数据成员用于从一个组件传输到另一个组件。该文档讨论了如何使用类版本机制 反序列化类的旧版本和新版本。
我的用例略有不同。在某些情况下,新版本的代码会将对象发送到旧版本。旧版本不会有新的定义,因此它无法使用该机制来读取新版本。相反,我想以编程方式设置 Boost 类版本,并在连接到旧客户端时生成该类的旧版本。问题在于版本似乎在编译时设置为静态常量。
我可以使用 Boost 提供的工具来完成此任务,还是必须将自己的版本信息添加到类的每个实例中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法动态影响 Boost.Serialization 使用的类型版本,因为它是编译时常量(正如您提到的)。您将需要添加自己的版本控制方案来处理这种特殊情况。
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.
您所说的是向前(或向上)兼容性。即使您所做的一切都是添加新的类成员,那么您仍然无法使用标准 boost 档案获得向前兼容性。
boost xml 存档有一个补丁,它将跳过新的未知字段。这样,您在添加新的类成员时将获得向前兼容性。
另一个是 3rdparty ptree archive。它将数据存储到 boost::property_tree::ptree 中,您可以将其写为 json。加载时它将忽略未知的新字段。
人们可以在 Protocol Buffers 中立即获得向前兼容性
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