lukas-krecan / JsonUnit

Compare JSON in your Unit Tests
Apache License 2.0
884 stars 115 forks source link

Json diff #44

Open marcospassos opened 7 years ago

marcospassos commented 7 years ago

Is there any way to see the json diff, exactly as when we assert strings? Using ordinary assertions on IntelliJ allows me to compare two jsons when an assertion fails. It's much more efficient for identifying the differences in long json files.

marcospassos commented 7 years ago

According to this source, it's simple as output both documents using a specific format:

http://stackoverflow.com/questions/10934743/formatting-output-so-that-intellij-idea-shows-diffs-for-two-texts

lukas-krecan commented 7 years ago

Hi, the trouble is that JSON document does not have predefined order. In other words what should get displayed as diff when comparing {a:1, b:2} and {b:1, a:2}? In order to show string-like diff we would need to somehow normalize the JSON. And it's tricky, it basically means to be able to sort generic object tree.

marcospassos commented 7 years ago

Yeah, I know, but for comparison purposes we can sort the keys, i.e alphabetical ascending order, or even sort the compared version according to the reference (preferred). In the later case, we can use a comparator that sorts looking to the index of the same key in the reference JSON document.

marcospassos commented 7 years ago

In our use case, we can save about 1 minute per broken test by comparing both JSON documents straight in the IDE.

lukas-krecan commented 7 years ago

I will think about it. The trouble is that it has to play well with other features like ${json-unit.ignore} and ignoring array order and other stuff.

ndebeiss commented 6 years ago

It is a work-around, as it will show all the differences, not only the json unit detected differences, but I use the following code to show IntelliJ comparator if there is an issue :

public static void assertThatAndShowIntelliJComparator(String expected, String actual, Matcher matcher) { try { assertThat(actual, matcher); } catch (AssertionError e) { Assert.assertEquals(e.getMessage(), expected, actual); } }

Hronom commented 3 years ago

I still need to use workaround to see full json:

        try {
            assertThatJson(actualJson)
                    .isEqualTo(expectedJson);
        } catch (AssertionError e) {
            Assert.assertEquals(e.getMessage(), expectedJson, actualJson);
        }

Can you fix this?

lukas-krecan commented 2 months ago

Hi @Hronom, @ndebeiss, @marcospassos I am currently looking into that. I am curious what would be your expectation, we can to the following

  1. Just normalize the JSONs, for example, order keys alphabetically and pretty print the JSONs. The donwside would be that the order of fields would change, is that acceptable?
  2. We can normalize the actual value based on the expected value. Instead of sorting the fields alphabetically, we would make sure the order in the actual value is the same as in the expected one.
  3. We can put the differences to the top. Instead of sorting, we can start printing the JSON from the parts that are different. This may be useful for larger JSONs
  4. We can only print the parts of JSON that are different. What option would you prefer and why? Thank you
Hronom commented 2 months ago

From my side it will be ok if we can have just printed actual and expected json's as is.

ndebeiss commented 1 month ago

hello Sorry, I don't have the use case anymore, but I would vote for option 1. Option 2 seems difficult. Option 3 and option 4 would override the IDE behavior. many thanks for the project !