JSON-java
JSON-java copied to clipboard
HashMap to LinkedHashMap
https://github.com/stleary/JSON-java/blob/6f92a3ab4e425123c4b1ac2b8b65a7d9fb1d9bcb/src/main/java/org/json/JSONObject.java#L187 https://github.com/stleary/JSON-java/blob/6f92a3ab4e425123c4b1ac2b8b65a7d9fb1d9bcb/src/main/java/org/json/JSONObject.java#L299 https://github.com/stleary/JSON-java/blob/6f92a3ab4e425123c4b1ac2b8b65a7d9fb1d9bcb/src/main/java/org/json/JSONObject.java#L301 https://github.com/stleary/JSON-java/blob/6f92a3ab4e425123c4b1ac2b8b65a7d9fb1d9bcb/src/main/java/org/json/JSONObject.java#L475 https://github.com/stleary/JSON-java/blob/6f92a3ab4e425123c4b1ac2b8b65a7d9fb1d9bcb/src/main/java/org/json/JSONObject.java#L2673
Not work
public static void main(String[] args) {
String json1 = "{\n" +
" \"b\": \"b\",\n" +
" \"a\": \"a\"\n" +
"}";
JSONObject jsonObject = new JSONObject();
jsonObject.put("b","b");
jsonObject.put("a","a");
String json2 = jsonObject.toString(4);
System.out.println(json2.equals(json1));
System.out.println(new JSONObject(json1).equals(jsonObject));
}
Can you replace HashMap to LinkedHashMap to new version dependency?
And create equals to JSONObject class
Now we use
public static JSONObject getLinkedJson() {
JSONObject jsonObject = new JSONObject();
try {
Field field = JSONObject.class.getDeclaredField("map");
field.setAccessible(true);
field.set(jsonObject,new LinkedHashMap<>());
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
return jsonObject;
}
but it's slow and wrong
I have the same problem
PRs will be accepted and approved for this change. The requirements will be that the app must use the existing HashMap by default but can be configured or executed to run in a mode that uses ~a LinkedHashMap~ a new mapping instead. The new mode should only use ~LinkedHashMap~ the new mapping and never use HashMap.
An example where the code in #658 would not work is when parsing a JSONArray that contains a nested JSONObject.
I will come up with a suitable test suite for validation. For more context please see the discussion in PR #658.
Hi @stleary , Kindly assign this to me
Assigned to @Mykiiii
hi @stleary, I can work on this, kindly assign it to me.
Any news on this? This is taking so long for such an easy fix. I pushed a solution on #658 more than 1y ago, but for some reason wasn't good enough. Seriously is anyone considering this issue? It's an easy fix and it would allow much greater flexibility.
@lvca Apologies for letting this sit for so long. Closing this issue because ordered objects will not be supported in this project due to it going against the spec and the intent of the original author.
I think it's unfortunate that this issue has not been addressed.
- The objects would not be ordered in the sense of being sorted, we are talking about retaining the insertion order.
- Android's implementation has long switched to LinkedHashMap.
- It could be an option.
- And that is the main point, it would really help with writing unit tests!
I think it's unfortunate that this issue has not been addressed.
- The objects would not be ordered in the sense of being sorted, we are talking about retaining the insertion order.
- Android's implementation has long switched to LinkedHashMap.
- It could be an option.
- And that is the main point, it would really help with writing unit tests!
Not just unit tests. We are currently doing integration and using json. Due to the inability to decode and encode json using this library, it is necessary to pass between methods and json as String and json as JSONObject. Many thanks to the developers of this project for their laziness, because ObjectMapper, Gson, etc. uses LinkedHashMap. Apparently, you should start using another library to work with JSON.
@Aleksey2093 Point taken, and sorry this is causing difficulties for your team. If it's any help, I don't anticipate any more big changes to the project; it is firmly in maintenance mode now. Therefore, if you want to fork JSON-Java and implement your own internal map, there is little chance of future changes that would affect your work. Alternatively, if this is not acceptable, your project might be better off with Gson or similar.