Skip to content

Commit 676d536

Browse files
authored
Merge pull request #12 from codeleep/dev_4.x
fix-to
2 parents 5755c33 + 70d4b8d commit 676d536

File tree

33 files changed

+172
-48
lines changed

33 files changed

+172
-48
lines changed

Readme.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
- 高效的
2121
- 精准定位差异
2222
- 轻量级
23-
- 依赖非常干净,只依赖fastJson
23+
- 依赖非常干净,只依赖fastJson,fastjson2,gson,jackson其中之一
24+
25+
目前支持功能
26+
27+
- [x] 支持json-path表示路径
28+
- [x] 支持配置化比较行为。详情见下文
29+
- [x] 支持拓展自定义比较器,用于满足特殊需求
30+
- [x] 支持不同json框架选择。
2431

2532

2633

@@ -39,9 +46,16 @@
3946
<!-- 旧版本可能存在某些缺陷。版本请以maven仓库最版为准。 -->
4047
<version>${version}</version>
4148
</dependency>
49+
50+
<!-- 选择json解析框架。fastjson, fastjson2,gson,jackson 之一 -->
51+
<dependency>
52+
<groupId>cn.xiaoandcai</groupId>
53+
<artifactId>json-diff-impl-fastjson</artifactId>
54+
<version>${version}</version>
55+
</dependency>
4256
```
4357
[版本查看](./VersionHistory.md)
44-
2024-04-11 最新版本:4.0.6-RC1-RELEASE
58+
2024-04-11 最新版本:4.1.1-RC1-RELEASE
4559

4660
```java
4761
/**
@@ -99,10 +113,7 @@ public class UseExample {
99113
100114
工具提供了四个配置,来之对比过程中一些其他的要求。工具还在积极开发中,如果有新的需求,可以给作者提一个issuse。
101115

102-
在开发中。很多时候对比配置一致。可以使用 `JsonDiffOption` 进行开启唯一配置。这样也将获取更好的性能;
103-
```java
104-
105-
```
116+
在开发中。很多时候对比配置一致。可以使用 `JsonDiffOption` 进行开启唯一配置。这样也将获取更好的性能
106117

107118
### 3.进阶
108119

json-diff-common/src/main/java/me/codeleep/jsondiff/common/model/TravelPath.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public TravelPath(TravelPath parentPath, MappingKey mappingKey) {
3131
// 抽象的路径
3232
this.abstractTravelPath = PathUtil.getObjectPath(parentPath.getAbstractTravelPath()) + (mappingKey.getExpectKey() != null ? mappingKey.getExpectKey() : mappingKey.getActualKey());
3333
// 实际遍历的路径
34-
this.actualTravelPath = PathUtil.getObjectPath(parentPath.actualTravelPath) + mappingKey.getActualKey();
34+
this.actualTravelPath = PathUtil.getObjectPath(parentPath.getActualTravelPath()) + mappingKey.getActualKey();
3535
this.expectTravelPath = PathUtil.getObjectPath(parentPath.getExpectTravelPath()) + mappingKey.getExpectKey();
3636
}
3737

3838
public TravelPath(TravelPath parentPath, int expectIndex, int actualIndex) {
3939
// 抽象的路径
40-
this.abstractTravelPath = parentPath.getAbstractTravelPath() + PathUtil.getIndexPath("*");
40+
this.abstractTravelPath = parentPath.getAbstractTravelPath() + PathUtil.getAbstractIndexPath(null);
4141
// 实际遍历的路径
4242
this.actualTravelPath = parentPath.getActualTravelPath() + PathUtil.getIndexPath(String.valueOf(actualIndex));
4343
this.expectTravelPath = parentPath.getExpectTravelPath() + PathUtil.getIndexPath(String.valueOf(expectIndex));

json-diff-common/src/main/java/me/codeleep/jsondiff/common/utils/PathUtil.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ public class PathUtil {
1414
private static final String ARRAY_SING_RIGHT = "]";
1515

1616

17+
public static String getAbstractIndexPath(String index) {
18+
return ARRAY_SING_LEFT + "*" + ARRAY_SING_RIGHT;
19+
}
20+
1721
public static String getIndexPath(String index) {
1822
return ARRAY_SING_LEFT + index + ARRAY_SING_RIGHT;
1923
}
2024

2125
public static String getObjectPath(String parentPath) {
22-
return parentPath.replaceAll("\\[\\d+]", "[*]") + OBJECT_SING;
26+
return parentPath + OBJECT_SING;
2327
}
2428

2529
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
*/
1919
public class AbstractHandleFactory implements HandleFactory{
2020
@Override
21-
public JsonNeat<? extends JsonDiff> generate(JsonDiff actual, JsonDiff expect, TravelPath travelPath) {
21+
public JsonNeat<? extends JsonDiff> generate(JsonDiff expect, JsonDiff actual, TravelPath travelPath) {
2222
if (!ClassUtil.isSameClass(expect, actual)) {
2323
return null;
2424
}
2525
// TODO 返回系统默认处理器
2626
if (expect instanceof JsonDiffObject && actual instanceof JsonDiffObject) {
27-
return new ComplexObjectJsonNeat(travelPath, actual, expect);
27+
return new ComplexObjectJsonNeat(travelPath, expect, actual);
2828
}
2929
if (expect instanceof JsonDiffArray && actual instanceof JsonDiffArray) {
30-
return new ComplexArrayJsonNeat(travelPath, actual, expect);
30+
return new ComplexArrayJsonNeat(travelPath, expect, actual);
3131
}
3232
if (expect.isLeaf() && actual.isLeaf()) {
33-
return new ComplexPrimitiveJsonNeat(travelPath, actual, expect);
33+
return new ComplexPrimitiveJsonNeat(travelPath, expect, actual);
3434
}
35-
return new ComplexOtherJsonNeat(travelPath, actual, expect);
35+
return new ComplexOtherJsonNeat(travelPath, expect, actual);
3636
}
3737
}

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 actual, JsonDiff expect, TravelPath travelPath);
14+
JsonNeat<? extends JsonDiff> generate(JsonDiff expect, JsonDiff actual, TravelPath travelPath);
1515

1616
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public abstract class AbstractArrayJsonNeat<T extends JsonDiffArray> extends Abs
1919

2020
protected final JsonDiffArray expect;
2121

22-
protected AbstractArrayJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
22+
protected AbstractArrayJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
2323
super(travelPath);
24-
if (!(actual instanceof JsonDiffArray) || !(expect instanceof JsonDiffArray)) {
24+
if (!(expect instanceof JsonDiffArray) || !(actual instanceof JsonDiffArray)) {
2525
throw new IllegalArgumentException("Parameter type error, actual and expect must be JsonDiffArray");
2626
}
2727
this.actual = (JsonDiffArray) actual;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
*/
2020
public class ComplexArrayJsonNeat extends AbstractArrayJsonNeat<JsonDiffArray> {
2121

22-
public ComplexArrayJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
23-
super(travelPath, actual, expect);
22+
public ComplexArrayJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
23+
super(travelPath, expect, actual);
2424
}
2525

2626
protected JsonCompareResult ignoreOrder(JsonDiffArray expect, JsonDiffArray actual) {
@@ -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(actual.get(actualIndex), expect.get(expectIndex), nextTravelPath);
42+
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate( expect.get(expectIndex), actual.get(actualIndex), nextTravelPath);
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(actualItem, expectItem, nextTravelPath);
68+
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expectItem, actualItem, nextTravelPath);
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(actualItem, expectItem, nextTravelPath);
96+
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expectItem, actualItem, nextTravelPath);
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
*/
1515
public class AlignArrayJsonDiff extends ComplexArrayJsonNeat {
1616

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

2121
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public abstract class AbstractObjectJsonNeat<T extends JsonDiffObject> extends A
1818

1919
protected final JsonDiffObject expect;
2020

21-
protected AbstractObjectJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
21+
protected AbstractObjectJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
2222
super(travelPath);
23-
if (!(actual instanceof JsonDiffObject) || !(expect instanceof JsonDiffObject)) {
23+
if (!(expect instanceof JsonDiffObject) || !(actual instanceof JsonDiffObject)) {
2424
throw new IllegalArgumentException("Parameter type error, actual and expect must be JsonDiffObject");
2525
}
2626
this.actual = (JsonDiffObject) actual;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public class ComplexObjectJsonNeat extends AbstractObjectJsonNeat<JsonDiffObject
3131
*/
3232
private final List<MappingKey> keyMap = new ArrayList<>();
3333

34-
public ComplexObjectJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
35-
super(travelPath, actual, expect);
34+
public ComplexObjectJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
35+
super(travelPath, expect, actual);
3636
}
3737

3838
/**
@@ -111,7 +111,7 @@ protected JsonCompareResult diff1() {
111111
JsonDiff actualDiffJson = actual.get(mappingKey.getActualKey());
112112
TravelPath nextTravelPath = new TravelPath(travelPath, mappingKey);
113113
// 判断类型, 根据类型去实例化JsonNeat。
114-
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(actualDiffJson, expectDiffJson, nextTravelPath);
114+
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expectDiffJson, actualDiffJson, nextTravelPath);
115115
if (jsonNeat == null) {
116116
Defects defects = new Defects()
117117
.setActual(actualDiffJson)

0 commit comments

Comments
 (0)