Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

c++11 - how to customise default Boost xml Serialisation default node naming to make it more readable

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

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

Current Output:

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

Desired Output

<?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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Serialization for the map is a generic implementation.

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

See it Live On Coliru

This prints (excerpt):

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

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

Disclaimer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...