Boost logo

Boost :

From: Chris Weed (chrisweed_at_[hidden])
Date: 2008-05-15 15:08:41

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.

#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>
inline const
make_nvp<Bob>(const char * name,Bob& e)
    return nvp<Bob>("Bob", e);

template<class Archive>
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">
        <Bob class_id="1" tracking_level="0" version="0">

Boost list run by bdawes at, gregod at, cpdaniel at, john at