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”或类似的东西。

最佳答案

是的,解决方案是在名称-值对中序列化您的类。请参阅Boost文档中的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)。如果您需要不同的内容,则需要实现自己的存档类。

关于c++ - boost::archive::xml_oarchive中的class_id,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1159429/

10-11 16:24