0%

比较两对象属性差异性

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

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject

class Diff {

static Map<String, Object> equals(Object source, Object target) {
Map<String, Object> diffMap = new HashMap<>()
JSONObject sourceJson = source ? JSON.toJSON(source) as JSONObject : new JSONObject()
JSONObject targetJson = target ? JSON.toJSON(target) as JSONObject : new JSONObject()
diffMap.putAll(equalsJsonObject(sourceJson, targetJson, null))
return diffMap.sort()
}

static Map<String, Object> equals(Object source, Object target, List<Pattern> regexIgnoreFiled) {
Map<String, Object> diffMap = equals(source, target)
if (regexIgnoreFiled) {
diffMap = diffMap.findAll { entry -> !regexIgnoreFiled.find { regex -> entry.key ==~ regex } }
}
return diffMap.sort()
}

private static Map<String, Object> equalsJsonObject(JSONObject sourceJson, JSONObject targetJson, String key) {
Map<String, Object> diffMap = new HashMap<>()
Set<Map.Entry<String, Object>> sourceEntrySet = sourceJson.entrySet()
Set<Map.Entry<String, Object>> targetEntrySet = targetJson.entrySet()
Set<Map.Entry<String, Object>> baseEntrySet = sourceEntrySet.size() > targetEntrySet.size() ? sourceEntrySet : targetEntrySet
baseEntrySet.each { entry ->
String diffKey = key ? "${key}.${entry.key}" : entry.key
Object sourceValue = sourceJson.get(entry.key)
Object targetValue = targetJson.get(entry.key)
Object value = entry.value ?: sourceValue ?: targetValue
if (value instanceof JSONObject) {
diffMap.putAll(equalsJsonObject(sourceValue as JSONObject ?: new JSONObject(), targetValue as JSONObject ?: new JSONObject(), diffKey))
} else if (value instanceof JSONArray) {
diffMap.putAll(equalsJsonArray(sourceValue as JSONArray ?: new JSONArray(), targetValue as JSONArray ?: new JSONArray(), diffKey))
} else {
if ((sourceValue || targetValue) && sourceValue != targetValue) {
diffMap.put(diffKey, "(${sourceValue != null ? sourceValue : ""}) -> (${targetValue != null ? targetValue : ""})".toString())
}
}
}
return diffMap
}

private static Map<String, Object> equalsJsonArray(JSONArray sourceJson, JSONArray targetJson, String key) {
Map<String, Object> diffMap = new HashMap<>()
int minSize = Math.min(sourceJson.size(), targetJson.size())
int maxSize = Math.max(sourceJson.size(), targetJson.size())
for (int i = 0; i < minSize; i++) {
Object sourceValue = sourceJson.get(i)
Object targetValue = targetJson.get(i)
String diffKey = "${key}[${i}]"
if (sourceValue instanceof JSONObject) {
diffMap.putAll(equalsJsonObject(sourceValue as JSONObject ?: new JSONObject(), targetValue as JSONObject ?: new JSONObject(), diffKey))
} else if (sourceValue instanceof JSONArray) {
diffMap.putAll(equalsJsonArray(sourceValue as JSONArray ?: new JSONArray(), targetValue as JSONArray ?: new JSONArray(), diffKey))
} else if (sourceValue != targetValue) {
diffMap.put(diffKey, "(${sourceValue}) -> (${targetValue})".toString())
}
}
if (minSize != maxSize) {
JSONArray baseJson = sourceJson.size() > targetJson.size() ? sourceJson : targetJson
boolean delete = sourceJson.size() > targetJson.size()
for (int i = minSize; i < maxSize; i++) {
Object value = baseJson.get(i)
String diffKey = "${key}[${i}]"
if (value instanceof JSONObject) {
diffMap.putAll(equalsJsonObject(
delete ? value as JSONObject : new JSONObject(),
!delete ? value as JSONObject : new JSONObject(),
diffKey))
} else if (value instanceof JSONArray) {
diffMap.putAll(equalsJsonArray(
delete ? value as JSONArray : new JSONArray(),
!delete ? value as JSONArray : new JSONArray(),
diffKey))
} else {
diffMap.put(diffKey, "(${delete ? value : ""}) -> (${!delete ? value : ""})".toString())
}
}
}
return diffMap
}

}

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
static void main(String[] args) {
User source = new User(
userId: "1-userId",
userName: "1-userName",
school: new School(
name: "1-name",
level: "1-level",
),
addresses: [
new Address(
city: "1-city",
street: "1-street"
),
new Address(
city: "1-1-city",
street: "1-1-street"
)
],
families: [
[
new Family(
member: "1-member",
ship: "1-ship"
),
new Family(
member: "1-1-member",
ship: "1-1-ship"
)
],
[
new Family(
member: "1-1-1-member",
ship: "1-1-1-ship"
),
new Family(
member: "1-1-1-1-member",
ship: "1-1-1-1-ship"
)
]
],
friends: ["2", "2-2-2", "1"]
)

User target = new User(
userId: "1-userId",
userName: "1-userName",
school: new School(
name: "2-name",
level: "2-level",
),
addresses: [
new Address(
city: "1-city",
street: "1-street"
),
new Address(
city: "1-1-city",
street: "2-2-street"
)
],
families: [
[
new Family(
member: "2-member",
ship: "2-ship"
),
new Family(
member: "2-2-member",
ship: "2-2-ship"
)
],
[
new Family(
member: "2-2-2-member",
ship: "2-2-2-ship"
)
]
],
friends: ["2", "2-2", "2-2-2"]
)
print(JSON.toJSONString(Diff.equals(target,source)))
}


static class User {
String userId
String userName
School school
List<Address> addresses
List<List<Family>> families
List<String> friends
}

static class Address {
String city
String street
}

static class School {
String name
String level
}

static class Family {
String member
String ship
}