Skip to content
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

Add ordered_json specialization with ordered object keys #2258

Merged
merged 27 commits into from
Jul 12, 2020
Merged

Conversation

nlohmann
Copy link
Owner

@nlohmann nlohmann commented Jul 11, 2020

This PR adds a container ordered_map which maintains the order of inserted keys. This container is then used in ordered_json, a specialization of basic_json type.

Closes #2179.
Closes #1817.
Closes #1717.
Closes #1106.
Closes #952.
Closes #727.
Closes #660.
Closes #543.
Closes #424.
Closes #106.

nlohmann and others added 26 commits June 19, 2020 15:27
instead of hard-coding it for std::map's value_type
This is more comprehensive and the "my_workaround_fifo_map" wrapper does not allow to infer value type as required in this branch.

This reverts commit 064a9c9.
to extract the actual ObjectType::value_type
Still fails on older compilers (GCC <= 5.5)
so that it can handle pair<const Key,...>
(forgotten in previous commit!)
Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>
Simple ordered_json that works on all supported compilers
…2179

� Conflicts:
�	single_include/nlohmann/json.hpp
@coveralls
Copy link

coveralls commented Jul 11, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling 738c83d on issue2179 into cbafed3 on develop.

@nlohmann nlohmann merged commit e316f5c into develop Jul 12, 2020
@nlohmann nlohmann deleted the issue2179 branch July 12, 2020 11:11
@nlohmann
Copy link
Owner Author

@tawmoto
Copy link

tawmoto commented Jul 20, 2020

Hi @nlohmann
So if we want to have the elements in the same way we insert them, should we use the new ordered_json instead of json type? Will be any performance penalty of using ordered_json compared to json?

Thank you 👍

@nlohmann
Copy link
Owner Author

@tawmoto

Yes, ordered_json is the type to use if the insertion order of object keys should be preserved. For small JSON objects, the performance may even be better than before. ordered_json uses a vector of key/value pairs as internal data structure, whereas json uses an std::map.

@tawmoto
Copy link

tawmoto commented Jul 21, 2020

@nlohmann
Thank you for the answer.
Just a curiosity of mine: may I ask if there a specifici reason that std::map is used instead of std::unordered_map? I know from benchmarks that std::unordered_map is almost always faster.

The new additions are really great, I guess I will wait for the next release, hopefully it comes soon. 👍

@nlohmann
Copy link
Owner Author

No specific reason - I only learned C++11 when I started the library, and so I was not aware of std::unordered_map then. But I am afraid that replacing std::map would break existing code.

@cellard0or
Copy link

Great addition, I guess this will help a lot of users :)

One question: Is it also possible to use parse() and get an ordered_json object? I quickly tried one or two things but did get it to work.

@gregmarr
Copy link
Contributor

gregmarr commented Aug 5, 2020

@cellard0or Use ordered_json::parse instead of json::parse.

@nlohmann
Copy link
Owner Author

nlohmann commented Aug 5, 2020

@cellard0or The release version 3.9.0 is buggy in that regard. This is fixed with #2319 and will be released as 3.9.1 soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment