-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
maintaining order of keys during iteration #106
Comments
When I understand you correctly, JavaScript remembers the order in which values are defined and preserves it during iteration and serialization. However, RFC 7159 states:
(emphasis added) and
The status quoAs the Where to go from hereAs the |
Hi there, great work! I'm currently using jsoncpp and looking for an alternative which should keep track of the insertion order. Otherwise I would need to somehow somewhere keep track of the order myself. I'll check out your json lib anyway (like the intuitive syntax). Your link suggests to use a map and a vector but to integrate that sounds like a lot of work ... Any better ideas? |
You can check out https://github.com/nlohmann/fifo_map - it should work as a replacement of |
Hi -- Are there any known gotchas to using nlohmann's fifo map as a replacement for the default std::map? I was thinking of making the switch... |
@habemus-papadum I implemented that container a while ago just for the purpose of having something to refer to, because a lot of people asked for order-preserving objects. I haven't heard of any bugs, but also not of a lot users. So just give it a try and file issues if you encounter an error. |
@nlohmann : sounds like a plan! thanks |
sadly I believe the problematic places are (in nlohmann_json.hpp):
after trying to replace the comparator with the one in fifo_map, the following portion of the code complains
This is as far as I got. I'll keep trying, but it started to look like there may not be a simple drop in solution. |
I was able to make (two) small changes to json.hpp (removing the hard coded comparator & and just replacing the default template parameter in basic_json from std::map to fifo map). While ugly, this get things compiling for now. However, things still do not work because I believe the default copy/move constructors/assignment operators in fifo_map are not correct (because of the pointer living inside the comparator). That's what I am playing around with now but I might be stuck. Worse case I will create a github repo that shows the current progress. |
To be fixed with #2258. |
keys in JSON are not sorted, but ordered based on definition. This becomes apparent when iterating keys of an object. For example, in JavaScript, with an object literal:
prints
For example, I believe most JS JITs keep separate shapes for object with the same properties but defined in different orders.
The text was updated successfully, but these errors were encountered: