QtJsonSerializer icon indicating copy to clipboard operation
QtJsonSerializer copied to clipboard

Q_GADGET and QVariantList

Open arietto opened this issue 5 years ago • 1 comments

Hello! I am trying to serialize Q_GADGET struct (My_gadget). It contains QVariantList children. These children are QVariant values which encapsulate My_gadget. The corresponding property is not serialized (it's a pity). If i change to QVariant child, then it fails too. What is the workaround? In real project QVariantList will contain other Q_GADGETs too.

P.S. AFAIK, CBOR is used internally in this serializer even if I need JSON. Q_GADGET struct fails to convert to QCBorValue via QVariant wrapper, because QVariant::toString() returns empty string.

arietto avatar Apr 28 '20 10:04 arietto

When I have embedded QVariant child, I put some dummy conversion function, then serialization succeeds (surely, with dummy value instead of actual data).

QString Gadget2QString(const Gadget& gadget)
{
return "dummy";
}
//just for example
std::function<QString(const Gadget&)> f = &Gadget2QString;
QMetaType::registerConverter<Gadget, QString>(f);

So, I suppose that CBOR limitation (QVariant ->CBOR conversion fails for custom types) gives headache.

arietto avatar Apr 28 '20 12:04 arietto