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:
struct person
{
string name;
int dob;
string email;
};
using people = vector;
And once a
vectorof
person‘s is serialized I want it to look something like this:
2 1 Martin Vorbrodt 19800830 martin@vorbrodt.blog Dorota Vorbrodt 19810127 dorota@vorbrodt.blog
Easy! First you must define a generic serialize function for your data structure, then you instantiate an XML output archive with an
ofstreamobject and pass it the data. Reading is done by instantiating an XML input archive with an
ifstreamobject and loading the data into a variable. Like this:
#include#include #include #include #include #include #include using namespace std; using namespace boost::archive; using namespace boost::serialization; struct person { string name; int dob; string email; }; using people = vector ; BOOST_CLASS_VERSION(person, 1); BOOST_CLASS_VERSION(people, 1); template 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) { person me{"Martin Vorbrodt", 19800830, "martin@vorbrodt.blog"}; person her{"Dorota Vorbrodt", 19810127, "dorota@vorbrodt.blog"}; 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 : martin@vorbrodt.blog
Name : Dorota Vorbrodt
DOB Â : 19810127
EMail : dorota@vorbrodt.blog
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 ;)