I have a QVariant which can contain a double, a QString, a Foo object, or anything.
I would like to serialize my QVariant without knowing what it contains.
I am trying to do the serialization like this:
QJsonObject jsonObject;
jsonObject["myObject"] = _variant.toJsonObject();
What function must I overload in Foo? Must I use Q_PROPERTY?
I don't believe that you can do what you want directly. There is no way to convert an arbitrary object like
Footo aQJsonObjectexcept by manually reading and writing the corresponding fields of theFooobject. SinceQVarianthas no way of knowing that your object supports that functionality, you won't be able to useQVariantdirectly. It may help you to follow the example provided by Qt for doing serialization:http://doc.qt.io/qt-5/qtcore-json-savegame-example.html
Notice they have manual
readandwritemethods for theGameobject.