如何自定义默认的 Boost xml 序列化默认节点命名以使其更具可读性
how to customise default Boost xml Serialisation default node naming to make it more readable
下面的代码生成了一个 xml 文件,但是当它循环遍历一个地图时,它总是将地图键命名为 first
,并将值命名为 second
有没有一种方法可以将标签名称 first
和 second
自定义为 groupid
和 groupType
,如所需输出所示
#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>
地图的序列化是一种通用实现。
您当然可以提供/更好的匹配/并覆盖命名:
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);
}
} }
这会打印(摘录):
<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 -> string
的所有地图都获得相同的命名,所以一定要看看http://www.boost.org/doc/libs/1_63_0/libs/serialization/doc/strong_typedef.html
免责声明
I'd suggest that if you want/need this level of control, you shouldn't not be using XML archive.
下面的代码生成了一个 xml 文件,但是当它循环遍历一个地图时,它总是将地图键命名为 first
,并将值命名为 second
有没有一种方法可以将标签名称 first
和 second
自定义为 groupid
和 groupType
,如所需输出所示
#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>
地图的序列化是一种通用实现。
您当然可以提供/更好的匹配/并覆盖命名:
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);
}
} }
这会打印(摘录):
<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 -> string
的所有地图都获得相同的命名,所以一定要看看http://www.boost.org/doc/libs/1_63_0/libs/serialization/doc/strong_typedef.html
免责声明
I'd suggest that if you want/need this level of control, you shouldn't not be using XML archive.