[Interest] QJsonDocument::fromVariant failing with list

Jason H jhihn at gmx.com
Wed Jun 9 19:21:28 CEST 2021

> Sent: Wednesday, June 09, 2021 at 10:48 AM
> From: "Thiago Macieira" <thiago.macieira at intel.com>
> To: interest at qt-project.org
> Subject: Re: [Interest] QJsonDocument::fromVariant failing with list
> On Wednesday, 9 June 2021 07:28:08 PDT Jason H wrote:
> > One hack I would workfor me is a new class, QVariantMapList, (List of
> > QVariantMaps) which is really all I need at the moment, unless there'd be a
> > need for QVariantListList too? (That's a terrible name though)
> >
> > Ultimately, I just want to express JSON as closely as I can in C++.
> > Ideally:
> > QVariantMap map {
> > {"err", QVariant()},
> > {"letters", QVariantMapList {
> > {{"a",1}},
> > {{"b",2}},
> > {{"c",3}}
> > }}
> > };
> Stop using QVariantMapList. You want a QVariantMap.

I think you meant QVariantList?:
 QVariantMap map {
   {"err", QVariant()},
   {"letters", QVariantList {
     QVariantMap {{"a",1}},
     QVariantMap {{"b",2}},
     QVariantMap {{"c",3}}

I had hoped that by using QList<QVariantMap> that I could omit the 3 QVariantMaps above.

If you didn't mean QVariantList, then I have confused you, what I wanted was
(JSON) [{"a":1}, {"b":2}, {"c":3}] not {"a":1, "b":2, "c":3}
Perhaps I confused you by using sequential letters but [{"a":1}, {"a":2}, {"a":3}] could be just as valid.

But anyway, it seems my mistake was thinking that QVariant hold a QList<QVariantMap>, the best thing possible is the more general case of QVariantList, and this can be a heterogeneous collection of QVariants. Neither compiler nor Qt is capable of enforcing homogeneity as a QList of QVariantMaps AND having that collection being a QVariant itself, in a declaration. It would have to be built at runtime.

Did I get that right?

More information about the Interest mailing list