-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJsonObjectDiff.java
More file actions
89 lines (71 loc) · 3.18 KB
/
JsonObjectDiff.java
File metadata and controls
89 lines (71 loc) · 3.18 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.deblock.jsondiff.diff;
import com.deblock.jsondiff.matcher.Path;
import com.deblock.jsondiff.viewer.JsonDiffViewer;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.node.ObjectNode;
import java.util.HashMap;
import java.util.Map;
public class JsonObjectDiff implements JsonDiff {
private static final int STRUCTURE_MAX_RATIO = 60;
private static final int VALUE_MAX_RATIO = 40;
private final Map<String, JsonDiff> propertiesDiff = new HashMap<>();
private final Map<String, JsonNode> notFoundProperties = new HashMap<>();
private final Map<String, JsonNode> extraProperties = new HashMap<>();
private final Path path;
public JsonObjectDiff(Path path) {
this.path = path;
}
public void addNotFoundProperty(String propertyName, JsonNode value) {
notFoundProperties.put(propertyName, value);
}
public void addExtraProperty(String propertyName, JsonNode value) {
extraProperties.put(propertyName, value);
}
public void addPropertyDiff(String propertyName, JsonDiff diff) {
propertiesDiff.put(propertyName, diff);
}
@Override
public double similarityRate() {
final var notFoundPropertiesCount = notFoundProperties.keySet().size();
final var unexpectedPropertiesCount = extraProperties.keySet().size();
final var totalPropertiesCount = propertiesDiff.keySet().size() + notFoundPropertiesCount + unexpectedPropertiesCount;
if (totalPropertiesCount == 0) {
return 100;
}
final var propertiesSimilarityRate = propertiesDiff.values().stream()
.mapToDouble(JsonDiff::similarityRate)
.sum();
final var structureRatio = (totalPropertiesCount - notFoundPropertiesCount - unexpectedPropertiesCount) * STRUCTURE_MAX_RATIO / totalPropertiesCount;
final double equalityRatio;
if (propertiesDiff.isEmpty()) {
equalityRatio = 0;
} else {
equalityRatio = propertiesSimilarityRate * VALUE_MAX_RATIO / (totalPropertiesCount * 100);
}
return structureRatio + equalityRatio;
}
@Override
public void display(JsonDiffViewer viewer) {
for (final var entry : notFoundProperties.entrySet()) {
viewer.missingProperty(this.path.add(Path.PathItem.of(entry.getKey())), entry.getValue());
}
for (final var entry : propertiesDiff.entrySet()) {
if (entry.getValue().similarityRate() >= 100) {
viewer.matchingProperty(path().add(Path.PathItem.of(entry.getKey())), entry.getValue());
} else {
viewer.nonMatchingProperty(path().add(Path.PathItem.of(entry.getKey())), entry.getValue());
}
}
for (final var entry: extraProperties.entrySet()) {
viewer.extraProperty(path().add(Path.PathItem.of(entry.getKey())), entry.getValue());
}
final var isEmptyObject = notFoundProperties.isEmpty() && propertiesDiff.isEmpty() && extraProperties.isEmpty();
if (isEmptyObject) {
viewer.primaryMatching(path(), new ObjectNode(null));
}
}
@Override
public Path path() {
return this.path;
}
}