Serialization of an std::list into XML without a NVP

Hi all, I'm working with the Serialization library and trying to output a list of objects into an XML file. The problem I'm having is that in serializing a std::list, the Serialization library requires that the list itself is given a NVP. The specification I'm working with requires a list of undefined length WITHOUT its own NVP. Is there any way to serialize a std::list into/out of an xml_oarchive/xml_iarchive? Any help would be much appreciated. Thanks, Devin Fallak

I'm not sure what the problem is with having an std::list have its own NVP but you can easily handle tihs yourself without using the definition of std::list serialization included in the package. class my_class { std::list<int> my_list; // instead of the following template<class Archive> void serialize(Archive &ar, const unsigned int version){ ar & BOOST_SERIALZATION_NVP(my_list) } // do the fillowing template<class Archive> void save(Archive &ar, const unsigned int version){ ar << make_nvp("size", my_list::size()); std::list::const_iterator litr = my_list.begin(); for(unsigned i = my_list::size(); --i; i > 0){ ar << make_nvp("list_element", *litr++); } } ... load... }; Robert Ramey Devin Fallak wrote:
Hi all,
I'm working with the Serialization library and trying to output a list of objects into an XML file. The problem I'm having is that in serializing a std::list, the Serialization library requires that the list itself is given a NVP. The specification I'm working with requires a list of undefined length WITHOUT its own NVP.
Is there any way to serialize a std::list into/out of an xml_oarchive/xml_iarchive?
Any help would be much appreciated.
Thanks,
Devin Fallak

Robert Ramey
... snip ...
template<class Archive> void save(Archive &ar, const unsigned int version){ ar << make_nvp("size", my_list::size()); std::list::const_iterator litr = my_list.begin(); for(unsigned i = my_list::size(); --i; i > 0){ ar << make_nvp("list_element", *litr++); } } ... load... };
Robert Ramey
... snip ...
Thanks for the quick reply. That approach was what I also had thought at first, but the length of the list is not necessarily a defined member in all the XML files, and they are often generated by another application without the size member. This is fine for saving but a problem for loading because the for loop has no way of knowing how many elements to load into the list. I've tried an implementation which loads objects until exceptions are caught, but this implementation crashes upon receiving a stream_error exception. Is it possible that after the archive reads an object and generates the exception, it doesn't 'rewind' to the position before the offending object? If so, is there a way to make it do this? Thanks for the help, Devin

Thanks for the quick reply. That approach was what I also had thought at first, but the length of the list is not necessarily a defined member in all the XML files, and they are often generated by another application without the size member.
In this case its extremely unlikely that they will be loadable by the serialization library at all. The serialization library includes support for std::list. Other list classes aren't included - you'll have to roll your own serialization for anything not included in the library.
This is fine for saving but a problem for loading because the for loop has no way of knowing how many elements to load into the list.
Then there will have to be SOME way to know when to stop loading the list items. perhaps special "end element".
I've tried an implementation which loads objects until exceptions are caught, but this implementation crashes upon receiving a stream_error exception.
Depending on an exception is an extremely bad idea. There is no way to know what kind of exception would occur. and how would you detect a "real" exception from one of these "special" ones.
Is it possible that after the archive reads an object and generates the exception, it doesn't 'rewind' to the position before the offending object? If so, is there a way to make it do this?
It doesn't "rewind". By the time you've caught the exception, the data stream has been read. Basically I think you have to think some more about whatyou really want to do. If you have your own list, that's ok. but you'll have to include information and/or functions on how to know how many elements to read. This is not really a serialization library question. The same issues would arise when reading any collection from a stream. Robert Ramey
participants (2)
-
Devin Fallak
-
Robert Ramey