-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to prevent alphabetical sorting of data? #727
Comments
This is covered in the README https://github.com/nlohmann/json#notes |
It is a pity to me that a JSON object does not preserve the order of elements. This is a highly desirable feature. For example, specifying parameters -- there is often a natural ordering. While the standard does not guarantee to preserve order, nor does it guarantee to alphabetise. This is a very beautiful library. I think this is why the community is reluctant to whinge, as am I. The ability to retain order would certainly beautify the function -- whether or not this would be nullified by the uglification to the code base, you are in the best position to see this. If it is possible to preserve order with some minor modification, a more complete documentation on how to do this would be most helpful! (I am linking your library to my development team as gold-standard example of how to architect a third-party library). |
I know that preserving the insertion order would be also compliant, but would also be more work in my case. However, the README states some solutions:
|
Unfortunaltely, this does not preserve the order in all cases:
|
@tilman22 have you found a work around to preserve the keys order if we were to parse from string like your example above? |
My workaround was to use RapidJson instead. |
I think your issue was in this line:
You are calling Instead try:
|
To be fixed with #2258. |
Feels like a huge flaw in the design of this library. Of course data order should be preserved - jumbling up the order just because the standard doesn't require ordered data is nonsense. |
Ooof, thanks. You can use ordered_json for this. And I think the documentation is clear about the rationale. |
Hi Niels,
Thanks a lot for writing your "JSON for Modern C++".
How do I save the JSON data to file while retaining it's original order (after opening and editing from an existing data.json file) such that the saved objects and their keys are subsequently not sorted alphabetically upon saving the data?
This is how I'd like the json data to be saved to file, as it was originally (not alphabetically sorted):-
However after saving to file, the data ends up alphabetically sorted (below).
Is there a way to prevent the json data being alphabetically sorted?
Much thanks. Greg
The text was updated successfully, but these errors were encountered: