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

Using LinkedHashMap to preserve property order. #106

Closed
wants to merge 1 commit into from

Conversation

JoshMcCullough
Copy link

Although the JSON standard suggests that property order does not matter, in the real world that is not always the case. At the very least, it makes sense to preserve the order in which the user added the properties (instead of sorting them alphabetically or in an otherwise arbitrary manner). Using a LinkedHashMap does just that.

@JoshMcCullough
Copy link
Author

For example, if we are data-binding a JSON object and emitting all properties, the developer may have ordered them in the same way they would like them to display. If the order is not preserved, then, the result will be an incorrectly displayed list of properties.

@douglascrockford
Copy link
Contributor

JSON specifically does not order the properties. Imposing an order could induce some receivers to expect a particular order, which could be breaking.

@JoshMcCullough
Copy link
Author

I'm not suggesting you impose an order, I'm suggesting you don't. E.g. leave the order of the properties as they were when the JSONObject was created. Currently the order comes out different then it goes in - which seems wrong, IMO.

@douglascrockford
Copy link
Contributor

What you are suggesting is an ordering.

@JoshMcCullough
Copy link
Author

Not to beat a dead horse here, this is your project and I respect your decision. However I have to disagree. You are suggesting that random ordering of keys based on hash value is more meaningful than leaving the order as it was originally when the object was created. I'm very interested to understand your point of view on this. Because you are inherently ordering by hash already! Thanks.

@rliota
Copy link

rliota commented Nov 25, 2014

JavaScript Object (the JSO in JSON) properties are not guaranteed to be ordered. If you want to preserve the order of any object properties you should wrap your ordered key value pairs in an object, and insert those objects into an array.

Even if you guarantee the order in which the properties are written is unchanged, you can't guarantee that the parser on the client end will respect the ordering you have imposed, and that's just unfair to anyone expected to consume your JSON.

@JoshMcCullough
Copy link
Author

My argument was simply that JSON-Java should attempt to honor the developer's property order instead of reordering the properties by using a hash-ordered map. It's a 2-second change that has no down side.

@douglascrockford
Copy link
Contributor

That is only true if you refuse to understand that there is a downside.

@JoshMcCullough
Copy link
Author

Well I did ask for your point of view, please let me know what the downside is in this case. I'm genuinely curious to know the answer. I don't think it would be performance since LinkedHashMap generally performs faster for iteration than HashMap.

@douglascrockford
Copy link
Contributor

Please read the above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants