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

json null value is not stringified correctly from QVariantMap

    XMLWordPrintable

Details

    • 4b63ab9a93, 3e5152be41

    Description

      Json "value:null" is converted to invalid QVariant when calling QJsonObject::toVariantMap(). When that variantmap is stringified in QML then null valued property is vanished. Can be reproduced by attached example.

      Expected output is:

      qml: {"x":null,"y":10}

      and output is:

      qml: {"y":10}

      Workaround is to replace invalid values with null QJsonValue:

          QString json = "{\"x\":null, \"y\": 10}";
          QJsonDocument jsonDoc = QJsonDocument::fromJson(json.toLatin1());
          QVariantMap map = jsonDoc.object().toVariantMap();
      
      // WORKAROUND:
          foreach(QString k, map.keys()) {
              QVariant v = map.value(k);
              if(!v.isValid())
                  map.insert(k,QJsonValue(QJsonValue::Null));
          }
      

      Attachments

        1. QTBUG-43077.zip
          3 kB
          Qt Support
        For Gerrit Dashboard: QTBUG-43077
        # Subject Branch Project Status CR V

        Activity

          People

            kandeler Christian Kandeler
            qtcomsupport Qt Support
            Votes:
            2 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes