Serializing objects in binary format
The XML serialization is fully functional! We can now switch to the last type of serialization covered in this chapter.
The binary serialization is easier because Qt provides a direct way to do it. Please create a BinarySerializer
class that inherits from Serializer
. The header is common, we have only 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 a destination QFile
. A QDataStream
class accepts a QVariant...