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

Json diff #44

Open
marcospassos opened this issue Nov 21, 2016 · 10 comments
Open

Json diff #44

marcospassos opened this issue Nov 21, 2016 · 10 comments

Comments

@marcospassos
Copy link

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 marcospassos changed the title Raw Json diff Json diff Nov 21, 2016
@marcospassos
Copy link
Author

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
Copy link
Owner

lukas-krecan commented Nov 23, 2016

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
Copy link
Author

marcospassos commented Nov 23, 2016

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
Copy link
Author

marcospassos commented Nov 23, 2016

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

@lukas-krecan
Copy link
Owner

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
Copy link

ndebeiss commented Aug 6, 2018

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
Copy link

Hronom commented Feb 7, 2021

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 added a commit that referenced this issue Jul 4, 2024
lukas-krecan added a commit that referenced this issue Jul 4, 2024
lukas-krecan added a commit that referenced this issue Jul 4, 2024
lukas-krecan added a commit that referenced this issue Jul 4, 2024
lukas-krecan added a commit that referenced this issue Jul 4, 2024
@lukas-krecan
Copy link
Owner

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
Copy link

Hronom commented Jul 8, 2024

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

lukas-krecan added a commit that referenced this issue Jul 15, 2024
lukas-krecan added a commit that referenced this issue Jul 17, 2024
@ndebeiss
Copy link

ndebeiss commented Aug 6, 2024

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 !

lukas-krecan added a commit that referenced this issue Sep 28, 2024
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

4 participants