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

Replace HashMap with LinkedHashMap in JSONObject #66

Closed
clentfort opened this issue Sep 10, 2012 · 9 comments
Closed

Replace HashMap with LinkedHashMap in JSONObject #66

clentfort opened this issue Sep 10, 2012 · 9 comments

Comments

@clentfort
Copy link

In my opinion it would be useful to replace the HashMap in JSONObject with a LinkedHashMap to maintain the order of the inserted values. This is would be really helpful if one wants to create JSON files that are easy to read and easy to compare with the original files. Also it is more helpful when iterating through the objects since the values come in the order you expect them.

@douglascrockford
Copy link
Contributor

JSON specifies that keys are not ordered. There is a hazard that LinkedHashMap could cause applications to depend on ordering, and so break compatibility, and that would be an extremely bad thing.

@AmirHossein
Copy link

i think it should be great if make it optional. Ordered keys is good for some cases such as public api.

@johnjaylward
Copy link
Contributor

No. All JSON specifications specifically say the keys are unordered. If a public API is using ordered keys, then they are using JSON incorrectly.

@stleary
Copy link
Owner

stleary commented Mar 5, 2016

Agree with @johnjaylward. A JSON reference app has to follow the spec.

@AlexByte
Copy link

AlexByte commented Apr 28, 2018

You can use Gson

String jsonObject = new JsonParser().parse(json).getAsJsonObject().get("field").toString();
LinkedHashMap details = new Gson().fromJson(jsonObject, LinkedHashMap.class);

BGehrels pushed a commit to BGehrels/JSON-java that referenced this issue Apr 29, 2020
@Tilko
Copy link

Tilko commented Nov 10, 2020

If a public API is using ordered keys, then they are using JSON incorrectly.

you put your mind in jail or what ? ...
and god said that JSON must be used only for APIs or what ?

@johnjaylward
Copy link
Contributor

Yes. JSON was made for data exchange. If you are relying on some ordering of keys, Then you are using something that looks like JSON, but is not. If you need ordering in JSON, you use an Array, not an Object.

@Tilko
Copy link

Tilko commented Nov 10, 2020

I tried to imagine a case where the choice of a particular order could be a bad idea, guess what, I did not find any because the JSON spec let us free about it. Have I a lack of imagination ?

@stleary
Copy link
Owner

stleary commented Nov 11, 2020

@Tilko Thanks for offering your thoughts on this issue. In this case the project is sticking with unordered due to RFC 8259: "An object is an unordered collection of zero or more name/value pairs".

Let's give the last word to Douglas Crockford, who invented JSON, was the author of this project, and left this comment in issue #37: "This has been tried. What happens is people make applications that depend on that ordering, which breaks interoperability."

Repository owner locked as resolved and limited conversation to collaborators Nov 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants