使用不同的Boost版本会影响序列化和反序列化吗?
Does using different Boost versions affect serialization and deserialization?
我正在用 C++ 开发一些项目,我在其中使用 Boost 进行二进制序列化和反序列化。 Boost 1.61 版已经提供了序列化功能 我使用 Boost 1.77 版添加了整个反序列化功能,现在我在读取二进制文件时遇到了问题。所以,我的问题是反序列化版本的这种差异如何影响这个过程?因为我无法正确读取二进制文件。
使用 Boost 版本 1.61 进行序列化的代码
#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();
}
使用 boost 1.77 版本进行反序列化的代码
#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();
}
这只是一个示例代码,我使用的框架不同,但方法相同。
编码稳定性不是 属性 提升序列化本身,而是在每个存档的基础上处理。
您可以在存档上使用 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. [...]
具有向后兼容性。但它需要存档 headers 存在,以便库可以检测 de-serialization.
所需的版本支持
如果没有 header,则无法判断,因此库必须假定它是最新版本。
删除应该有效,实时查看:
- 在 1.61.0 中保存:https://wandbox.org/permlink/PkqDp4fWrno9GLql
- 在 1.76.0 中阅读:https://wandbox.org/permlink/PAwLOhVg0STNGN59
在我的机器上,读取端使用 Boost 1.77.0 中的相同文件(在 wandbox 上不可用):
Success: Frame example
总结
您可能应该切换文件格式以使用 headers。这可能需要某种转换工具。如果所有其他方法都失败了,您可以选择通过 filename/extension 来区分版本。这很脆弱,但如果这是您唯一可以控制的东西,它可能会拯救您。
我正在用 C++ 开发一些项目,我在其中使用 Boost 进行二进制序列化和反序列化。 Boost 1.61 版已经提供了序列化功能 我使用 Boost 1.77 版添加了整个反序列化功能,现在我在读取二进制文件时遇到了问题。所以,我的问题是反序列化版本的这种差异如何影响这个过程?因为我无法正确读取二进制文件。
使用 Boost 版本 1.61 进行序列化的代码
#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();
}
使用 boost 1.77 版本进行反序列化的代码
#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();
}
这只是一个示例代码,我使用的框架不同,但方法相同。
编码稳定性不是 属性 提升序列化本身,而是在每个存档的基础上处理。
您可以在存档上使用 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. [...]
具有向后兼容性。但它需要存档 headers 存在,以便库可以检测 de-serialization.
所需的版本支持如果没有 header,则无法判断,因此库必须假定它是最新版本。
删除应该有效,实时查看:
- 在 1.61.0 中保存:https://wandbox.org/permlink/PkqDp4fWrno9GLql
- 在 1.76.0 中阅读:https://wandbox.org/permlink/PAwLOhVg0STNGN59
在我的机器上,读取端使用 Boost 1.77.0 中的相同文件(在 wandbox 上不可用):
Success: Frame example
总结
您可能应该切换文件格式以使用 headers。这可能需要某种转换工具。如果所有其他方法都失败了,您可以选择通过 filename/extension 来区分版本。这很脆弱,但如果这是您唯一可以控制的东西,它可能会拯救您。