[Serialization] NVP and xml archives
Hi All, I have a class that contains a list of name value pairs . I would like the serialization to use the names for the tag names in the xml archive. Is there a way to do this or do I need to write my own custom archive? Thanks in advance Michael __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Doesn't it already do this? Robert Ramey michael mathews wrote:
Hi All,
I have a class that contains a list of name value pairs . I would like the serialization to use the names for the tag names in the xml archive. Is there a way to do this or do I need to write my own custom archive?
Thanks in advance
Michael
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Well I thought it should and I suspect that I am doing something silly but this is basically the class I have: template <class N, class V> class test { friend class boost::serialization::access; list< boost::serialization::nvp<V> > sl; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & sl; } public: { ... } and it gets caught in the compile trap template<class T> void save_override(T & t, BOOST_PFTO int) { // If your program fails to compile here, its most likely due to // not specifying an nvp wrapper around the variable to // be serialized. BOOST_STATIC_ASSERT(0 == sizeof(T)); } I thought that a nvp should be serializable automatically. Ideas? Thanks in advance michael --- Robert Ramey <ramey@rrsd.com> wrote:
Doesn't it already do this?
Robert Ramey
Hi All,
I have a class that contains a list of name value pairs . I would
michael mathews wrote: like
the serialization to use the names for the tag names in the xml archive. Is there a way to do this or do I need to write my own custom archive?
Thanks in advance
Michael
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________________________________________ Sponsored Link Online degrees - find the right program to advance your career. Www.nextag.com
This isn't an NVP - its a list of NVPs Try list<V> sl; ... ar & BOOST_SERIALIZATION_NVP(sl); Robert Ramey michael mathews wrote:
Well I thought it should and I suspect that I am doing something silly but this is basically the class I have:
template <class N, class V> class test { friend class boost::serialization::access; list< boost::serialization::nvp<V> > sl; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(sl);
}
public: { ... } and it gets caught in the compile trap
template<class T> void save_override(T & t, BOOST_PFTO int) { // If your program fails to compile here, its most likely due to // not specifying an nvp wrapper around the variable to // be serialized. BOOST_STATIC_ASSERT(0 == sizeof(T)); } I thought that a nvp should be serializable automatically. Ideas? Thanks in advance michael
--- Robert Ramey <ramey@rrsd.com> wrote:
Doesn't it already do this?
Robert Ramey
michael mathews wrote:
Hi All,
I have a class that contains a list of name value pairs . I would like the serialization to use the names for the tag names in the xml archive. Is there a way to do this or do I need to write my own custom archive?
Thanks in advance
Michael
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________________________________________ Sponsored Link
Online degrees - find the right program to advance your career. Www.nextag.com
Correct I have a list of NVP's. But that is what I need. The idea is to have a list of name value pairs where the name is used to generate the tag and the value is the value to save. Isn't that how a NVP gets serialized to an xml archive? I have also tried list< boost::serialization::nvp<V> > sl; ... ar & sl; ... which I thought would work since the elements of the list are NVP's It also gets caught in the trap. michael --- Robert Ramey <ramey@rrsd.com> wrote:
This isn't an NVP - its a list of NVPs
Try list<V> sl; ... ar & BOOST_SERIALIZATION_NVP(sl);
Robert Ramey
michael mathews wrote:
Well I thought it should and I suspect that I am doing something silly but this is basically the class I have:
template <class N, class V> class test { friend class boost::serialization::access; list< boost::serialization::nvp<V> > sl; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(sl);
}
public: { ... } and it gets caught in the compile trap
template<class T> void save_override(T & t, BOOST_PFTO int) { // If your program fails to compile here, its most likely due to // not specifying an nvp wrapper around the variable to // be serialized. BOOST_STATIC_ASSERT(0 == sizeof(T)); } I thought that a nvp should be serializable automatically. Ideas? Thanks in advance michael
--- Robert Ramey <ramey@rrsd.com> wrote:
Doesn't it already do this?
Robert Ramey
michael mathews wrote:
Hi All,
I have a class that contains a list of name value pairs . I would like the serialization to use the names for the tag names in the xml archive. Is there a way to do this or do I need to write my own custom archive?
Thanks in advance
Michael
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________________________________________
Sponsored Link
Online degrees - find the right program to advance your career. Www.nextag.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________________________________________ Sponsored Link Don't quit your job - take classes online www.Classesusa.com
and xml_archive requires that ALL elements have a name. So
list< boost::serialization::nvp<V> > sl; ... ar & sl; ...
would have to be changed to:
list< boost::serialization::nvp<V> > sl; ... ar & BOOST_SERIALIZATION_NVP(sl); ...
which would be OK... But there is one thing you might not like. You'll see an extra level tags added by the standard serialization of std::list. These tags have the name "item" - because at that level, there is not way to know what name they should have. I'm guessing this would bother you. In that case you should use your own enhanced version of serialization of list items. The first thing I would try would be to make template<class std::list<boost::nvp<T> > > void serialize(boost::archive::xml_archive &ar, std::list<boost::nvp<T> const unsigned int version){ copy the standard implementation eliminating the NVP("item"...) } This would be more specialized than the one based on std::list<T> and hence would take precedence. Assuming this gives you what you want, you could then make versions for all the different collections. And a separate set for xml_warchive. Slightly tedious but a simple process which would solve the problem for eternity. Just included all the above specializations in my_xml_archive.hpp along with inclusion of the standard xml_archive.hpp Robert Ramey michael mathews wrote:
Correct I have a list of NVP's. But that is what I need. The idea is to have a list of name value pairs where the name is used to generate the tag and the value is the value to save. Isn't that how a NVP gets serialized to an xml archive?
I have also tried list< boost::serialization::nvp<V> > sl; ... ar & sl; ... which I thought would work since the elements of the list are NVP's It also gets caught in the trap.
michael
--- Robert Ramey <ramey@rrsd.com> wrote:
This isn't an NVP - its a list of NVPs
Try list<V> sl; ... ar & BOOST_SERIALIZATION_NVP(sl);
Robert Ramey
michael mathews wrote:
Well I thought it should and I suspect that I am doing something silly but this is basically the class I have:
template <class N, class V> class test { friend class boost::serialization::access; list< boost::serialization::nvp<V> > sl; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(sl);
}
public: { ... } and it gets caught in the compile trap
template<class T> void save_override(T & t, BOOST_PFTO int) { // If your program fails to compile here, its most likely due to // not specifying an nvp wrapper around the variable to // be serialized. BOOST_STATIC_ASSERT(0 == sizeof(T)); } I thought that a nvp should be serializable automatically. Ideas? Thanks in advance michael
--- Robert Ramey <ramey@rrsd.com> wrote:
Doesn't it already do this?
Robert Ramey
michael mathews wrote:
Hi All,
I have a class that contains a list of name value pairs . I would like the serialization to use the names for the tag names in the xml archive. Is there a way to do this or do I need to write my own custom archive?
Thanks in advance
Michael
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________________________________________
Sponsored Link
Online degrees - find the right program to advance your career. Www.nextag.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________________________________________ Sponsored Link
Don't quit your job - take classes online www.Classesusa.com
Robert, Would this be the correct template to modify? I cannot find the function you mention anywhere. Of course I could be looking in the wrong places. ////////////////////////////////////////////////////////////////////// // implementation of serialization for STL containers // template<class Archive, class Container> inline void save_collection(Archive & ar, const Container &s) { // record number of elements unsigned int count = s.size(); ar << make_nvp("count", const_cast<const unsigned int &>(count)); BOOST_DEDUCED_TYPENAME Container::const_iterator it = s.begin(); while(count-- > 0){ //if(0 == (ar.get_flags() & boost::archive::no_object_creation)) // note borland emits a no-op without the explicit namespace boost::serialization::save_construct_data_adl(ar, &(*it), 0U); ar << boost::serialization::make_nvp("item", *it++); } } One more question. The examples say that you should be able to serialize a list like this: #include <boost/serialization/list.hpp> class bus_route { friend class boost::serialization::access; std::list<bus_stop *> stops; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & stops; } public: bus_route(){} }; Does this just work because it is a list of pointers? Thanks for your help michael --- Robert Ramey <ramey@rrsd.com> wrote:
and xml_archive requires that ALL elements have a name. So
list< boost::serialization::nvp<V> > sl; ... ar & sl; ...
would have to be changed to:
list< boost::serialization::nvp<V> > sl; ... ar & BOOST_SERIALIZATION_NVP(sl); ...
which would be OK... But there is one thing you might not like.
You'll see an extra level tags added by the standard serialization of std::list. These tags have the name "item" - because at that level, there is not way to know what name they should have.
I'm guessing this would bother you. In that case you should use your own enhanced version of serialization of list items.
The first thing I would try would be to make
template<class std::list<boost::nvp<T> > > void serialize(boost::archive::xml_archive &ar, std::list<boost::nvp<T> const unsigned int version){ copy the standard implementation eliminating the NVP("item"...) }
This would be more specialized than the one based on std::list<T> and hence would take precedence.
Assuming this gives you what you want, you could then make versions for all the different collections. And a separate set for xml_warchive. Slightly tedious but a simple process which would solve the problem for eternity.
Just included all the above specializations in my_xml_archive.hpp along with inclusion of the standard xml_archive.hpp
Robert Ramey
Correct I have a list of NVP's. But that is what I need. The idea is to have a list of name value pairs where the name is used to generate the tag and the value is the value to save. Isn't that how a NVP gets serialized to an xml archive?
I have also tried list< boost::serialization::nvp<V> > sl; ... ar & sl; ... which I thought would work since the elements of the list are NVP's It also gets caught in the trap.
michael
--- Robert Ramey <ramey@rrsd.com> wrote:
This isn't an NVP - its a list of NVPs
Try list<V> sl; ... ar & BOOST_SERIALIZATION_NVP(sl);
Robert Ramey
michael mathews wrote:
Well I thought it should and I suspect that I am doing something silly but this is basically the class I have:
template <class N, class V> class test { friend class boost::serialization::access; list< boost::serialization::nvp<V> > sl; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(sl);
}
public: { ... } and it gets caught in the compile trap
template<class T> void save_override(T & t, BOOST_PFTO int) { // If your program fails to compile here, its most likely due to // not specifying an nvp wrapper around the variable to // be serialized. BOOST_STATIC_ASSERT(0 == sizeof(T)); } I thought that a nvp should be serializable automatically. Ideas? Thanks in advance michael
--- Robert Ramey <ramey@rrsd.com> wrote:
Doesn't it already do this?
Robert Ramey
michael mathews wrote:
Hi All,
I have a class that contains a list of name value pairs . I would like the serialization to use the names for the tag names in
michael mathews wrote: the
xml archive. Is there a way to do this or do I need to write my own custom archive?
Thanks in advance
Michael
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________________________________________
Sponsored Link
Online degrees - find the right program to advance your career. Www.nextag.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________________________________________
Sponsored Link
Don't quit your job - take classes online www.Classesusa.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
____________________________________________________________________________________ The all-new Yahoo! Mail beta Fire up a more powerful email and get things done faster. http://new.mail.yahoo.com
Here is a little program which shows how you re-implement serialization of std::list<nvp<... to your taste. The code would only apply to these kinds of lists as only in this case would the template arguments match. I've included different versions depending upon whether you want to apply the change to all archives or just xml archives. I've compiled it on VC 7.1. To actually run it, it would have to be enhanced somewhat to include serialization of the item count and maybe some other functionality of the current default implementation. Robert Ramey #include <list> #include <fstream> #include <boost/serialization/nvp.hpp> #include <boost/serialization/utility.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> namespace boost { namespace serialization { // replace default serialization for lists of nvp in ALL archives template<class Archive, class U, class Allocator> inline void serialize( Archive & ar, std::list<boost::serialization::nvp<U>, Allocator> & t, const unsigned int version ){ // r std::list<boost::serialization::nvp<U>, Allocator>::iterator itr = t.begin(); while(itr != t.end()){ ar & *itr; itr++; } } // OR // replace default serialization for lists of nvp only in xml_archives template<class U, class Allocator> inline void serialize( boost::archive::xml_oarchive & ar, std::list<boost::serialization::nvp<U>, Allocator> & t, const unsigned int version ){ std::list<boost::serialization::nvp<U>, Allocator>::iterator itr = t.begin(); while(itr != t.end()){ ar & *itr; itr++; } } template<class U, class Allocator> inline void serialize( boost::archive::xml_iarchive & ar, std::list<boost::serialization::nvp<U>, Allocator> & t, const unsigned int version ){ // r std::list<boost::serialization::nvp<U>, Allocator>::iterator itr = t.begin(); while(itr != t.end()){ ar & *itr; itr++; } } } // serialization } // boost main(int arg, char *argv[]){ std::ofstream os("test"); boost::archive::xml_oarchive oa(os); const std::list<boost::serialization::nvp<int> > l; oa << BOOST_SERIALIZATION_NVP(l); }
participants (2)
-
michael mathews
-
Robert Ramey