how to customise default Boost xml Serialisation default node naming to make it more readable

前端 未结 1 620
广开言路
广开言路 2020-12-12 01:36

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

相关标签:
1条回答
  • 2020-12-12 01:59

    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.

    0 讨论(0)
提交回复
热议问题