0

I'm using JSONAssert 1.5.0 to compare Jsons. I have a custom comparator to ignore ID fields, which would differ from testrun to testrun. JSON would look like this

{
  "id": 34,
  "fieldxy": abc
  "status": [
    {
      "id": 69,
      "code": 9250,
    },
    {
      "id": 68,
      "code": 9251,
    }
  ]
}

This comparator looks like this:

private final JSONComparator myComparator = new CustomComparator(
        JSONCompareMode.NON_EXTENSIBLE,
        new Customization("id",(o1, o2) -> true),
        new Customization("status",
                new ArrayValueMatcher<>(
                        new CustomComparator(JSONCompareMode.NON_EXTENSIBLE,
                            new Customization("status[*].id", (o1, o2) -> true))))
        );

I added the second customization today to ignore fields in an array as proposed here: Ignore specific node within array when comparing two JSON in Java Note: Without the ArrayValueMatcher it does not work.

The ignoring of both id fields, the base one "id" and the one in the status array "status.id" now works just fine

However. Now the ordering of the array objects is suddenly important The status objects has random ordering and now my comparison fails, if the "status.code" are not on the right place, even with NON_EXTENSIBLE as the JSONCompareMode. Before adding the second Customization and taking care, that status.id always has the real value, the ignoring of the array ordering did work.

Any ideas why this does not work? Do I need to add customizations for all array fields now? I'm clueless

Edit: Tried out a different approach, also doesn't work:

private final JSONComparator myComparator = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE,
new Customization("**.id",(o1, o2) -> true));
  • correct data obviously works:
  • (status.id = 69; status.code = 9250 // status.id = 68; status.code = 9251) : PASS
  • But using these two, does also work:
  • (status.id = 68; status.code = 9250 // status.id = 68; status.code = 9251) : PASS
  • (status.id = 69; status.code = 9250 // status.id = 69; status.code = 9251) : PASS
  • But not this:
  • (status.id = 68; status.code = 9250 // status.id = 69; status.code = 9251) : FAIL
  • Or anything:
  • (status.id = 123; status.code = 9250 // status.id = 456; status.code = 9251) : FAIL
Krwasch
  • 29
  • 1
  • 7

1 Answers1

0

Try this:

new CustomComparator(
    JSONCompareMode.NON_EXTENSIBLE,
    new Customization("id",(o1, o2) -> true),
    new Customization("status[*].id", (o1, o2) -> true));
  • Thanks for the suggestion. This did not work for me, for the same reasons the other solutions did not work, suddenly the "NON_EXTENSIBLE" was ignored. I've basically restructured everyhting by now, to avoid this comparison, as I didn't find a solution. – Krwasch Dec 05 '22 at 10:39