QJsonObject parameters order
-
hi,
i serialize an object with QJsonObject and write it to filestatic 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 -
@LeLev said in QJsonObject parameters order:
What is the reason ?
There is no reason - json has no ordering of attributes in any way.
-
hi,
i serialize an object with QJsonObject and write it to filestatic 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@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#keysReturns 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?
-
hi,
i serialize an object with QJsonObject and write it to filestatic 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@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
-
@LeLev said in QJsonObject parameters order:
What is the reason ?
There is no reason - json has no ordering of attributes in any way.
@Christian-Ehrlicher , @JonB , @KroMignon
ok, thank you for the inputs -
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.
-
@LeLev I'm now facing the same problem, did you find a way to make it work the way you wanted?