-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLenientNumberPrimitivePartialMatcher.java
More file actions
41 lines (37 loc) · 1.49 KB
/
LenientNumberPrimitivePartialMatcher.java
File metadata and controls
41 lines (37 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.deblock.jsondiff.matcher;
import com.deblock.jsondiff.diff.JsonDiff;
import com.deblock.jsondiff.diff.MatchedPrimaryDiff;
import com.deblock.jsondiff.diff.UnMatchedPrimaryDiff;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.node.ValueNode;
/**
* A matcher that compares numeric values leniently.
* Two numbers are considered equal if their decimal values are equal,
* regardless of their representation (e.g., 10.0 == 10).
*
* <p>This matcher only handles numeric nodes. For other primitive types,
* add {@link StrictPrimitivePartialMatcher} to your {@link CompositeJsonMatcher}.</p>
*
* <p>Example usage:</p>
* <pre>
* new CompositeJsonMatcher(
* new LenientJsonArrayPartialMatcher(),
* new LenientJsonObjectPartialMatcher(),
* new LenientNumberPrimitivePartialMatcher(),
* new StrictPrimitivePartialMatcher()
* );
* </pre>
*/
public class LenientNumberPrimitivePartialMatcher implements PartialJsonMatcher<ValueNode> {
@Override
public JsonDiff jsonDiff(Path path, ValueNode expectedValue, ValueNode receivedValue, JsonMatcher jsonMatcher) {
if (expectedValue.decimalValue().compareTo(receivedValue.decimalValue()) != 0) {
return new UnMatchedPrimaryDiff(path, expectedValue, receivedValue);
}
return new MatchedPrimaryDiff(path, expectedValue);
}
@Override
public boolean manage(Path path, JsonNode received, JsonNode expected) {
return expected.isNumber() && received.isNumber();
}
}