-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Conversation
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. |
JSON specifically does not order the properties. Imposing an order could induce some receivers to expect a particular order, which could be breaking. |
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 |
What you are suggesting is an ordering. |
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. |
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. |
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. |
That is only true if you refuse to understand that there is a downside. |
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 |
Please read the above. |
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.