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

Automatic ordered JSON #424

Closed
MilowB opened this issue Jan 9, 2017 · 12 comments · Fixed by #2258
Closed

Automatic ordered JSON #424

MilowB opened this issue Jan 9, 2017 · 12 comments · Fixed by #2258
Assignees
Labels
kind: question release item: ✨ new feature solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@MilowB
Copy link

MilowB commented Jan 9, 2017

Hi,

Json is automatically sorted by an alphabetic order. I don't find any information about that in your readme. Is there a way to avoid sorting ?

@gregmarr
Copy link
Contributor

gregmarr commented Jan 9, 2017

https://github.com/nlohmann/json/blob/master/.github/CONTRIBUTING.md

We do not preserve the insertion order of object elements. The JSON standard defines objects as "an unordered collection of zero or more name/value pairs". To this end, this library does not preserve insertion order of name/value pairs. (In fact, keys will be traversed in alphabetical order as std::map with std::less is used by default.) Note this behavior conforms to the standard, and we shall not it to any other order.

@gregmarr
Copy link
Contributor

gregmarr commented Jan 9, 2017

#106

You can check out https://github.com/nlohmann/fifo_map - it should work as a replacement of std::map and maintains the key's order.

@nlohmann nlohmann added kind: question solution: duplicate the issue is a duplicate; refer to the linked issue instead labels Jan 9, 2017
@nlohmann nlohmann closed this as completed Jan 9, 2017
@fyears
Copy link

fyears commented Jan 12, 2017

Hi. A quick question regarding this issue (just out of curious): why not use std::unordered_map by default instead of std::map? After all, json objects are unsorted by semantics. And unordered_map should gain significant speed up regarding the time complexity.

@nlohmann
Copy link
Owner

The problem is that we are defining an object basic_json and inside a member of type std::map<std::string, basic_json>. At this point, basic_json is not yet fully defined. This seems to be OK for std::map on all platforms/compilers I encountered so far, but it is not for std::unordered_map.

@code-blooded245
Copy link

Hi, is there any way to sort nlohmann::json depending upon values, like for example,
if key is string and value is double, can I sort json based on double?

@nlohmann
Copy link
Owner

Can you give an example for this?

@code-blooded245
Copy link

Example:
if input = {
"abc" : 0.288,
"pqr" : 0.176,
"xyz" : 0.555,
"zaz" : 0.345
}
After sorting it should look something like,
output = {
"pqr" : 0.176,
"abc" : 0.288,
"lmo" : 0.345,
"xyz" : 0.555
}
Basically sorted by values.

@nlohmann
Copy link
Owner

This is not possible in the library (see https://github.com/nlohmann/json#order-of-object-keys). You could parse the value into some C++ container (e.g., a vector of pairs) and sort it yourself.

@code-blooded245
Copy link

Thanks for the support.

@code-blooded245
Copy link

Hey, is there a way to use std::max_element with nlohmann::json to get the key-value pair with the highest value for the above-given example?
prototype:
auto max = std::max_element (j.begin(), j.end(), cmp);

@nlohmann
Copy link
Owner

You may be able to achieve this via the items() function.

@nlohmann
Copy link
Owner

To be fixed with #2258.

@nlohmann nlohmann reopened this Jul 11, 2020
@nlohmann nlohmann added release item: ✨ new feature solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jul 11, 2020
@nlohmann nlohmann self-assigned this Jul 11, 2020
@nlohmann nlohmann added this to the Release 3.8.1 milestone Jul 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question release item: ✨ new feature solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants