added an option to use a LinkedHashMap to keep the original order#190
added an option to use a LinkedHashMap to keep the original order#190Orbiter wants to merge 1 commit intostleary:masterfrom
Conversation
|
I think a better option may be to modify the toString option to use a specified order. Then you can just call the toString(true) method from logger calls and not mess with the internal representation. Historically PRs like this have been denied because JSON specifically defines that the keys are not ordered. |
This is true. Also, the access time of a key in a LinkedHashMap is O(N) while it is approximately O(1) for a HashMap, therefore switching to LinkedHashMap may cause performance problems for larger objects. |
|
you are both right, json defines that there is no specific order and a LinkedHashMap is less performant. However, the LinkedHashMap is just an option here (not a full replacement!) and having an explicit order is not a contradiction to the requirement, that the order has no logical relevance. Let me give you an example: I am migrating from com.fasterxml.jackson.databind.ObjectMapper to JSON-java. While I can hand over jackson a LinkedHashSet to pretty-print this in json, I don't have this option with JSON-java. The patch gives the user a convenience to create prettier json strings than just pretty-printed json because it's possible to show humans the same output as given i.e. in documentations. |
|
We aren't arguing about it's prettiness. We are commenting that historically requests like this have been denied for the fact that developers using JSON should not rely on an ordering of the keys. I offered a second option to implement something similar (toString, or maybe call it something like toStringOrdered()). Personally, I can't imagine ever instantiating a JSONObject and passing it a DEBUG value like your pull request would require. I'd rather see something like this in my code: JSONObject jo = new JSONObject(someMap);
... do stuff
if(error) {
LOG.error(jo.toStringOrdered());
}instead of JSONObject jo = new JSONObject(inDebugMode);
// copy my map into my json object
... do stuff
if(error) {
LOG.error(jo.toString());
} |
|
Thanks for making the pull request, but it can't be merged for the reasons already stated. |
debugging of data from a json api is much easier if the order of the keys is stable.
I added an option to store the JSONObject's properties in a LinkedHashMap to produce a self-defined order of entities when a JSONObject is serialized with toString()