Skip to content

Commit 1c6565b

Browse files
committed
refactor: rename class and add readme section
1 parent 56961a3 commit 1c6565b

File tree

4 files changed

+82
-23
lines changed

4 files changed

+82
-23
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ final var mixedMatcher = new CompositeJsonMatcher(
165165
| Matcher | Description |
166166
|---------|-------------|
167167
| `NullEqualsEmptyArrayMatcher` | Treats `null` and `[]` as equivalent |
168+
| `IgnoredPathMatcher` | Ignores specified fields during comparison |
168169

169170
## Treating Null as Empty Array
170171

@@ -196,6 +197,64 @@ System.out.println(diff.similarityRate()); // 100.0
196197
- This matcher only handles `null` vs empty array `[]`, not missing properties
197198
- Non-empty arrays do not match `null`
198199

200+
## Ignoring path
201+
202+
The `IgnoredPathMatcher` allows you to ignore specific fields during comparison. This is useful for fields like timestamps, IDs, or other dynamic values that you don't want to compare.
203+
204+
```java
205+
final var jsonMatcher = new CompositeJsonMatcher(
206+
new IgnoredPathMatcher("timestamp", "id"), // Must be first
207+
new LenientJsonArrayPartialMatcher(),
208+
new LenientJsonObjectPartialMatcher(),
209+
new StrictPrimitivePartialMatcher()
210+
);
211+
212+
// These will match with 100% similarity:
213+
final var diff = DiffGenerator.diff(
214+
"{\"name\": \"John\", \"timestamp\": \"2024-01-01\"}",
215+
"{\"name\": \"John\", \"timestamp\": \"2024-12-31\"}",
216+
jsonMatcher
217+
);
218+
219+
System.out.println(diff.similarityRate()); // 100.0
220+
```
221+
222+
### Path Patterns
223+
224+
The `IgnoredPathMatcher` supports various path patterns:
225+
226+
| Pattern | Description | Example |
227+
|---------|-------------|---------|
228+
| `name` | Matches field `name` at any level | Ignores `$.name`, `$.user.name`, `$.data.user.name` |
229+
| `user.name` | Matches `name` under `user` | Ignores `$.user.name`, `$.data.user.name` |
230+
| `*.name` | Wildcard for any property | Ignores `$.foo.name`, `$.bar.name` |
231+
| `items[0]` | Matches specific array index | Ignores `$.items[0]` |
232+
| `items[*]` | Wildcard for any array index | Ignores `$.items[0]`, `$.items[1]`, etc. |
233+
| `items[*].id` | Field in any array element | Ignores `$.items[0].id`, `$.items[5].id` |
234+
235+
### Examples
236+
237+
```java
238+
// Ignore a single field everywhere
239+
new IgnoredPathMatcher("createdAt")
240+
241+
// Ignore multiple fields
242+
new IgnoredPathMatcher("createdAt", "updatedAt", "id")
243+
244+
// Ignore nested field
245+
new IgnoredPathMatcher("metadata.timestamp")
246+
247+
// Ignore field in all array elements
248+
new IgnoredPathMatcher("users[*].password")
249+
250+
// Combine multiple patterns
251+
new IgnoredPathMatcher("id", "*.createdAt", "items[*].internalId")
252+
```
253+
254+
**Important:**
255+
- Place `IgnoredPathMatcher` **before** other matchers in the constructor
256+
- Patterns match against the end of the path, so `name` matches `$.user.name` as well as `$.name`
257+
199258
## Advanced Example
200259

201260
```java

src/main/java/com/deblock/jsondiff/matcher/IgnoringFieldMatcher.java renamed to src/main/java/com/deblock/jsondiff/matcher/IgnoredPathMatcher.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
import java.util.Arrays;
88
import java.util.List;
99

10-
public class IgnoringFieldMatcher implements PartialJsonMatcher {
11-
private final List<PathMatcher> fieldsToIgnore;
10+
public class IgnoredPathMatcher implements PartialJsonMatcher {
11+
private final List<PathMatcher> pathsToIgnore;
1212

13-
public IgnoringFieldMatcher(List<String> paths) {
14-
this.fieldsToIgnore = paths.stream()
13+
public IgnoredPathMatcher(List<String> paths) {
14+
this.pathsToIgnore = paths.stream()
1515
.map(PathMatcher::from)
1616
.toList();
1717
}
1818

19-
public IgnoringFieldMatcher(String ...paths) {
19+
public IgnoredPathMatcher(String ...paths) {
2020
this(Arrays.stream(paths).toList());
2121
}
2222

@@ -27,6 +27,6 @@ public JsonDiff jsonDiff(Path path, JsonNode expectedJson, JsonNode receivedJson
2727

2828
@Override
2929
public boolean manage(Path path, JsonNode expected, JsonNode received) {
30-
return fieldsToIgnore.stream().anyMatch(pattern -> pattern.match(path));
30+
return pathsToIgnore.stream().anyMatch(pattern -> pattern.match(path));
3131
}
3232
}

src/test/java/com/deblock/jsondiff/integration/IgnoringFieldsIntegrationTest.java renamed to src/test/java/com/deblock/jsondiff/integration/IgnoredPathIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
import static org.junit.jupiter.api.Assertions.assertEquals;
88

9-
public class IgnoringFieldsIntegrationTest {
9+
public class IgnoredPathIntegrationTest {
1010

1111
private final CompositeJsonMatcher jsonMatcher = new CompositeJsonMatcher(
12-
new IgnoringFieldMatcher("foo"),
12+
new IgnoredPathMatcher("foo"),
1313
new LenientJsonArrayPartialMatcher(),
1414
new LenientJsonObjectPartialMatcher(),
1515
new StrictPrimitivePartialMatcher()

src/test/java/com/deblock/jsondiff/matcher/IgnoringFieldMatcherTest.java renamed to src/test/java/com/deblock/jsondiff/matcher/IgnoredPathMatcherTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010

1111
import static org.junit.jupiter.api.Assertions.*;
1212

13-
class IgnoringFieldMatcherTest {
13+
class IgnoredPathMatcherTest {
1414

1515
@Nested
1616
class ManageMethod {
1717

1818
@Test
1919
void shouldMatchExactPath() {
20-
var matcher = new IgnoringFieldMatcher("name");
20+
var matcher = new IgnoredPathMatcher("name");
2121
var path = Path.ROOT.add(Path.PathItem.of("name"));
2222

2323
assertTrue(matcher.manage(path, null, null));
2424
}
2525

2626
@Test
2727
void shouldMatchEndOfPath() {
28-
var matcher = new IgnoringFieldMatcher("name");
28+
var matcher = new IgnoredPathMatcher("name");
2929
var path = Path.ROOT
3030
.add(Path.PathItem.of("user"))
3131
.add(Path.PathItem.of("name"));
@@ -35,15 +35,15 @@ void shouldMatchEndOfPath() {
3535

3636
@Test
3737
void shouldNotMatchDifferentPath() {
38-
var matcher = new IgnoringFieldMatcher("name");
38+
var matcher = new IgnoredPathMatcher("name");
3939
var path = Path.ROOT.add(Path.PathItem.of("age"));
4040

4141
assertFalse(matcher.manage(path, null, null));
4242
}
4343

4444
@Test
4545
void shouldMatchNestedPath() {
46-
var matcher = new IgnoringFieldMatcher("user.name");
46+
var matcher = new IgnoredPathMatcher("user.name");
4747
var path = Path.ROOT
4848
.add(Path.PathItem.of("data"))
4949
.add(Path.PathItem.of("user"))
@@ -54,7 +54,7 @@ void shouldMatchNestedPath() {
5454

5555
@Test
5656
void shouldNotMatchPartialNestedPath() {
57-
var matcher = new IgnoringFieldMatcher("user.name");
57+
var matcher = new IgnoredPathMatcher("user.name");
5858
var path = Path.ROOT
5959
.add(Path.PathItem.of("name"));
6060

@@ -63,7 +63,7 @@ void shouldNotMatchPartialNestedPath() {
6363

6464
@Test
6565
void shouldMatchWithWildcardProperty() {
66-
var matcher = new IgnoringFieldMatcher("*.name");
66+
var matcher = new IgnoredPathMatcher("*.name");
6767
var path = Path.ROOT
6868
.add(Path.PathItem.of("user"))
6969
.add(Path.PathItem.of("name"));
@@ -73,7 +73,7 @@ void shouldMatchWithWildcardProperty() {
7373

7474
@Test
7575
void shouldMatchArrayIndex() {
76-
var matcher = new IgnoringFieldMatcher("items[0]");
76+
var matcher = new IgnoredPathMatcher("items[0]");
7777
var path = Path.ROOT
7878
.add(Path.PathItem.of("items"))
7979
.add(Path.PathItem.of(0));
@@ -83,7 +83,7 @@ void shouldMatchArrayIndex() {
8383

8484
@Test
8585
void shouldMatchArrayWildcard() {
86-
var matcher = new IgnoringFieldMatcher("items[*].id");
86+
var matcher = new IgnoredPathMatcher("items[*].id");
8787
var path = Path.ROOT
8888
.add(Path.PathItem.of("items"))
8989
.add(Path.PathItem.of(5))
@@ -94,7 +94,7 @@ void shouldMatchArrayWildcard() {
9494

9595
@Test
9696
void shouldMatchAnyOfMultiplePatterns() {
97-
var matcher = new IgnoringFieldMatcher("name", "age", "email");
97+
var matcher = new IgnoredPathMatcher("name", "age", "email");
9898
var pathName = Path.ROOT.add(Path.PathItem.of("name"));
9999
var pathAge = Path.ROOT.add(Path.PathItem.of("age"));
100100
var pathEmail = Path.ROOT.add(Path.PathItem.of("email"));
@@ -108,7 +108,7 @@ void shouldMatchAnyOfMultiplePatterns() {
108108

109109
@Test
110110
void shouldWorkWithListConstructor() {
111-
var matcher = new IgnoringFieldMatcher(List.of("name", "age"));
111+
var matcher = new IgnoredPathMatcher(List.of("name", "age"));
112112
var pathName = Path.ROOT.add(Path.PathItem.of("name"));
113113
var pathAge = Path.ROOT.add(Path.PathItem.of("age"));
114114

@@ -118,7 +118,7 @@ void shouldWorkWithListConstructor() {
118118

119119
@Test
120120
void shouldNotMatchRootPath() {
121-
var matcher = new IgnoringFieldMatcher("name");
121+
var matcher = new IgnoredPathMatcher("name");
122122

123123
assertFalse(matcher.manage(Path.ROOT, null, null));
124124
}
@@ -129,7 +129,7 @@ class JsonDiffMethod {
129129

130130
@Test
131131
void shouldReturnMatchedDiffWithFullSimilarity() {
132-
var matcher = new IgnoringFieldMatcher("name");
132+
var matcher = new IgnoredPathMatcher("name");
133133
var path = Path.ROOT.add(Path.PathItem.of("name"));
134134
var expected = StringNode.valueOf("John");
135135
var received = StringNode.valueOf("Jane");
@@ -142,7 +142,7 @@ void shouldReturnMatchedDiffWithFullSimilarity() {
142142

143143
@Test
144144
void shouldReturnMatchedDiffEvenWithDifferentTypes() {
145-
var matcher = new IgnoringFieldMatcher("value");
145+
var matcher = new IgnoredPathMatcher("value");
146146
var path = Path.ROOT.add(Path.PathItem.of("value"));
147147
var expected = StringNode.valueOf("100");
148148
var received = IntNode.valueOf(100);
@@ -154,7 +154,7 @@ void shouldReturnMatchedDiffEvenWithDifferentTypes() {
154154

155155
@Test
156156
void shouldReturnMatchedDiffForNestedPath() {
157-
var matcher = new IgnoringFieldMatcher("user.timestamp");
157+
var matcher = new IgnoredPathMatcher("user.timestamp");
158158
var path = Path.ROOT
159159
.add(Path.PathItem.of("user"))
160160
.add(Path.PathItem.of("timestamp"));

0 commit comments

Comments
 (0)