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.

  • beginAttribute starts 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.
  • writeNumberOfElements writes 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.
  • writeParameter writes an actual item of information, like a std::string or an f32 (which is a typedef for a float)or some application-dependent types like irr::video::SColorf (which is the structure which holds color information in the Irrlicht engine, in case you were curious).
  • endAttribute finally closes a logical group of data. As with beginAttribute, 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:

Pages:

1 2 3 4 5 6 7 8 9


Write a Comment

Take a moment to comment and tell me what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!