The XML serialization is fully functional! We can now switch to the last type of serialization covered in this chapter.
Binary serialization is easier because Qt provides a direct way to do it. Create a BinarySerializer class that inherits from Serializer. The header is common; we only have the override functions save() and load(). Here is the implementation of the save() function:
void BinarySerializer::save(const Serializable& serializable, const QString& filepath, const QString& /*rootName*/) { QFile file(filepath); file.open(QFile::WriteOnly); QDataStream dataStream(&file); dataStream << serializable.toVariant(); file.close(); }
We hope you recognized the QDataStream class used in Chapter 10, Need IPC? Get Your Minions to Work. This time, we use this class to serialize binary data in...