Electively serialize to/from XML or binary in C++
Defining a shared interface
Given these prerequisites, I came up with the following interface for the Serializer and Deserializer class.
Let’s have a look at the methods available.
beginAttributestarts a logical group of data. It’s required from the XML point of view where it will introduce a new enclosing tag. Therefore, this would be the very first method you would call in order to serialize something. Technically speaking, the XML serializer we will implement later appends a child element. Because no such information is required for binary serialization, the binary serializer implementation of this method would be empty.writeNumberOfElementswrites the number of child elements, if any. Now it’s the other way round: the XML serializer won’t write anything since the number of children is implicitly known by the number of child elements in the XML tree, contrary the the binary format, where we need to know how many child elements will follow.writeParameterwrites an actual item of information, like astd::stringor anf32(which is atypedeffor afloat)or some application-dependent types likeirr::video::SColorf(which is the structure which holds color information in the Irrlicht engine, in case you were curious).endAttributefinally closes a logical group of data. As withbeginAttribute, the underlying implementation for XML serialization closes the enclosing tag while the binary one does nothing.
If you like this article, please share a slice of your pie and click here:


