JSON-java icon indicating copy to clipboard operation
JSON-java copied to clipboard

HashMap to LinkedHashMap

Open Aleksey2093 opened this issue 3 years ago • 6 comments

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

Aleksey2093 avatar Aug 02 '22 11:08 Aleksey2093

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

Aleksey2093 avatar Aug 02 '22 11:08 Aleksey2093

I have the same problem

Anna-Stepancheva avatar Aug 15 '22 12:08 Anna-Stepancheva

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.

stleary avatar Aug 18 '22 16:08 stleary

Hi @stleary , Kindly assign this to me

Mykiiii avatar Oct 02 '22 11:10 Mykiiii

Assigned to @Mykiiii

stleary avatar Oct 02 '22 13:10 stleary

hi @stleary, I can work on this, kindly assign it to me.

Rajat-Sharma1710 avatar Oct 02 '22 17:10 Rajat-Sharma1710

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 avatar Jan 05 '23 00:01 lvca

@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.

stleary avatar Jan 05 '23 01:01 stleary

I think it's unfortunate that this issue has not been addressed.

  1. The objects would not be ordered in the sense of being sorted, we are talking about retaining the insertion order.
  2. Android's implementation has long switched to LinkedHashMap.
  3. It could be an option.
  4. And that is the main point, it would really help with writing unit tests!

devconsole avatar Jan 28 '23 11:01 devconsole

I think it's unfortunate that this issue has not been addressed.

  1. The objects would not be ordered in the sense of being sorted, we are talking about retaining the insertion order.
  2. Android's implementation has long switched to LinkedHashMap.
  3. It could be an option.
  4. 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 avatar Apr 30 '23 21:04 Aleksey2093

@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.

stleary avatar May 01 '23 16:05 stleary