customizable std::vector item name

Hi, I am interested in using the xml serialization code for std::vector, but currently it is hard-coded to name items, "item". Is this customizable by overloading/specializing make_nvp for my type to replace "item" with a type-specific name? I tried the following code which didn't accomplish this. Chris #include <boost/archive/xml_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/serialization/vector.hpp> #include <sstream> #include <iostream> struct Bob { int i; }; namespace boost { namespace serialization { /** * Try to rename items in vector<Bob> */ template<> inline const nvp<Bob> make_nvp<Bob>(const char * name,Bob& e) { return nvp<Bob>("Bob", e); } template<class Archive> void serialize(Archive& ar, Bob& v, const unsigned int version) { ar & boost::serialization::make_nvp("i",v.i); } }} int main() { Bob b; b.i = 2; std::vector<Bob> e(1); e[0] = b; std::stringstream ss; { boost::archive::xml_oarchive oa(ss); oa << boost::serialization::make_nvp("Bobs",e); } std::cout << ss.str() << std::endl; return 0; } I would like this to print the following: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="4"> <Bobs class_id="0" tracking_level="0" version="0"> <count>1</count> <item_version>0</item_version> <Bob class_id="1" tracking_level="0" version="0"> <i>2</i> </Bob> </Bobs> </boost_serialization>

There's some confusion here, the name in name value pair is used for the name data - not the typename. In any case, you might try deriving from xml_oachive and re-implement the following function: typedef detail::common_oarchive<Archive> detail_common_oarchive; template<class T> void save_override( #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING const #endif ::boost::serialization::nvp<my_type> & t, int ){ save_start(t.name()); // t.name includes "item" replace with whatever you choose like "my_type" + t.name this->detail_common_oarchive::save_override(t.const_value(), 0); save_end(t.name()); Of course this would likely have all manner of undesired side-effects - like changing ALL the tags for this type - not just "item". And of course it would only work for my_type, etc.,etc. Robert Ramey Chris Weed wrote:
Hi, I am interested in using the xml serialization code for std::vector, but currently it is hard-coded to name items, "item". Is this customizable by overloading/specializing make_nvp for my type to replace "item" with a type-specific name? I tried the following code which didn't accomplish this. Chris
participants (2)
-
Chris Weed
-
Robert Ramey