Protocol Buffers are not always an option and you just have to serialize your data to XML. Then what? Luckily there is a serialization library available from Boost and it makes that pretty easy for us. You don’t even have to modify your existing data structures to write them out as XML: the library is non-invasive.
Let’s say I want to serialize a list of people to a file, and read it back later. My data structures would be defined like this:
1 2 3 4 5 6 7 8 |
struct person { string name; int dob; string email; }; using people = vector<person>; |
And once a vector of person’s is serialized I want it to look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<people class_id="0" tracking_level="0" version="1"> <count>2</count> <item_version>1</item_version> <item class_id="1" tracking_level="0" version="1"> <person.name>Martin Vorbrodt</person.name> <person.dob>19800830</person.dob> <person.email>martin@vorbrodt.blog</person.email> </item> <item> <person.name>Dorota Vorbrodt</person.name> <person.dob>19810127</person.dob> <person.email>dorota@vorbrodt.blog</person.email> </item> </people> |
Easy! First you must define a generic serialize function for your data structure, then you instantiate an XML output archive with an ofstream object and pass it the data. Reading is done by instantiating an XML input archive with an ifstream object and loading the data into a variable. Like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#include <iostream> #include <fstream> #include <string> #include <vector> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/serialization/vector.hpp> using namespace std; using namespace boost::archive; using namespace boost::serialization; struct person { string name; int dob; string email; }; using people = vector<person>; BOOST_CLASS_VERSION(person, 1); BOOST_CLASS_VERSION(people, 1); template<typename Archive> void serialize(Archive& ar, person& person, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(person.name); ar & BOOST_SERIALIZATION_NVP(person.dob); ar & BOOST_SERIALIZATION_NVP(person.email); } int main(int argc, char** argv) { people us{me, her}; { ofstream ofs("data.xml"); xml_oarchive oa(ofs, boost::archive::no_header); oa << make_nvp("people", us); } people us_too{}; { ifstream ifs("data.xml"); xml_iarchive ia(ifs, boost::archive::no_header); ia >> make_nvp("people", us_too); } for(auto& p : us_too) { cout << "Name : " << p.name << endl; cout << "DOB : " << p.dob << endl; cout << "EMail : " << p.email << endl << endl; } return 1; } |
Name : Martin Vorbrodt
Program output.
DOB : 19800830
EMail : [email protected]
Name : Dorota Vorbrodt
DOB : 19810127
EMail : [email protected]
The library has built in support for STL containers. It can also write the data in many output formats, not just XML. Luckily you only have to define one serialization function per data type and it will work with all input and output archives. Heck, you could even define a serialization function for your protocol buffers data types 😉