提升类序列化,改变成员类型

时间:2014-04-22 21:17:02

标签: c++ serialization boost boost-serialization

如何处理切换序列化成员的类型,同时保持与以前存档的兼容性?例如。我想将float/int更改为double/size_t

我知道我可以增加版本号,但这会使代码变得混乱。有没有不同的方法来处理?如果它有所不同,则成员通过MAKE_NVP序列化。

1 个答案:

答案 0 :(得分:1)

您可以使用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'