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. QJsonObject parameters order
Qt 6.11 is out! See what's new in the release blog

QJsonObject parameters order

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 6 Posters 1.3k Views 2 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by ODБOï
    #1

    hi,
    i serialize an object with QJsonObject and write it to file

      static void saveRampe(QList<Tool*> rampe){
           
            QJsonArray rampeArr;
            QJsonObject jTool;
    
            for (auto &tool : rampe){
                jTool["nom"] = tool->nom();
                jTool["type"] = tool->typeToString();
                jTool["longueur"] = static_cast<double>(tool->longueur());
                jTool["diametre"] = static_cast<double>(tool->diametre());
                jTool["dureeVie"] = static_cast<double>(tool->dureeVie());
                jTool["tempsUsage"] = static_cast<double>(tool->tempsUtilisation());
                rampeArr.append(jTool);
            }
    
            QJsonDocument jrampe(rampeArr);
            QFile jsonFile("rampe.json");
            if(jsonFile.open(QFile::WriteOnly))
                 jsonFile.write(jrampe.toJson());
    }
    

    but the output in the file does not respect the properties order

    // current output
        {
            "diametre": 10, 
            "dureeVie": 600000, 
            "longueur": 100, 
            "nom": "T1", //<< this should be the first property
            "tempsUsage": 0,
            "type": "CYLINDRIQUE" 
        },
    
    // suited output
        {   
            "nom": "T1", 
            "type": "CYLINDRIQUE",
            "longueur": 100,
            "diametre": 10,
            "dureeVie": 600000, 
            "tempsUsage": 0
        },
    
    

    What is the reason ? is there a way to have the suited output witout using swap() ? edit : swap is not for that

    JonBJ KroMignonK 2 Replies Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @LeLev said in QJsonObject parameters order:

      What is the reason ?

      There is no reason - json has no ordering of attributes in any way.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      ODБOïO 1 Reply Last reply
      4
      • ODБOïO ODБOï

        hi,
        i serialize an object with QJsonObject and write it to file

          static void saveRampe(QList<Tool*> rampe){
               
                QJsonArray rampeArr;
                QJsonObject jTool;
        
                for (auto &tool : rampe){
                    jTool["nom"] = tool->nom();
                    jTool["type"] = tool->typeToString();
                    jTool["longueur"] = static_cast<double>(tool->longueur());
                    jTool["diametre"] = static_cast<double>(tool->diametre());
                    jTool["dureeVie"] = static_cast<double>(tool->dureeVie());
                    jTool["tempsUsage"] = static_cast<double>(tool->tempsUtilisation());
                    rampeArr.append(jTool);
                }
        
                QJsonDocument jrampe(rampeArr);
                QFile jsonFile("rampe.json");
                if(jsonFile.open(QFile::WriteOnly))
                     jsonFile.write(jrampe.toJson());
        }
        

        but the output in the file does not respect the properties order

        // current output
            {
                "diametre": 10, 
                "dureeVie": 600000, 
                "longueur": 100, 
                "nom": "T1", //<< this should be the first property
                "tempsUsage": 0,
                "type": "CYLINDRIQUE" 
            },
        
        // suited output
            {   
                "nom": "T1", 
                "type": "CYLINDRIQUE",
                "longueur": 100,
                "diametre": 10,
                "dureeVie": 600000, 
                "tempsUsage": 0
            },
        
        

        What is the reason ? is there a way to have the suited output witout using swap() ? edit : swap is not for that

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @LeLev
        QJsonObject looks like it hold an unordered list/hash/map. The output you show it produces is alphabetical, as per https://doc.qt.io/qt-5/qjsonobject.html#keys

        Returns a list of all keys in this object.

        The list is sorted lexographically.

        which is doubtless what it is using internally.

        I suspect the only thing you could try if you care about the order would be https://doc.qt.io/qt-5/qjsonobject.html#constBegin etc., and see what order the iterator returns?

        1 Reply Last reply
        2
        • ODБOïO ODБOï

          hi,
          i serialize an object with QJsonObject and write it to file

            static void saveRampe(QList<Tool*> rampe){
                 
                  QJsonArray rampeArr;
                  QJsonObject jTool;
          
                  for (auto &tool : rampe){
                      jTool["nom"] = tool->nom();
                      jTool["type"] = tool->typeToString();
                      jTool["longueur"] = static_cast<double>(tool->longueur());
                      jTool["diametre"] = static_cast<double>(tool->diametre());
                      jTool["dureeVie"] = static_cast<double>(tool->dureeVie());
                      jTool["tempsUsage"] = static_cast<double>(tool->tempsUtilisation());
                      rampeArr.append(jTool);
                  }
          
                  QJsonDocument jrampe(rampeArr);
                  QFile jsonFile("rampe.json");
                  if(jsonFile.open(QFile::WriteOnly))
                       jsonFile.write(jrampe.toJson());
          }
          

          but the output in the file does not respect the properties order

          // current output
              {
                  "diametre": 10, 
                  "dureeVie": 600000, 
                  "longueur": 100, 
                  "nom": "T1", //<< this should be the first property
                  "tempsUsage": 0,
                  "type": "CYLINDRIQUE" 
              },
          
          // suited output
              {   
                  "nom": "T1", 
                  "type": "CYLINDRIQUE",
                  "longueur": 100,
                  "diametre": 10,
                  "dureeVie": 600000, 
                  "tempsUsage": 0
              },
          
          

          What is the reason ? is there a way to have the suited output witout using swap() ? edit : swap is not for that

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @LeLev said in QJsonObject parameters order:

          What is the reason ?

          as @Christian-Ehrlicher said there is no ordering of attributes for Json strings.
          I think that QJsonObject, the values are stored in a QMap, and QMap are sorted by keys, so the attributes are alphabetic sorted ;)

          That's it

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            @LeLev said in QJsonObject parameters order:

            What is the reason ?

            There is no reason - json has no ordering of attributes in any way.

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            @Christian-Ehrlicher , @JonB , @KroMignon
            ok, thank you for the inputs

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              This is not limited to QJsonObject.

              See the official JSON specification (https://www.json.org/json-en.html ): "An object is an unordered set of name/value pairs."

              Therefore, we should not expect or require our objects to be serialized in any specific order.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              5
              • ElmehdiE Offline
                ElmehdiE Offline
                Elmehdi
                wrote on last edited by
                #7

                @LeLev I'm now facing the same problem, did you find a way to make it work the way you wanted?

                ODБOïO 1 Reply Last reply
                0
                • ElmehdiE Elmehdi

                  @LeLev I'm now facing the same problem, did you find a way to make it work the way you wanted?

                  ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by
                  #8

                  @Elmehdi no, i just let the file unordered

                  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