Simple C++ serialization for a QObject property tree.
QObject <==> QVariantMap <==> {Json, XML, ...}
- Recursively serializes child objects.
- ONLY serializes properties (ignores other class members).
- Dynamic runtime creation of child objects during deserialization (requires factory).
Author: Marcel Goldschen-Ohm
Email: marcel.goldschen@gmail.com
License: MIT
Copyright (c) 2015 Marcel Goldschen-Ohm
For JSON serialization, also check out qjson.
Everything is in:
QtPropertySerializer.h
QtPropertySerializer.cpp
See CMakeLists.txt
for example build as a static library.
👉 This is most likely what you want: See test/CMakeLists.txt
for example build of an app that uses QtPropertySerializer. This build uses CMake to automatically download QtPropertySerializer files directly from this GitHub repository, builds QtPropertySerializer as a static library and links it to the app executable. This way you can use QtPropertySerializer in your project without downloading or managing the QtPropertySerializer repository manually.
QVariantMap (key, value) pairs are either:
- (property name, property value)
- (child object class name, child QVariantMap)
- (child object class name, QVariantList of QVariantMaps for multiple children of the same type)
See test.h/cpp
for complete example code.
#include "QtPropertySerializer.h"
jane
|-- john
|-- josephine
|-- spot
class Person : public QObject { ... };
class Pet : public QObject { ... };
Person jane;
Person *john = new Person;
Person *josephine = new Person;
Pet *spot = new Pet;
john->setParent(&jane);
josephine->setParent(&jane);
spot->setParent(josephine);
QVariantMap janePropertyTree = QtPropertySerializer::serialize(&jane);
- Access child QVariantMaps in parent QVariantMap by class name.
- Multiple children of the same type are placed in a QVariantList.
QVariantList janePersonList = janePropertyTree["Person"].toList();
QVariantMap johnPropertyTree = janePersonList[0].toMap();
QVariantMap josephinePropertyTree = janePersonList[1].toMap();
QVariantMap spotPropertyTree = josephinePropertyTree["Pet"].toMap();
- Access properties in QVariantMap by property name.
qDebug() << janePropertyTree["objectName"];
qDebug() << johnPropertyTree["objectName"];
qDebug() << josephinePropertyTree["objectName"];
qDebug() << spotPropertyTree["objectName"];
Deserialize into a pre-existing object tree.
QtPropertySerializer::deserialize(&jane, janePropertyTree);
Deserialize into an object without pre-existing child objects (requires runtime dynamic object creation using a factory).
QtPropertySerializer::ObjectFactory factory;
factory.registerCreator("Person", factory.defaultCreator<Person>);
factory.registerCreator("Pet", factory.defaultCreator<Pet>);
// factory.defaultCreator<T> is a static function taking
// no arguments and returning a QObject* for `new T()`.
// You are also free to use your own custom creator function here.
// Prior to deserialization, bizarroJane has no children.
Person bizarroJane;
// After deserialization, bizarroJane is identical to Jane
// with children John and Josephine, and Josephine's pet Spot.
QtPropertySerializer::deserialize(&bizarroJane, janePropertyTree, &factory);
QtPropertySerializer::writeJson(&jane, "jane.json");
QtPropertySerializer::readJson(&jane, "jane.json");
Person juniper;
QtPropertySerializer::readJson(&juniper, "jane.json", &factory);