Skycoder42 / QtJsonSerializer

A library to perform generic seralization and deserialization of QObjects from and to JSON and CBOR
https://skycoder42.github.io/QtJsonSerializer/
BSD 3-Clause "New" or "Revised" License
143 stars 70 forks source link

Q_GADGET and QVariantList #35

Open arietto opened 4 years ago

arietto commented 4 years ago

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 commented 4 years ago

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.