boost::archive::xml_oarchive 中的 class_id

发布于 2024-07-28 21:27:06 字数 736 浏览 2 评论 0原文

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

Is it possible for XML serialization to use more human friendly class_id as GUID, described using BOOST_CLASS_EXPORT_GUID ???

Consider serializing class:

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

Output will be like:

<?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>

When you remove class_id="0" this will not deserialize. I would prefer class_id="SomeClass" or something similar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱暴击 2024-08-04 21:27:12

是的,解决方案是将您的类序列化为名称值对。 请参阅 boost 文档中的此项

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

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)。 如果您想要不同的东西,您需要实现自己的存档类。

Yes, the solution is to serialize your class in a name-value-pair. See this item at boost documentation.

If you want two diferent behaviours, you will have to implement them. Try with template specialization:

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);
}

By default object_id_type is unsigned int (basic_archive.hpp). If you want something diferent you need to implement your own archive class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文