boost :: archive :: xml_oarchive中的class_id

时间:2009-07-21 14:08:02

标签: c++ boost-serialization

XML序列化是否可以使用更友好的class_id作为GUID,使用BOOST_CLASS_EXPORT_GUID ???

进行描述

考虑序列化类:

SomeClass* b=new SomeClass("c");
{
    boost::archive::xml_oarchive oa(cout);
    oa.register_type<SomeClass>();
    oa << boost::serialization::make_nvp("b",b);
}

输出将如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="5">
<b class_id="0" tracking_level="1" version="0" object_id="_0">
<name>c</name>
</b>
</boost_serialization>

删除class_id =“0”时,不会反序列化。我更喜欢class_id =“SomeClass”或类似的东西。

1 个答案:

答案 0 :(得分:4)

是的,解决方案是在名称 - 值对中序列化您的类。请参阅增强文档中的this item

如果您想要两种不同的行为,则必须实施它们。尝试使用模板专业化:

template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
    ar & degrees;
    ar & minutes;
    ar & seconds;
}

template<class Archive>
void serialize_with_name(Archive & ar, const unsigned int version)
{
    ar & make_nvp("degrees", degrees);
    ar & make_nvp("minutes", minutes);
    ar & make_nvp("seconds", seconds);
}

template<>
void serialize<>(xml_iarchive & ar, const unsigned int version)
{
    serialize_with_name(ar, version);
}

template<>
void serialize<>(xml_oarchive & ar, const unsigned int version)
{
    serialize_with_name(ar, version);
}

默认情况下,object_id_type是unsigned int(basic_archive.hpp)。如果你想要不同的东西,你需要实现自己的档案类。