Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
About QJsonObject: the order of the inserted items is not preserved
liangbright last edited by
I have checked the forum, there is no such topic. So I start this one.
JObject["Department"] = QString("xxx");
JObject["Chairman"] = QString("xxx");
JObject["Assignment"] = QString("xxx");
JObject["Backup"] = QString("xxx");
then write it to a json file, and I get
"Assignment" : "xxx"
"Backup" : "xxx"
"Chairman" : "xxx"
It is not the order by which I put the items in.
I know it is not a bug because JSON standard does not require the order of the output to be the same as the order of the input. However, since json file is designed to be human-readable, we use it to store valuable information (algorithm output), and often we need to read the files.
We could invent a file format, but we need to exchange file with other people to communicate over internet. It is better to use a standard file, and we use json.
Java has a solution: use LinkedHashMap as input, which is mentioned in the link
Other people may also need order-preserved output for other reasons . And someone mentioned that "V8, for instance, keeps all keys in insertion order except for keys that can be parsed as unsinged 32-bit integers." in the link
In summary, could Qt provide a method/option that can preserve the input order?
Thank you very much.
I don't think this functionality should be provided by Qt. The standard says the order is unspecified, so you shouldn't rely on it, even if some library or language provides it. What if suddenly your code that relies on it receives a json from another source that doesn't have this guarantee or, for example, reverses that order?
I would say if you need to preserve order use arrays instead of objects. They're guaranteed to do so.
liangbright last edited by
Thanks for the reply.
in my case:
My computer program can read out of order json, and it does not rely on any order. However, human (me and others) can not read out of order json. So we need to preserve the output order for human readers. JSON advocates itself as "human-readable" against XML, and clearly the order matters for the human-readability. By doing so, we do not violate JSON standard, and we make it better.
in other cases:
according to stackoverflow, v8 preserves the order. I guess in that way json file can be easily checked by human users, e.g., a file with records form database.
In summary, it is all about human-readability.
Well I would guess that json object is implemented as some sort of map, which usually don't care about insertion order and changing it into something else might have performance penalties.
From what I see that V8 parser keeps the order only partially and I wouldn't hold my breath that it will stay like that forever. Certainly they don't guarantee it. It's probably an artifact of their implementation. XML doesn't care about the order of elements either(without some sort of schema or DTD).
It's not like Qt implementation makes a mess. I would argue that alphabetical order is pretty human-readable and certainly a lot friendlier to the likes of source control systems (eg. look what mess are xml Visual Studio project files).
Are arrays an alternative for you or is the format already set in stone?
Anyway, I'm not opposing. I just don't see it as that much of an issue. If you want to you can always make a feature request "here":https://bugreports.qt-project.org