Skip to content

Commit dfdb352

Browse files
committed
fix-to: bugfix
1. 调整参数列表 2. 修复null属性判断错误 3. 优化handleFactory实现方法
1 parent 2900811 commit dfdb352

File tree

21 files changed

+72
-44
lines changed

21 files changed

+72
-44
lines changed
Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package me.codeleep.jsondiff.core.handle;
22

33
import me.codeleep.jsondiff.common.model.TravelPath;
4-
import me.codeleep.jsondiff.common.model.neat.JsonDiff;
5-
import me.codeleep.jsondiff.common.model.neat.JsonDiffArray;
6-
import me.codeleep.jsondiff.common.model.neat.JsonDiffObject;
7-
import me.codeleep.jsondiff.common.model.neat.JsonNeat;
4+
import me.codeleep.jsondiff.common.model.neat.*;
5+
import me.codeleep.jsondiff.core.handle.array.AbstractArrayJsonNeat;
86
import me.codeleep.jsondiff.core.handle.array.ComplexArrayJsonNeat;
7+
import me.codeleep.jsondiff.core.handle.object.AbstractObjectJsonNeat;
98
import me.codeleep.jsondiff.core.handle.object.ComplexObjectJsonNeat;
9+
import me.codeleep.jsondiff.core.handle.other.AbstractOtherJsonNeat;
1010
import me.codeleep.jsondiff.core.handle.other.ComplexOtherJsonNeat;
11+
import me.codeleep.jsondiff.core.handle.primitive.AbstractPrimitiveJsonNeat;
1112
import me.codeleep.jsondiff.core.handle.primitive.ComplexPrimitiveJsonNeat;
1213
import me.codeleep.jsondiff.core.utils.ClassUtil;
1314

@@ -18,20 +19,37 @@
1819
*/
1920
public class AbstractHandleFactory implements HandleFactory{
2021
@Override
21-
public JsonNeat<? extends JsonDiff> generate(JsonDiff expect, JsonDiff actual, TravelPath travelPath) {
22+
public JsonNeat<? extends JsonDiff> generate(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
2223
if (!ClassUtil.isSameClass(expect, actual)) {
2324
return null;
2425
}
2526
// TODO 返回系统默认处理器
2627
if (expect instanceof JsonDiffObject && actual instanceof JsonDiffObject) {
27-
return new ComplexObjectJsonNeat(travelPath, expect, actual);
28+
return generateObjectJsonNeat(travelPath, expect, actual);
2829
}
2930
if (expect instanceof JsonDiffArray && actual instanceof JsonDiffArray) {
30-
return new ComplexArrayJsonNeat(travelPath, expect, actual);
31+
return generateArrayJsonNeat(travelPath, expect, actual);
3132
}
3233
if (expect.isLeaf() && actual.isLeaf()) {
33-
return new ComplexPrimitiveJsonNeat(travelPath, expect, actual);
34+
return generatePrimitiveJsonNeat(travelPath, expect, actual);
3435
}
36+
return generateOtherJsonNeat(travelPath, expect, actual);
37+
}
38+
39+
public AbstractObjectJsonNeat<JsonDiffObject> generateObjectJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
40+
return new ComplexObjectJsonNeat(travelPath, expect, actual);
41+
}
42+
43+
public AbstractArrayJsonNeat<JsonDiffArray> generateArrayJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
44+
return new ComplexArrayJsonNeat(travelPath, expect, actual);
45+
}
46+
47+
public AbstractPrimitiveJsonNeat<JsonDiffPrimitive> generatePrimitiveJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
48+
return new ComplexPrimitiveJsonNeat(travelPath, expect, actual);
49+
}
50+
51+
public AbstractOtherJsonNeat<JsonDiffOther> generateOtherJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
3552
return new ComplexOtherJsonNeat(travelPath, expect, actual);
3653
}
54+
3755
}

json-diff-core/src/main/java/me/codeleep/jsondiff/core/handle/HandleFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
*/
1212
public interface HandleFactory {
1313

14-
JsonNeat<? extends JsonDiff> generate(JsonDiff expect, JsonDiff actual, TravelPath travelPath);
14+
JsonNeat<? extends JsonDiff> generate(TravelPath travelPath, JsonDiff expect, JsonDiff actual);
1515

1616
}

json-diff-core/src/main/java/me/codeleep/jsondiff/core/handle/array/ComplexArrayJsonNeat.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected JsonCompareResult ignoreOrder(JsonDiffArray expect, JsonDiffArray actu
3939
continue;
4040
}
4141
TravelPath nextTravelPath = new TravelPath(this.travelPath, expectIndex, actualIndex);
42-
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expect.get(expectIndex), actual.get(actualIndex), nextTravelPath);
42+
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(nextTravelPath, expect.get(expectIndex), actual.get(actualIndex));
4343
if (jsonNeat == null) {
4444
continue;
4545
}
@@ -65,7 +65,7 @@ protected JsonCompareResult ignoreOrder(JsonDiffArray expect, JsonDiffArray actu
6565
JsonDiff actualItem = actual.get(actualIndex);
6666
TravelPath nextTravelPath = new TravelPath(this.travelPath, expectIndex, actualIndex);
6767
// 判断类型, 根据类型去实例化JsonNeat。
68-
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expectItem, actualItem, nextTravelPath);
68+
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(nextTravelPath, expectItem, actualItem);
6969
// 类型不一致
7070
if (jsonNeat != null) {
7171
JsonCompareResult diff = jsonNeat.diff();
@@ -93,7 +93,7 @@ protected JsonCompareResult keepOrder(JsonDiffArray expect, JsonDiffArray actual
9393
JsonDiff actualItem = actual.get(i);
9494
TravelPath nextTravelPath = new TravelPath(this.travelPath, i, i);
9595
// 判断类型, 根据类型去实例化JsonNeat。
96-
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expectItem, actualItem, nextTravelPath);
96+
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(nextTravelPath, expectItem, actualItem);
9797
// 类型不一致
9898
if (jsonNeat == null) {
9999
Defects defects = new Defects()

json-diff-core/src/main/java/me/codeleep/jsondiff/core/handle/custom/AlignArrayJsonDiff.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
public class AlignArrayJsonDiff extends ComplexArrayJsonNeat {
1616

17-
protected AlignArrayJsonDiff(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
17+
public AlignArrayJsonDiff(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
1818
super(travelPath, expect, actual);
1919
}
2020

json-diff-core/src/main/java/me/codeleep/jsondiff/core/handle/object/ComplexObjectJsonNeat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected JsonCompareResult diff1() {
122122
JsonDiff actualDiffJson = actual.get(mappingKey.getActualKey());
123123
TravelPath nextTravelPath = new TravelPath(travelPath, mappingKey);
124124
// 判断类型, 根据类型去实例化JsonNeat。
125-
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expectDiffJson, actualDiffJson, nextTravelPath);
125+
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(nextTravelPath, expectDiffJson, actualDiffJson);
126126
if (jsonNeat == null) {
127127
Defects defects = new Defects()
128128
.setActual(actualDiffJson)

json-diff-impl/json-diff-impl-fastjson/src/main/java/me/codeleep/jsondiff/impl/fastjson/FastJsonOther.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public Object getOther() {
2222

2323
@Override
2424
public boolean isEquals(JsonDiffOther jsonDiffOther) {
25-
if (jsonDiffOther == null && object == null) {
25+
if (jsonDiffOther != null && jsonDiffOther.getOther() == null && object == null) {
2626
return true;
2727
}
28-
if (jsonDiffOther == null) {
28+
if (jsonDiffOther == null || jsonDiffOther.getOther() == null) {
2929
return false;
3030
}
3131
Object target = jsonDiffOther.getOther();

json-diff-impl/json-diff-impl-fastjson/src/main/java/me/codeleep/jsondiff/impl/fastjson/FastJsonPrimitive.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public FastJsonPrimitive(Object object) {
1717

1818
@Override
1919
public boolean isEquals(JsonDiffPrimitive jsonDiffPrimitive) {
20-
if (jsonDiffPrimitive == null && object == null) {
20+
if (jsonDiffPrimitive != null && jsonDiffPrimitive.getTarget() == null && object == null) {
2121
return true;
2222
}
23-
if (jsonDiffPrimitive == null) {
23+
if (jsonDiffPrimitive == null || jsonDiffPrimitive.getTarget() == null) {
2424
return false;
2525
}
2626
Object target = jsonDiffPrimitive.getTarget();

json-diff-impl/json-diff-impl-fastjson2/src/main/java/me/codeleep/jsondiff/impl/fastjson2/FastJson2Other.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public Object getOther() {
2323

2424
@Override
2525
public boolean isEquals(JsonDiffOther jsonDiffOther) {
26-
if (jsonDiffOther == null && object == null) {
26+
if (jsonDiffOther != null && jsonDiffOther.getOther() == null && object == null) {
2727
return true;
2828
}
29-
if (jsonDiffOther == null) {
29+
if (jsonDiffOther == null || jsonDiffOther.getOther() == null) {
3030
return false;
3131
}
3232
Object target = jsonDiffOther.getOther();

json-diff-impl/json-diff-impl-fastjson2/src/main/java/me/codeleep/jsondiff/impl/fastjson2/FastJson2Primitive.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public FastJson2Primitive(Object object) {
1717

1818
@Override
1919
public boolean isEquals(JsonDiffPrimitive jsonDiffPrimitive) {
20-
if (jsonDiffPrimitive == null && object == null) {
20+
if (jsonDiffPrimitive != null && jsonDiffPrimitive.getTarget() == null && object == null) {
2121
return true;
2222
}
23-
if (jsonDiffPrimitive == null) {
23+
if (jsonDiffPrimitive == null || jsonDiffPrimitive.getTarget() == null) {
2424
return false;
2525
}
2626
Object target = jsonDiffPrimitive.getTarget();

json-diff-impl/json-diff-impl-fastjson2/src/main/java/me/codeleep/jsondiff/impl/fastjson2/FastJson2Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static JsonDiff formatJsonDiff(Object value) {
2828
}
2929

3030
public static boolean isJavaPrimitive(Object value) {
31-
return value instanceof String || value instanceof Integer || value instanceof BigInteger || value instanceof BigDecimal || value instanceof Boolean || value instanceof Double || value instanceof Float || value instanceof Long || value instanceof Short || value instanceof Byte || value instanceof Character;
31+
return value == null || value instanceof String || value instanceof Integer || value instanceof BigInteger || value instanceof BigDecimal || value instanceof Boolean || value instanceof Double || value instanceof Float || value instanceof Long || value instanceof Short || value instanceof Byte || value instanceof Character;
3232
}
3333

3434
}

0 commit comments

Comments
 (0)