如何自定义默认的 Boost xml 序列化默认节点命名以使其更具可读性 [英] how to customise default Boost xml Serialisation default node naming to make it more readable

查看:17
本文介绍了如何自定义默认的 Boost xml 序列化默认节点命名以使其更具可读性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码生成一个 xml 文件,但是,当它循环映射时,它总是将映射键命名为 first,将值命名为 second

The code below generates a xml file but , when it loops theough a map , it always names the map key as first and value as second

有没有办法可以自定义标签名称firstsecondgroupidgroupType,如图所示在所需的输出

Is there a way that we can customise tag names first and second to groupid and groupType as shown in desired output

 #include <fstream>
#include <boost/serialization/map.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <string>
#include <iostream>
#include <map>

using namespace std;

class MyConnections
{
  public:
    MyConnections()
    {

       e_group.insert( std::make_pair(1,"ETOTO") ) ;
       e_group.insert( std::make_pair(2,"ETOTO") ) ;

   }

    template<class archive>
    void serialize(archive& ar, const unsigned int version)
    {
        using boost::serialization::make_nvp;
        ar & make_nvp("Connections", e_group);
    }

  public:
   typedef   map<int,std::string> groups;
   groups  e_group;
};

int main(int argc, char* argv[])
{
    std::ofstream ofs("output.xml");
    const MyConnections conn;
    boost::archive::xml_oarchive xml(ofs);
    xml << boost::serialization::make_nvp("Connections", conn);
}

电流输出:

     <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="14">
<Connections class_id="0" tracking_level="0" version="0">
        <Connections class_id="1" tracking_level="0" version="0">
                <count>2</count>
                <item_version>0</item_version>
                <item class_id="2" tracking_level="0" version="0">
                        <first>1</first>
                        <second>ETOTO</second>
                </item>
                <item>
                        <first>2</first>
                        <second>ETOTO</second>
                </item>
        </Connections>
</Connections>
</boost_serialization>

期望输出

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="14">
<Connections class_id="0" tracking_level="0" version="0">
        <Connections class_id="1" tracking_level="0" version="0">
                <count>2</count>
                <item_version>0</item_version>
                <item class_id="2" tracking_level="0" version="0">
                        <groupid>1</groupid>
                        <groupType>ETOTO</groupType >
                </item>
                <item>
                        < groupid >2</groupid >
                        < groupType >ETOTO</groupType >
                </item>
        </Connections>
</Connections>
</boost_serialization>

推荐答案

地图的序列化是一个通用的实现.

Serialization for the map is a generic implementation.

您当然可以提供/better match/并覆盖命名:

You can of course provide a /better match/ and override the naming:

namespace boost { namespace serialization { 
    template <typename Ar>
        void serialize(Ar& ar, std::pair<int const, std::string>& p, unsigned) {
            ar & make_nvp("groupid", p.first) & make_nvp("groupType", p.second);
        }
} }

看到它在 Coliru 上直播

See it Live On Coliru

这打印(摘录):

<Connections class_id="1" tracking_level="0" version="0">
    <count>2</count>
    <item_version>0</item_version>
    <item class_id="2" tracking_level="0" version="0">
        <groupid>1</groupid>
        <groupType>ETOTO</groupType>
    </item>
    <item>
        <groupid>2</groupid>
        <groupType>ETOTO</groupType>
    </item>
</Connections>

当然,这有int -> ALL映射的问题.string 获得相同的命名,所以一定要查看 http://www.boost.org/doc/libs/1_63_0/libs/serialization/doc/strong_typedef.html

Of course, this has the problem that ALL maps of int -> string get the same naming, so be sure to look at http://www.boost.org/doc/libs/1_63_0/libs/serialization/doc/strong_typedef.html

我建议,如果您想要/需要这种级别的控制,则不应使用 XML 存档.

I'd suggest that if you want/need this level of control, you shouldn't not be using XML archive.

这篇关于如何自定义默认的 Boost xml 序列化默认节点命名以使其更具可读性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆