-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonCompare.java
115 lines (102 loc) · 4.18 KB
/
JsonCompare.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package algorithm.topics.unclassified;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* @author marvin
* @version JsonCompare.java, v 0.1 2022/04/13 22:06 Exp $
*/
public class JsonCompare {
public static void main(String[] args) {
JSONObject left = new JSONObject();
left.put("a", 1);
JSONObject right = new JSONObject();
right.put("a", 2);
DiffResult diffResult = compareJson(left, right);
System.out.println(diffResult.diffJson);
System.out.println(diffResult.missingJson);
}
public static DiffResult compareJson(JSONObject left, JSONObject right) {
Map<String, Object> leftMap = new HashMap<>();
Map<String, Object> rightMap = new HashMap<>();
Map<String, Object> diffMap = new HashMap<>();
Map<String, Object> missingMap = new HashMap<>();
convertJsonToMap(left, "", leftMap);
convertJsonToMap(right, "", rightMap);
diff(leftMap, rightMap, diffMap, missingMap);
DiffResult diffResult = new DiffResult();
if (diffMap.isEmpty() && missingMap.isEmpty()) {
diffResult.success = true;
} else {
diffResult.success = false;
if (!diffMap.isEmpty()) {
diffResult.diffMap = diffMap;
diffResult.diffJson = JSON.toJSONString(diffMap, SerializerFeature.WriteMapNullValue,
SerializerFeature.MapSortField);
}
if (!missingMap.isEmpty()) {
diffResult.missingMap = missingMap;
diffResult.missingJson = JSON.toJSONString(missingMap, SerializerFeature.WriteMapNullValue,
SerializerFeature.MapSortField);
}
}
return diffResult;
}
public static class DiffResult {
public boolean success;
public Map<String, Object> diffMap;
public Map<String, Object> missingMap;
public String diffJson;
public String missingJson;
}
public static void convertJsonToMap(JSON json, String root, Map<String, Object> resultMap) {
if (json instanceof JSONObject) {
JSONObject jsonObject = ((JSONObject) json);
jsonObject.forEach((key, value) -> {
String path = "".equals(root) ? key + "" : root + "." + key;
if (value != null && JSON.class.isAssignableFrom(value.getClass())) {
convertJsonToMap((JSON) value, path, resultMap);
} else {
resultMap.put(path, value);
}
});
} else if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
for (int i = 0; i < jsonArray.size(); i++) {
Object value = jsonArray.get(i);
String path = "".equals(root) ? "[" + i + "]" : root + ".[" + i + "]";
if (value != null && JSON.class.isAssignableFrom(value.getClass())) {
convertJsonToMap((JSON) value, path, resultMap);
} else {
resultMap.put(path, value);
}
}
}
}
public static void diff(Map<String, Object> leftMap, Map<String, Object> rightMap, Map<String, Object> diffMap,
Map<String, Object> missingMap) {
leftMap.forEach((key, leftVal) -> {
if (rightMap.containsKey(key)) {
Object rightVal = rightMap.get(key);
if (!Objects.equals(leftVal, rightVal)) {
Map<String, Object> diffContent = new HashMap<>();
diffContent.put("L", leftVal);
diffContent.put("R", rightVal);
diffMap.put(key, diffContent);
}
rightMap.remove(key);
} else {
missingMap.put(key, "R");
}
});
if (!rightMap.isEmpty()) {
rightMap.forEach((key, rightVal) -> {
missingMap.put(key, "L");
});
}
}
}