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
?
According to this you should be able to convert any QVariant to QJSonObject
Now your question become : how to transform your Foo class to QVariant ?
I suggest you to add toQVariant and fromQVariant methods to Foo class to encapsulate this behavior ; the most convenient QVariant class is actually QVariantMap that you can use like this to serialize your Foo object