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

Data back from QVariant to QJsonDocument/QJsonObject does not work.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: 5.9.1
    • Fix Version/s: None
    • Component/s: Core: I/O
    • Labels:
      None
    • Platform/s:
      Linux/Other display system

      Description

      Data copied to QVariant from QJsonDocument, unable to copy back to QJsonDocument/QJsonObject. However, in 5.11 conversion is possible.

       

      Facing issues in attaching a file, hence copying the entire code

       

      #include <QCoreApplication>
      #include <QString>
      #include <QJsonObject>
      #include <QBuffer>
      #include <QSettings>
      #include <QJsonArray>
      #include <QJsonDocument>
      class User
      {
      public:
       User() {}
       User(const QString& ruser_name, const int& ruser_age)
       {
       user_name = ruser_name;
       user_age = ruser_age;
       }
      
      QString get_user_name() const \{ return user_name; }
      
      void write(QJsonObject &json) const
       {
       json["user_name"] = user_name;
       json["user_age"] = user_age;
       }
      
      void read(const QJsonObject &json)
       {
       if (json.contains("user_name") && json["user_name"].isString())
       user_name = json["user_name"].toString();
      
      if (json.contains("user_age"))
       user_age = json["user_name"].toInt();
       }
      private:
       QString user_name;
       int user_age;
      };
      
      int main(int argc, char *argv[])
      {
       QCoreApplication a(argc, argv);
      
      QMap<QString, User> user_list;
       user_list["Tom"] = User("Tom",10);
       user_list["Henry"] = User("Henry",20);
      
      QJsonArray userListArray;
      
      QMapIterator<QString,User> iterator(user_list);
      
      while (iterator.hasNext())
       {
       iterator.next();
       QJsonObject userJsonObject;
       User userObj = iterator.value();
       userObj.write(userJsonObject);
       userListArray.append(userJsonObject);
       }
      
      QJsonObject user_list_object_wrote;
       user_list_object_wrote["user_list"] = userListArray;
      
      QJsonDocument user_list_doc_wrote(user_list_object_wrote);
      
      QVariant user_list_variant = user_list_doc_wrote.toVariant();
      
      // Now read the data back into QJsonDocument/QJsonObject
       QJsonDocument user_list_doc_read = user_list_variant.toJsonDocument();
      
      QJsonObject user_list_object_read = user_list_variant.toJsonObject();
      
      return a.exec();
      }
      

       

        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:
              joshi Prasanna Joshi
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:

                Gerrit Reviews

                There are no open Gerrit changes