Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-94371

Handle QList<QVariantMap> better

    XMLWordPrintable

    Details

    • Type: Suggestion
    • Status: Closed
    • Priority: Not Evaluated
    • Resolution: Invalid
    • Affects Version/s: 5.15.0
    • Fix Version/s: None
    • Labels:
      None
    • Platform/s:
      All

      Description

      I'm having trouble with QJsonDocument::fromVariant() is not handling arrays as expected:

      QVariantMap map;
      QVariant list;
      
      QList<QVariantMap> letters { 
      {{"a",1}}, 
      {{"b",2}}, 
      {{"c",3}}
      };
      
      list.setValue(letters);
      map["err"] = QVariant(); // null
      map["letters"] = list;
      
      qDebug() << QJsonDocument::fromVariant(map).toJson(QJsonDocument::Compact);
      

      Output:

      {"err":null,"letters":null}

      I'm expecting:

      {"err":null,"letters":[

      {"a": 1}

      ,{"b": 2},{"c": 3}]}

      However using below, it works:

      QVariantList letters {
                              QVariantMap {{"a",1}},
                              QVariantMap {{"b",2}},
                              QVariantMap {{"c",3}}
       };
      

      Ideally, What I'd like to be able to write is:

      	QVariantMap map {
      		{"err", QVariant()},
      		{"letters", QList<QVariantMap> {
      				{{"a",1}},
      				{{"b",2}},
      				{{"c",3}}
      		}}
      	};
      

      Which doesn't include repeated type information for each item (because it is specified in the template) But that won't compile, so I attempted a hack:

      	QVariant list;
      	list.setValue(letters);
      
      	QVariantMap map {
      		{"err", QVariant()},
      		{"letters", list}
      	};
      

      In which case it silently fails to convert.

      While not every QList template instantiation can be known, the common ones should be supported. This becomes important when expressing structures that end up as CBOR or JSON output, or in-memory databases.

      Someone on the mailing list said:

      I guess our system could detect QList<QVariant> as QVariantList and
      QList<QString> as QStringList. They should be binary identical anyway.

      I don't know if there is a need for another class QVariantMapList, to hold a list of QVariantMaps, but once should be able to:

      	QVariantMap map {
      		{"err", QVariant()},
      		{"letters", QVariantMapList {
      				{{"a",1}},
      				{{"b",2}},
      				{{"c",3}}
      		}}
      	};
      

      Which would be the next best thing if it can't be supported though automatic detection. But once this is in place, expressing these structures would be almost as easy as specifying them in JS/JSON.

       

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            thiago Thiago Macieira
            Reporter:
            scorp1us Jason Hihn
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes