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

QJsonObject::value()::toVariant()::toJsonArray() doesn't work for arrays

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.6.2
    • Core: Other
    • None

    Description

      But QJsonObject::value()::toArray() does.

      The QVariant returned by QJsonValue::toVariant() seems to have the wrong variant type internally (QMetaType::QVariantList instead of QMetaType::QJsonArray).

      Code to reproduce:

       

      #include <QJsonArray>
      #include <QJsonObject>
      #include <QJsonDocument>
      #include <QVariant>
      
      int main()
      {
          QByteArray json = R"(
      {
       "a": {
              "b": [
                  {
                      "id": "item1"
                  },
                  {
                      "id": "item2"
                  }
              ]
          }
      }
      )";
      
          QJsonDocument doc = QJsonDocument::fromJson(json);
          auto root = doc.object();
          auto a = root.value("a").toObject();
          auto bAsValue = a.value("b");
          auto bAsVariant = bAsValue.toVariant();
          auto bAsArray = bAsValue.toArray();
          auto bAsArrayFromVariant = bAsVariant.toJsonArray();
      
          return bAsArray.count() == bAsArrayFromVariant.count() ? 0 : -1;
      }

       

      bAsArrayFromVariant is invalid above.

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            hernan.c.martinez Hernan da Cunha Martinez
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes