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

Comparing JSONArrays does not behave like expected #155

Open
erwinc1 opened this issue Jan 5, 2022 · 1 comment
Open

Comparing JSONArrays does not behave like expected #155

erwinc1 opened this issue Jan 5, 2022 · 1 comment

Comments

@erwinc1
Copy link

erwinc1 commented Jan 5, 2022

I hope below code is self explanatory. If not:

I am composing a new JSONArray from String a and String b combined in a list. Then I am composing a new JSONArray from a single string which contains a and b (in a different order though)

When I do a JSONAssert on both JSONArrays it tries to compare {"a":"b"} to a JSON object.

Console output: Note the backslashes in the first print line

["{\"a\":\"b\"}","{\"c\":\"d\"}"]
[{"c":"d"},{"a":"b"}]
Exception in thread "main" java.lang.AssertionError: [0]
Expected: {"a":"b"}
     got: a JSON object
 ; [1]
Expected: {"c":"d"}
     got: a JSON object

	at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:726)
	at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:709)
	at step_definitions.StepDefinitions.main(StepDefinitions.java:205)

Code to reproduce in Java:

    public static void main(String[] args) throws JSONException {
        var a = "{\"a\":\"b\"}";
        var b = "{\"c\":\"d\"}";
        var res = "[{\"c\":\"d\"},{\"a\":\"b\"}]";

        final var jsonElements = new JSONArray(List.of(a,b));
        final var resultJsonArray = new JSONArray(res);

        System.out.println(jsonElements);
        System.out.println(resultJsonArray);
        JSONAssert.assertEquals(jsonElements, resultJsonArray, JSONCompareMode.STRICT);
    }
@suyamamadushan
Copy link

Build JsonObject List and then pass it.
var a = "{"a":"b"}";
var b = "{"c":"d"}";
var res = "[{"c":"d"},{"a":"b"}]";

    List<JSONObject> jsonObjectList = new ArrayList<>();
    jsonObjectList.add(new JSONObject(a));
    jsonObjectList.add(new JSONObject(b));
    
    final var jsonElements = new JSONArray(jsonObjectList);
    final var resultJsonArray = new JSONArray(res);

    System.out.println(jsonElements);
    System.out.println(resultJsonArray);

    JSONAssert.assertEquals(jsonElements, resultJsonArray, JSONCompareMode.NON_EXTENSIBLE);

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

No branches or pull requests

2 participants