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. Fetching current json adding to it and storing it back in the file

Fetching current json adding to it and storing it back in the file

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 2 Posters 1.2k 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.
  • K Kris Revi

    @sierdzio wait QFile::ReadOnly & QFile::Text i thought it was QIODevice::ReadOnly and QIODevice::Text

    sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #4

    @Kris-Revi said in Fetching current json adding to it and storing it back in the file:

    @sierdzio wait QFile::ReadOnly & QFile::Text i thought it was QIODevice::ReadOnly and QIODevice::Text

    Doesn't matter it's the same enum and the values are the same. I prefer using QFile because it's shorter and more consistent.

    (Z(:^

    K 1 Reply Last reply
    1
    • sierdzioS sierdzio

      @Kris-Revi said in Fetching current json adding to it and storing it back in the file:

      @sierdzio wait QFile::ReadOnly & QFile::Text i thought it was QIODevice::ReadOnly and QIODevice::Text

      Doesn't matter it's the same enum and the values are the same. I prefer using QFile because it's shorter and more consistent.

      K Offline
      K Offline
      Kris Revi
      wrote on last edited by
      #5

      @sierdzio said in Fetching current json adding to it and storing it back in the file:

      QJsonArray devices = jsonOrg.array();

      Add a log here (or look it up in debugging session) to see if there is actually any data read from the file.

      hmmm the print out comes up empty :S infact it does not print at all

              for(int i = 0; i < devices.size(); i++)
                  qDebug() << "Json Original : " << devices[i];
      
      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #6

        Then at least we know the problem lies in file reading, not in saving.

        Check what readAll() actually returns. Plus add error checking to JSON document (add and check QJsonParseError to your fromJson() call).

        (Z(:^

        K 1 Reply Last reply
        1
        • sierdzioS sierdzio

          Then at least we know the problem lies in file reading, not in saving.

          Check what readAll() actually returns. Plus add error checking to JSON document (add and check QJsonParseError to your fromJson() call).

          K Offline
          K Offline
          Kris Revi
          wrote on last edited by Kris Revi
          #7

          @sierdzio

          qDebug() << "File Readl All : " << deviceListFile.readAll();
          qDebug() << "Error : " << error->errorString() << error->offset;
          
          File Readl All :  ""
          Error :  "no error occurred" 0
          

          it just comes up empty :S

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #8

            OK, case clear. The file is empty :-) Verify the path, and open the file in system to see if it's true.

            (Z(:^

            K 1 Reply Last reply
            0
            • sierdzioS sierdzio

              OK, case clear. The file is empty :-) Verify the path, and open the file in system to see if it's true.

              K Offline
              K Offline
              Kris Revi
              wrote on last edited by
              #9

              @sierdzio i JUST did :| the file exists and has stuff in it :S

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #10

                @Kris-Revi said in Fetching current json adding to it and storing it back in the file:

                deviceListFile.readAll();

                Except: make sure you call readAll() ONCE - store it in a variable, then print with qDebug(), then pass to the logic below it.

                If you call it twice, the second call will likely be empty.

                (Z(:^

                K 1 Reply Last reply
                1
                • sierdzioS sierdzio

                  @Kris-Revi said in Fetching current json adding to it and storing it back in the file:

                  deviceListFile.readAll();

                  Except: make sure you call readAll() ONCE - store it in a variable, then print with qDebug(), then pass to the logic below it.

                  If you call it twice, the second call will likely be empty.

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on last edited by Kris Revi
                  #11

                  @sierdzio so by qDebug() << "File Readl All : " << jsonOrg; i got

                  File Readl All :  QJsonDocument({"Device":[{"IPA":"studiolights","Icon":"fa_bolt","Name":"Studio Lights","Status":"online","Type":"Strip","UVLights":"false"}]})
                  Error :  "no error occurred" 0
                  
                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #12

                    Of course it clears the file but that has no consequence - you are building a new JSON will all the data in it, and then save it. So it's good that at that point the file is empty.

                    (Z(:^

                    K 1 Reply Last reply
                    0
                    • sierdzioS sierdzio

                      Of course it clears the file but that has no consequence - you are building a new JSON will all the data in it, and then save it. So it's good that at that point the file is empty.

                      K Offline
                      K Offline
                      Kris Revi
                      wrote on last edited by
                      #13

                      @sierdzio so now the only part is this

                          if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
                          {
                              QJsonObject newDevice;
                      
                              newDevice.insert("IPA",      "newdevice");
                              newDevice.insert("Icon",     "fa_bolt");
                              newDevice.insert("Name",     "New Device");
                              newDevice.insert("Status",   "online");
                              newDevice.insert("Type",     "Strip");
                              newDevice.insert("UVLights", "false");
                      
                              QJsonArray devices = jsonOrg.array();
                      
                              devices.push_back(newDevice);
                      
                              QJsonDocument jsonDoc( devices );
                      
                              QJsonObject obj = jsonDoc.object();
                      
                              deviceListFile.write(jsonDoc.toJson());
                      
                              deviceListFile.close();
                              return true;
                          }
                      
                      1 Reply Last reply
                      0
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #14

                        Hey wait. The jsonOrg is not an array! You have a top-level object there called "Device". So that's why this code is failing for you.

                        auto top = jsonOrg.object();
                        auto devices = top.value("Device").toArray();
                        [...]
                        QJsonDocument jsonDoc( top );
                        

                        With that it should work.

                        (Z(:^

                        K 1 Reply Last reply
                        1
                        • sierdzioS sierdzio

                          Hey wait. The jsonOrg is not an array! You have a top-level object there called "Device". So that's why this code is failing for you.

                          auto top = jsonOrg.object();
                          auto devices = top.value("Device").toArray();
                          [...]
                          QJsonDocument jsonDoc( top );
                          

                          With that it should work.

                          K Offline
                          K Offline
                          Kris Revi
                          wrote on last edited by
                          #15

                          @sierdzio

                              if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
                              {
                                  QJsonObject newDevice; // projectDetails
                          
                                  newDevice.insert("IPA",      "newdevice");
                                  newDevice.insert("Icon",     "fa_bolt");
                                  newDevice.insert("Name",     "New Device");
                                  newDevice.insert("Status",   "online");
                                  newDevice.insert("Type",     "Strip");
                                  newDevice.insert("UVLights", "false");
                          
                                  auto top = jsonOrg.object();
                          
                                  QJsonArray devices = top.value("Device").toArray();
                          
                                  devices.push_back(newDevice);
                          
                                  QJsonDocument jsonDoc( top );
                          
                                  deviceListFile.write(jsonDoc.toJson());
                          
                                  deviceListFile.close();
                                  return true;
                              }
                          

                          does nothing! like the file remains as it was :/ no new entry was added when running the code nor any error!

                          1 Reply Last reply
                          0
                          • sierdzioS Offline
                            sierdzioS Offline
                            sierdzio
                            Moderators
                            wrote on last edited by
                            #16

                            Ah sorry, it's working on a copy of the array, obviously. You need to also add devices back to top:

                            top.insert("Device", devices);
                            

                            (Z(:^

                            K 1 Reply Last reply
                            1
                            • sierdzioS sierdzio

                              Ah sorry, it's working on a copy of the array, obviously. You need to also add devices back to top:

                              top.insert("Device", devices);
                              
                              K Offline
                              K Offline
                              Kris Revi
                              wrote on last edited by
                              #17

                              @sierdzio there we go :)

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                Kris Revi
                                wrote on last edited by
                                #18

                                The complete answer :)

                                bool DeviceData::devicedataupdated()
                                {
                                    QFile deviceListFile(_JsonFile);
                                
                                    if( !deviceListFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
                                    {
                                        return false;
                                    }
                                
                                    QJsonDocument jsonOrg = QJsonDocument::fromJson( deviceListFile.readAll() );
                                
                                    deviceListFile.close();
                                
                                    if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
                                    {
                                        QJsonObject newDevice;
                                
                                        newDevice.insert( "IPA",      "newdevice" );
                                        newDevice.insert( "Icon",     "fa_bolt" );
                                        newDevice.insert( "Name",     "New Device" );
                                        newDevice.insert( "Status",   "online" );
                                        newDevice.insert( "Type",     "Strip" );
                                        newDevice.insert( "UVLights", "false" );
                                
                                        auto top = jsonOrg.object();
                                
                                        auto devices = top.value( "Device" ).toArray();
                                
                                        devices.push_back( newDevice );
                                
                                        top.insert("Device", devices);
                                
                                        QJsonDocument jsonDoc( top );
                                
                                        deviceListFile.write( jsonDoc.toJson() );
                                
                                        deviceListFile.close();
                                        return true;
                                    }
                                
                                    return false;
                                }
                                
                                1 Reply Last reply
                                2

                                • Login

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