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

Why is an object ordering values by Alphabetical Order? #660

Closed
gDev95 opened this issue Jul 20, 2017 · 6 comments · Fixed by #2258
Closed

Why is an object ordering values by Alphabetical Order? #660

gDev95 opened this issue Jul 20, 2017 · 6 comments · Fixed by #2258
Assignees
Labels
release item: ✨ new feature solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: invalid the issue is not related to the library solution: proposed fix a fix for the issue has been proposed and waits for confirmation solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)
Milestone

Comments

@gDev95
Copy link

gDev95 commented Jul 20, 2017

Hi I am experiencing some weird behaviour I cannot really get behind why it does the following.
Imagine you want to have following json object:

{
	"result_parameters":{
		"Category": 1,
		"Username": "Warrior11",
		"First_Name": "Max",
		"Date_Signed_Up": [1,1,2017]
	}
}

What I did was:

nlohmann::json j;
j["result_parameter"]["Category"] = 1;
j["result_parameter"]["Username"] =  "Warrior11";
j["result_parameter"]["First_Name"] = "Max";
j["result_parameter"]["Date_Signed_Up"] = [1,1,2017];

When I print this ( cout << j.dump<< endl; ) I get following output:

{"result_parameter":{"Category":1,"Date_Signed_Up":[1,1,2017],"First_Name":"Max","Username":"Warrior11"}}

The order is messed up, how come?

@nlohmann
Copy link
Owner

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 change it to any other order. If you do want to preserve the insertion order, you can specialize the object type with containers like tsl::ordered_map or nlohmann::fifo_map.

@nlohmann nlohmann added solution: invalid the issue is not related to the library solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) labels Jul 20, 2017
@nlohmann
Copy link
Owner

#106 #424

@sqwunkly
Copy link

sqwunkly commented Sep 6, 2017

Hi Niels,

Thanks for your reply. I checked out #106 #424 & #660, unfortunately I'm not an very experienced programmer and I doubt I've the knowledge/expertise to get the fifo_map up and running myself.

In #106 habemus-papadum tried to impliment the fifo_map but he seemed to run into significant problems and it appears he may not have resolved them, at least he didn't say so. He seems a lot more experienced than me.

#424 and #660 also provide no guidance on how to implement the fifo_maps or another solution for outputting the json data in unsorted format.

I'd appreciate if you could provide or point me to a working example for implementing the fifo_map using your json library, such that I can open existing json data, add objects, edit and delete and save the data in unsorted format in the same easy intuitive manner described in your examples, like this:-

image

Sorry if my request is of a basic level. If you could help I'd be much grateful, I feel others would also like to see a working example of how to implement the fifo_map to output unsorted json.

Greg

@nlohmann
Copy link
Owner

nlohmann commented Sep 6, 2017

Using fifo_map and the like should be as simple as replacing the template parameter for the objects. Unfortunately, this seems not to work as I expected, and I am currently not aware of a working example.

@nlohmann
Copy link
Owner

nlohmann commented Sep 6, 2017

(In the future, I shall mark issues regarding object orders as "won't fix" rather than pointing to fifo_map...)

@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
release item: ✨ new feature solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: invalid the issue is not related to the library solution: proposed fix a fix for the issue has been proposed and waits for confirmation solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants