|
Boost : |
From: Chris Weed (chrisweed_at_[hidden])
Date: 2008-05-15 15:08:41
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>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk