c-Boost类序列化,成员类型更改
作者:互联网
如何在保持与先前存档的兼容性的同时切换序列化成员的类型?例如.我想将float / int更改为double / size_t.
我知道我可以增加版本号,但这会使代码混乱.有其他处理方式吗?如果有所不同,成员将通过MAKE_NVP进行序列化.
解决方法:
您可以使用version参数进行版本控制.文档给出了以下示例:http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/tutorial.html#versioning
请注意如何通过BOOST_CLASS_VERSION宏指定序列化期间的版本.
这是使用以下版本的数据结构的概念证明:See it Live On Coliru
struct DataV0
{
float f = 3.14;
int i = 42;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
if (0 != version)
throw std::runtime_error("Unsupported version");
ar & f;
ar & i;
}
};
现在,以您的V1序列化格式,您已经按照问题中的内容更改了类型:
struct DataV1
{
double d = 3.14;
size_t ul = 42;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
switch(version)
{
case 0:
{
DataV0 old;
ar & old.f;
ar & old.i;
d = old.f;
assert(old.i >= std::numeric_limits<size_t>::min());
assert(old.i <= std::numeric_limits<size_t>::max());
ul = static_cast<size_t>(old.i);
}
break;
case 1:
ar & d;
ar & ul;
break;
}
}
};
BOOST_CLASS_VERSION(DataV1, 1)
完整的测试程序:
template <typename Data>
std::string to_string(Data d)
{
std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
oa << d;
return oss.str();
}
template <typename Data>
bool verify(std::string const& text)
{
std::istringstream iss(text);
boost::archive::text_iarchive ia(iss);
Data d;
ia >> d;
return text == to_string(d);
}
int main()
{
std::string v0text = to_string(DataV0());
std::string v1text = to_string(DataV1());
std::cout << v0text << '\n';
std::cout << v1text << '\n';
std::cout << "v0 as v0: " << std::boolalpha << verify<DataV0>(v0text) << "\n";
std::cout << "v0 as v1: " << std::boolalpha << verify<DataV1>(v0text) << "\n";
std::cout << "v1 as v1: " << std::boolalpha << verify<DataV1>(v1text) << "\n";
try {
std::cout << "v1 as v0: " << std::boolalpha << verify<DataV0>(v1text) << "\n";
} catch (std::exception const& e)
{
std::cerr << "Threw the expected '" << e.what() << "'\n";
}
}
打印:
22 serialization::archive 10 0 0 3.1400001 42
22 serialization::archive 10 0 1 3.1400000000000001 42
v0 as v0: true
v0 as v1: false
v1 as v1: true
v1 as v0: Threw the expected 'Unsupported version'
标签:c,serialization,boost,boost-serialization 来源: https://codeday.me/bug/20191009/1882301.html