|
1 | 1 | # Java json-diff |
2 | 2 |
|
3 | | -A customizable lib to perform a json-diff |
| 3 | +A customizable library to perform JSON comparisons with detailed diff output. |
4 | 4 |
|
5 | | -## Why Use json-diff library |
| 5 | +## Why Use json-diff? |
6 | 6 |
|
7 | | -The goal of this library is to provide a readable diff between two json file. |
| 7 | +This library provides: |
8 | 8 |
|
9 | | -In addition to the differential, a similarity score is calculated. |
10 | | -This score can be used to compare several json with each other and find the two most similar. |
11 | | - |
12 | | -The way to compare json is completely customisable. |
13 | | - |
14 | | -2 way to display diff are provided by default (patch file, text file). And you can easily create your own formatter. |
| 9 | +- **Readable diffs** between two JSON documents |
| 10 | +- **Similarity scoring** (0-100) to compare multiple JSON documents and find the most similar ones |
| 11 | +- **Fully customizable** comparison modes (strict, lenient, or mixed) |
| 12 | +- **Multiple output formats** (patch file, text) with the ability to create custom formatters |
15 | 13 |
|
16 | 14 | ## Installation |
17 | 15 |
|
18 | | -maven: |
| 16 | +**Maven:** |
19 | 17 | ```xml |
20 | 18 | <dependency> |
21 | 19 | <groupId>io.github.deblockt</groupId> |
22 | 20 | <artifactId>json-diff</artifactId> |
23 | | - <version>1.1.0</version> |
| 21 | + <version>2.0.0</version> |
24 | 22 | </dependency> |
25 | 23 | ``` |
26 | 24 |
|
27 | | -gradle: |
| 25 | +**Gradle:** |
28 | 26 | ```gradle |
29 | | -implementation 'io.github.deblockt:json-diff:1.1.0' |
| 27 | +implementation 'io.github.deblockt:json-diff:2.0.0' |
30 | 28 | ``` |
31 | 29 |
|
32 | | -## Usage |
| 30 | +> **Note:** Version 2.0.0 requires Java 21+ and uses Jackson 3.x |
| 31 | +
|
| 32 | +## Quick Start |
33 | 33 |
|
34 | | -example: |
35 | 34 | ```java |
36 | | -final var expectedJson = "{\"additionalProperty\":\"a\", \"foo\": \"bar\", \"bar\": \"bar\", \"numberMatch\": 10.0, \"numberUnmatched\": 10.01, \"arrayMatch\": [{\"b\":\"a\"}], \"arrayUnmatched\": [{\"b\":\"a\"}]}"; |
37 | | -final var receivedJson = "{\"foo\": \"foo\", \"bar\": \"bar\", \"numberMatch\": 10, \"numberUnmatched\": 10.02, \"arrayMatch\": [{\"b\":\"a\"}], \"arrayUnmatched\": {\"b\":\"b\"}}"; |
| 35 | +final var expectedJson = "{\"name\": \"John\", \"age\": 30, \"city\": \"Paris\"}"; |
| 36 | +final var receivedJson = "{\"name\": \"Jane\", \"age\": 30, \"country\": \"France\"}"; |
38 | 37 |
|
39 | | -// define your matcher |
40 | | -// CompositeJsonMatcher use other matcher to perform matching on objects, list or primitive |
| 38 | +// Define your matcher |
41 | 39 | final var jsonMatcher = new CompositeJsonMatcher( |
42 | | - new LenientJsonArrayPartialMatcher(), // comparing array using lenient mode (ignore array order and extra items) |
43 | | - new LenientJsonObjectPartialMatcher(), // comparing object using lenient mode (ignoring extra properties) |
44 | | - new LenientNumberPrimitivePartialMatcher(new StrictPrimitivePartialMatcher()) // comparing primitive types and manage numbers (100.00 == 100) |
| 40 | + new LenientJsonArrayPartialMatcher(), |
| 41 | + new LenientJsonObjectPartialMatcher(), |
| 42 | + new StrictPrimitivePartialMatcher() |
45 | 43 | ); |
46 | 44 |
|
47 | | -// generate a diff |
48 | | -final var jsondiff = DiffGenerator.diff(expectedJson, receivedJson, jsonMatcher); |
| 45 | +// Generate the diff |
| 46 | +final var diff = DiffGenerator.diff(expectedJson, receivedJson, jsonMatcher); |
49 | 47 |
|
50 | | -// use the viewer to collect diff data |
51 | | -final var errorsResult= OnlyErrorDiffViewer.from(jsondiff); |
| 48 | +// Display errors |
| 49 | +System.out.println(OnlyErrorDiffViewer.from(diff)); |
52 | 50 |
|
53 | | -// print the diff result |
54 | | -System.out.println(errorsResult); |
55 | | -// print a similarity ratio between expected and received json (0 <= ratio <= 100) |
56 | | -System.out.println(jsondiff.similarityRate()); |
| 51 | +// Get similarity score (0-100) |
| 52 | +System.out.println("Similarity: " + diff.similarityRate() + "%"); |
57 | 53 | ``` |
58 | | -Result: |
59 | | -``` |
60 | | -The property "$.additionalProperty" is not found |
61 | | -The property "$.numberUnmatched" didn't match. Expected 10.01, Received: 10.02 |
62 | | -The property "$.arrayUnmatched" didn't match. Expected [{"b":"a"}], Received: {"b":"b"} |
63 | | -The property "$.foo" didn't match. Expected "bar", Received: "foo" |
64 | 54 |
|
65 | | -76.0 |
66 | | -``` |
| 55 | +## Output Formats |
| 56 | + |
| 57 | +### Error List (OnlyErrorDiffViewer) |
67 | 58 |
|
68 | | -You can also generate a patch file using this viewer: |
69 | 59 | ```java |
70 | | -final var patch = PatchDiffViewer.from(jsondiff); |
| 60 | +final var errors = OnlyErrorDiffViewer.from(diff); |
| 61 | +System.out.println(errors); |
| 62 | +``` |
| 63 | + |
| 64 | +Output: |
| 65 | +``` |
| 66 | +The property "$.city" is not found |
| 67 | +The property "$.name" didn't match. Expected "John", Received: "Jane" |
| 68 | +``` |
71 | 69 |
|
72 | | -// use the viewer to collect diff data |
73 | | -final var patchFile= PatchDiffViewer.from(jsondiff); |
| 70 | +### Patch Format (PatchDiffViewer) |
74 | 71 |
|
75 | | -// print the diff result |
76 | | -System.out.println(patchFile); |
| 72 | +```java |
| 73 | +final var patch = PatchDiffViewer.from(diff); |
| 74 | +System.out.println(patch); |
77 | 75 | ``` |
78 | 76 |
|
79 | | -Result: |
80 | | -``` diff |
| 77 | +Output: |
| 78 | +```diff |
81 | 79 | --- actual |
82 | 80 | +++ expected |
83 | 81 | @@ @@ |
84 | 82 | { |
85 | | -+ "additionalProperty": "a", |
86 | | - "bar": "bar", |
87 | | -- "numberUnmatched": 10.02, |
88 | | -+ "numberUnmatched": 10.01, |
89 | | -- "arrayUnmatched": {"b":"b"}, |
90 | | -+ "arrayUnmatched": [{"b":"a"}], |
91 | | -- "foo": "foo", |
92 | | -+ "foo": "bar", |
93 | | - "numberMatch": 10.0, |
94 | | - "arrayMatch": [ |
95 | | - { |
96 | | - "b": "a" |
97 | | - } |
98 | | - ] |
| 83 | + "age": 30, |
| 84 | ++ "city": "Paris", |
| 85 | +- "country": "France", |
| 86 | +- "name": "Jane", |
| 87 | ++ "name": "John" |
99 | 88 | } |
100 | 89 | ``` |
101 | 90 |
|
102 | | -### Comparison mode |
| 91 | +## Comparison Modes |
| 92 | + |
| 93 | +`CompositeJsonMatcher` accepts multiple matchers that handle different JSON types. The order matters: the first matcher that can handle a comparison will be used. |
103 | 94 |
|
104 | | -You can use many comparison mode to compare you json: |
| 95 | +### Lenient Mode |
105 | 96 |
|
106 | | -If you want compare json using *lenient* comparison: |
107 | | -```java |
108 | | -final var fullLenient = new CompositeJsonMatcher( |
109 | | - new LenientJsonArrayPartialMatcher(), // comparing array using lenient mode (ignore array order and extra items) |
110 | | - new LenientJsonObjectPartialMatcher(), // comparing object using lenient mode (ignoring extra properties) |
111 | | - new LenientNumberPrimitivePartialMatcher(new StrictPrimitivePartialMatcher()) // comparing primitive types and manage numbers (100.00 == 100) |
| 97 | +Ignores extra properties and array order: |
| 98 | + |
| 99 | +```java |
| 100 | +final var lenientMatcher = new CompositeJsonMatcher( |
| 101 | + new LenientJsonArrayPartialMatcher(), // Ignores array order and extra items |
| 102 | + new LenientJsonObjectPartialMatcher(), // Ignores extra properties |
| 103 | + new LenientNumberPrimitivePartialMatcher(new StrictPrimitivePartialMatcher()) // 10.0 == 10 |
112 | 104 | ); |
113 | 105 | ``` |
114 | 106 |
|
115 | | -If you want compare json using *strict* comparison: |
116 | | -```java |
| 107 | +### Strict Mode |
| 108 | + |
| 109 | +Requires exact matches: |
| 110 | + |
| 111 | +```java |
117 | 112 | final var strictMatcher = new CompositeJsonMatcher( |
118 | | - new StrictJsonArrayPartialMatcher(), // comparing array using strict mode (object should have same properties/value) |
119 | | - new StrictJsonObjectPartialMatcher(), // comparing object using strict mode (array should have same item on same orders) |
120 | | - new StrictPrimitivePartialMatcher() // comparing primitive types (values should be strictly equals type and value) |
| 113 | + new StrictJsonArrayPartialMatcher(), // Same items in same order |
| 114 | + new StrictJsonObjectPartialMatcher(), // Same properties, no extras |
| 115 | + new StrictPrimitivePartialMatcher() // Exact type and value match |
| 116 | +); |
| 117 | +``` |
| 118 | + |
| 119 | +### Mixed Mode |
| 120 | + |
| 121 | +You can combine matchers for custom behavior: |
| 122 | + |
| 123 | +```java |
| 124 | +final var mixedMatcher = new CompositeJsonMatcher( |
| 125 | + new LenientJsonArrayPartialMatcher(), // Lenient on arrays |
| 126 | + new StrictJsonObjectPartialMatcher(), // Strict on objects |
| 127 | + new StrictPrimitivePartialMatcher() |
| 128 | +); |
| 129 | +``` |
| 130 | + |
| 131 | +## Available Matchers |
| 132 | + |
| 133 | +### Array Matchers |
| 134 | + |
| 135 | +| Matcher | Description | |
| 136 | +|---------|-------------| |
| 137 | +| `LenientJsonArrayPartialMatcher` | Ignores array order and extra items | |
| 138 | +| `StrictJsonArrayPartialMatcher` | Requires same items in same order | |
| 139 | + |
| 140 | +### Object Matchers |
| 141 | + |
| 142 | +| Matcher | Description | |
| 143 | +|---------|-------------| |
| 144 | +| `LenientJsonObjectPartialMatcher` | Ignores extra properties in received JSON | |
| 145 | +| `StrictJsonObjectPartialMatcher` | Requires exact same properties | |
| 146 | + |
| 147 | +### Primitive Matchers |
| 148 | + |
| 149 | +| Matcher | Description | |
| 150 | +|---------|-------------| |
| 151 | +| `StrictPrimitivePartialMatcher` | Exact type and value match | |
| 152 | +| `LenientNumberPrimitivePartialMatcher` | Numbers are equal if values match (`10.0 == 10`) | |
| 153 | + |
| 154 | +### Special Matchers |
| 155 | + |
| 156 | +| Matcher | Description | |
| 157 | +|---------|-------------| |
| 158 | +| `NullEqualsEmptyArrayMatcher` | Treats `null` and `[]` as equivalent | |
| 159 | + |
| 160 | +## Treating Null as Empty Array |
| 161 | + |
| 162 | +The `NullEqualsEmptyArrayMatcher` allows you to consider `null` values and empty arrays `[]` as equivalent. This is useful when different systems represent "no data" differently. |
| 163 | + |
| 164 | +```java |
| 165 | +final var jsonMatcher = new CompositeJsonMatcher( |
| 166 | + new NullEqualsEmptyArrayMatcher(), // Must be first to handle null vs [] |
| 167 | + new LenientJsonArrayPartialMatcher(), |
| 168 | + new LenientJsonObjectPartialMatcher(), |
| 169 | + new StrictPrimitivePartialMatcher() |
| 170 | +); |
| 171 | + |
| 172 | +// These will match with 100% similarity: |
| 173 | +// {"items": null} vs {"items": []} |
| 174 | +// {"items": []} vs {"items": null} |
| 175 | + |
| 176 | +final var diff = DiffGenerator.diff( |
| 177 | + "{\"items\": null}", |
| 178 | + "{\"items\": []}", |
| 179 | + jsonMatcher |
121 | 180 | ); |
| 181 | + |
| 182 | +System.out.println(diff.similarityRate()); // 100.0 |
122 | 183 | ``` |
123 | 184 |
|
124 | | -You can mix matcher. For example, be lenient on array and strict on object: |
125 | | -```java |
126 | | -final var matcher = new CompositeJsonMatcher( |
127 | | - new LenientJsonArrayPartialMatcher(), // comparing array using lenient mode (ignore array order and extra items) |
128 | | - new StrictJsonObjectPartialMatcher(), // comparing object using strict mode (array should have same item on same orders) |
129 | | - new StrictPrimitivePartialMatcher() // comparing primitive types (values should be strictly equals type and value) |
| 185 | +**Important:** |
| 186 | +- Place `NullEqualsEmptyArrayMatcher` **before** other matchers in the constructor |
| 187 | +- This matcher only handles `null` vs empty array `[]`, not missing properties |
| 188 | +- Non-empty arrays do not match `null` |
| 189 | + |
| 190 | +## Advanced Example |
| 191 | + |
| 192 | +```java |
| 193 | +final var expectedJson = """ |
| 194 | + { |
| 195 | + "additionalProperty": "a", |
| 196 | + "foo": "bar", |
| 197 | + "bar": "bar", |
| 198 | + "numberMatch": 10.0, |
| 199 | + "numberUnmatched": 10.01, |
| 200 | + "arrayMatch": [{"b": "a"}], |
| 201 | + "arrayUnmatched": [{"b": "a"}] |
| 202 | + } |
| 203 | + """; |
| 204 | + |
| 205 | +final var receivedJson = """ |
| 206 | + { |
| 207 | + "foo": "foo", |
| 208 | + "bar": "bar", |
| 209 | + "numberMatch": 10, |
| 210 | + "numberUnmatched": 10.02, |
| 211 | + "arrayMatch": [{"b": "a"}], |
| 212 | + "arrayUnmatched": {"b": "b"} |
| 213 | + } |
| 214 | + """; |
| 215 | + |
| 216 | +final var jsonMatcher = new CompositeJsonMatcher( |
| 217 | + new LenientJsonArrayPartialMatcher(), |
| 218 | + new LenientJsonObjectPartialMatcher(), |
| 219 | + new LenientNumberPrimitivePartialMatcher(new StrictPrimitivePartialMatcher()) |
130 | 220 | ); |
| 221 | + |
| 222 | +final var diff = DiffGenerator.diff(expectedJson, receivedJson, jsonMatcher); |
| 223 | + |
| 224 | +System.out.println(OnlyErrorDiffViewer.from(diff)); |
| 225 | +System.out.println("Similarity: " + diff.similarityRate() + "%"); |
| 226 | +``` |
| 227 | + |
| 228 | +Output: |
| 229 | +``` |
| 230 | +The property "$.additionalProperty" is not found |
| 231 | +The property "$.numberUnmatched" didn't match. Expected 10.01, Received: 10.02 |
| 232 | +The property "$.arrayUnmatched" didn't match. Expected [{"b":"a"}], Received: {"b":"b"} |
| 233 | +The property "$.foo" didn't match. Expected "bar", Received: "foo" |
| 234 | +
|
| 235 | +Similarity: 76.0% |
131 | 236 | ``` |
| 237 | + |
| 238 | +## Creating Custom Matchers |
| 239 | + |
| 240 | +You can create custom matchers by implementing the `PartialJsonMatcher<T>` interface: |
| 241 | + |
| 242 | +```java |
| 243 | +public class MyCustomMatcher implements PartialJsonMatcher<JsonNode> { |
| 244 | + |
| 245 | + @Override |
| 246 | + public boolean manage(JsonNode expected, JsonNode received) { |
| 247 | + // Return true if this matcher should handle this comparison |
| 248 | + return /* your condition */; |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public JsonDiff jsonDiff(Path path, JsonNode expected, JsonNode received, JsonMatcher jsonMatcher) { |
| 253 | + // Return your diff result |
| 254 | + if (/* values match */) { |
| 255 | + return new MatchedPrimaryDiff(path, expected); |
| 256 | + } |
| 257 | + return new UnMatchedPrimaryDiff(path, expected, received); |
| 258 | + } |
| 259 | +} |
| 260 | +``` |
| 261 | + |
| 262 | +## License |
| 263 | + |
| 264 | +This project is licensed under the MIT License. |
0 commit comments