Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Empty QJsonDocument from file
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Empty QJsonDocument from file

Scheduled Pinned Locked Moved General and Desktop
28 Posts 4 Posters 17.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ningu
    wrote on last edited by
    #1

    Hi,

    I'm trying to parse an appSettigns.json file that holds some application settings, but when I set the json document with QJsonDocument::fromJson(file.readAll()) the json document stays empty.

    Here's the code:

    jsonmanager.cpp
    @
    #include "jsonmanager.h"

    #include <QFile>
    #include <QDebug>
    #include <QJsonDocument>
    #include <QJsonObject>

    JsonManager::JsonManager(QWidget *parent)
    {
    QFile file(":/config/appSettings.json");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    qDebug() << "Could not open file";
    qDebug() << file.errorString();
    return;
    }

    QJsonDocument   jsonDoc;
    jsonDoc = QJsonDocument::fromJson(file.readAll());
    if (!jsonDoc.isNull()) {
        qDebug() << "Could not read data";
    }
    
    QJsonObject jsonObj = jsonDoc.object();
    QVariantMap jsonMap = jsonObj.toVariantMap();
    qDebug() << jsonMap.size();  // outputs 0
    // and so does jsonDoc.array().size();
    
    file.close();
    

    }
    @

    appSettings.json
    @{
    "osc_port":57120,
    "server": {
    "path": "/path/to/executable",
    "ip": "127.0.0.1",
    "port": 54321
    },
    "data_path": "/path/to/data/folder",
    "frame_rate": 25,
    "vertical_sync": true,
    "threshold": 64,
    "camera": {
    "device_id": 0,
    "width": 640,
    "height": 480,
    "rotate_180": false,
    "use_uvc": false
    },
    "background_color": {
    "red": 64,
    "green": 64,
    "blue": 64,
    "alpha": 255,
    },
    "tool_highlight_color": {
    "red": 240,
    "green": 64,
    "blue": 64,
    "alpha": 128,
    }
    }
    @

    I also checked the array in the QJsonDocument but it's empty, too. Any hints on what I'm doing wrong?

    Thanks!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kurt.pattyn
      wrote on last edited by
      #2

      In the background_color object, there is a superfluous comma, as well as in tool_highlight_color. Maybe that is your problem.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        ningu
        wrote on last edited by
        #3

        Thanks for the quick answer.

        Unfortunately, even though you are right, removing the superfluous comma did not solve the problem. The QDomDocument is still empty.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          ningu
          wrote on last edited by
          #4

          I think the error is in the loading of the file.
          @qDebug() << file.bytesAvailable();@

          outputs 0, although it seems to load fine (no error output).

          1 Reply Last reply
          0
          • N Offline
            N Offline
            ningu
            wrote on last edited by
            #5

            If I load the data from the file to QDomDocument object it seems to work fine. I very puzzled.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Just to be on the safe side, did you properly put your json file in the resources (qrc file) ?

              You keep talking about QDomDocument, is it a typo ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                ningu
                wrote on last edited by
                #7

                Yes, the file is properly put in my qrc file.

                And yes, sorry, when I said

                bq. Unfortunately, even though you are right, removing the superfluous comma did not solve the problem. The QDomDocument is still empty.

                I meant the QJsonDocument is still empty.

                But later on I tried to load the data into a QDomDocument object just to try and it seems to work fine.
                @
                QDomDocument *domDocument;
                if (!domDocument->setContent(&file)) {
                qDebug() << "Could not set DOM";
                return;
                }

                qDebug() << domDocument->toString();@

                prints the file contents to the console.

                Sorry for the confusion I created with the typo.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Ok, then what does

                  @qDebug() << file.readAll()@

                  return ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    ningu
                    wrote on last edited by
                    #9

                    It returns
                    @""@

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Then it seems that your file is empty

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        ningu
                        wrote on last edited by
                        #11

                        That's what I thought, but
                        @qDebug() << QString::fromUtf8(file.readAll())@
                        returns the file contents.

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          ningu
                          wrote on last edited by
                          #12

                          Might it be an encoding issue?

                          1 Reply Last reply
                          0
                          • N Offline
                            N Offline
                            ningu
                            wrote on last edited by
                            #13

                            Sorry, that was not completely right:
                            @QString jsonString = QString::fromUtf8(file.readAll());
                            qDebug() << jsonString;
                            @
                            returns the file contents, while
                            @qDebug() << QString::fromUtf8(file.readAll())@
                            returns
                            @""@

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Strange, QJsonDocument should handle it...
                              Did you check what error you got when using fromJson ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • N Offline
                                N Offline
                                ningu
                                wrote on last edited by
                                #15

                                No, I didn't. How could I do that?

                                1 Reply Last reply
                                0
                                • N Offline
                                  N Offline
                                  ningu
                                  wrote on last edited by
                                  #16

                                  Ok, I got it. A friend of mine pointed me out that I was looking for the array in the QJsonDocument, and that I should probably be looking for the object, which worked.

                                  The only thing that doesn't work right is that
                                  @QString jsonString = QString::fromUtf8(file.readAll());

                                  QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());@
                                  works, but
                                  @QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll());@
                                  doesn't.

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    Look at the second parameter of "fromJson":http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html#fromJson

                                    From there you can get the error

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • N Offline
                                      N Offline
                                      ningu
                                      wrote on last edited by
                                      #18

                                      It's an 'Illegal value' error. But I get the same error even with an empty file.

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        Could it be your json file encoding that is problematic ?

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • N Offline
                                          N Offline
                                          ningu
                                          wrote on last edited by
                                          #20

                                          That might be it. Is there a way to make sure the file is UTF8? Or set it to UTF8?

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved