Skip to content

Commit 4caea7b

Browse files
committed
Merge branch 'new_model' into multi_frame
2 parents 1961d34 + 82081bf commit 4caea7b

File tree

17 files changed

+157
-42
lines changed

17 files changed

+157
-42
lines changed

Readme.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
<version>${version}</version>
4141
</dependency>
4242
```
43-
[版本查看](https://central.sonatype.com/artifact/cn.xiaoandcai/json-diff/3.0.3-RC1-RELEASE)
44-
2022-03-04 最新版本:3.0.3-RC1-RELEASE
43+
[版本查看](./VersionHistory.md)
44+
2022-04-19 最新版本:3.1.2-RC1-RELEASE
4545

4646
```java
4747
/**
@@ -94,7 +94,6 @@ public class UseExample {
9494
| mapping | Map<String, String> | 将真实字段映射到期望字段,key是真实字段name,value是期望的字段name |
9595
| ignorePath | Set\<String\> | 当对比的路径完全匹配时会被跳过。遇到数组使用 `[]` 即可。无需填入下标 |
9696
| ignoreKey | Set\<String\> | 对比object时。或忽略该key。对整个json生效 |
97-
| customComparator | Map<String, Class\<JsonNeat\>> | 用户自定义比较器。具体说明见下文 |
9897

9998
> `2.0.1-RC1-RELEASE` 之后版本中移除了 `keyFunction` 配置参数。可以使用 `ignorePath` 来代替达到同样的效果。
10099
@@ -183,9 +182,14 @@ ignoreKey.add("high");
183182

184183
它配置的key是一个 travelPath 。具体格式参照 ignorePath 。value 则是一个自定义比较器。对于自定义比较器需要继承对应的抽象类。并且实现具体的抽象接口。具体如下:
185184

185+
> 从 3.1.1-RC1-RELEASE 版本开始,自定义比较器的设置移至 JsonDiffOption.getJsonNeatFactory()
186+
> 不再在原先的Option中设置。如果需要使用自定义比较器。请使用 JsonDiffOption.getJsonNeatFactory().addCustomComparator() 进行设置。
187+
188+
```java
189+
186190
对象比较:
187191

188-
需要继承 `me.codeleep.jsondiff.core.handle.array.AbstractArrayJsonNeat` 并且重写以下方法。
192+
需要继承 `me.codeleep.jsondiff.core.handle.array.AbstractObjectJsonNeat` 并且重写以下方法。
189193

190194
```java
191195
/**
@@ -201,7 +205,7 @@ JsonCompareResult detectDiff(JSONObject expect, JSONObject actual);
201205

202206
数组比较:
203207

204-
需要继承 `me.codeleep.jsondiff.core.handle.object.AbstractObjectJsonNeat` 并且重写以下方法。
208+
需要继承 `me.codeleep.jsondiff.core.handle.object.AbstractArrayJsonNeat` 并且重写以下方法。
205209

206210
```java
207211
/**
@@ -307,7 +311,8 @@ this.result.mergeDefects(diff.getDefectsList());
307311
####
308312

309313
## 三、交流群:710435809
310-
314+
- 如果长时间未通过可发邮件至 codeleep@163.com 与我联系
315+
- 如果觉得对你有帮助,欢迎star。你的start是对我最大的鼓励。
311316

312317

313318

@@ -359,6 +364,7 @@ this.result.mergeDefects(diff.getDefectsList());
359364
"ignorePath": ["root[].a"],
360365
"ignoreKey": ["b"]
361366
}
367+
}
362368
```
363369

364370
"actual": 实际需要对比的值 类型:测试array文件为array 测试object文件为object

VersionHistory.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 版本列表 📃
2+
3+
## 3.X 🌟
4+
5+
- **3.1.2-RC1-RELEASE**
6+
- 修复数组比较数组越界问题
7+
8+
9+
- **3.1.1-RC1-RELEASE**
10+
- 支持自定义比较器添加附加数据
11+
- 支持替换默认比较器
12+
- 更改自定义比较器设置入口为 JsonDiffOption.getJsonNeatFactory()
13+
14+
15+
- **3.0.3-RC1-RELEASE**
16+
- 修复自定义比较器接口范围
17+
- 修复数组对比过程不支持比较长度不相等数组
18+
- 修复mapping映射
19+
- 丰富测试用例
20+
21+
22+
- **3.0.0-RC1-RELEASE**
23+
- 支持自定义比较器
24+
- 支持全局配置
25+
26+
27+
## 2.X 🌟
28+
29+
- **2.0.3-RC1-RELEASE**
30+
- 解决ignorePath无效
31+
- 解决配置清理问题
32+
- 解决大数类型数据
33+
- 支持更详细的路径展示
34+
- 修复对象无法发现多个差异
35+
36+
37+
- **2.0.2-RC1-RELEASE**
38+
- 摒弃1.x 版本代码,将代码完全重构
39+
- 返回信息描述添加统一清洗
40+
- fastjson版本更新到 2.0.7
41+
42+
43+
## 1.X 🌟
44+
45+
- **1.2.0**
46+
- 补充单测
47+
- 代码优化
48+
- 支持不同数组元素是不同类型的比较
49+
50+
51+
- **1.0.0**
52+
- 基本功能支持

json-diff-common/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<artifactId>json-diff-parent</artifactId>
77
<groupId>cn.xiaoandcai</groupId>
8-
<version>3.0.3-RC1-RELEASE</version>
8+
<version>3.1.2-RC1-RELEASE</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>json-diff-common</artifactId>
13-
<version>3.0.3-RC1-RELEASE</version>
13+
<version>3.1.2-RC1-RELEASE</version>
1414

1515
<properties>
1616
<maven.compiler.source>8</maven.compiler.source>

json-diff-core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<artifactId>json-diff-parent</artifactId>
77
<groupId>cn.xiaoandcai</groupId>
8-
<version>3.0.3-RC1-RELEASE</version>
8+
<version>3.1.2-RC1-RELEASE</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>json-diff-core</artifactId>
13-
<version>3.0.3-RC1-RELEASE</version>
13+
<version>3.1.2-RC1-RELEASE</version>
1414

1515
<dependencies>
1616
<dependency>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import me.codeleep.jsondiff.common.model.JsonCompareResult;
66
import me.codeleep.jsondiff.common.model.JsonComparedOption;
77
import me.codeleep.jsondiff.common.model.TravelPath;
8-
import me.codeleep.jsondiff.common.utils.RunTimeDataFactory;
8+
import me.codeleep.jsondiff.core.utils.RunTimeDataFactory;
99
import me.codeleep.jsondiff.core.handle.array.ComplexArrayJsonNeat;
1010
import me.codeleep.jsondiff.core.handle.object.ComplexObjectJsonNeat;
1111

json-diff-common/src/main/java/me/codeleep/jsondiff/common/JsonDiffOption.java renamed to json-diff-core/src/main/java/me/codeleep/jsondiff/core/config/JsonDiffOption.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package me.codeleep.jsondiff.common;
1+
package me.codeleep.jsondiff.core.config;
22

33
import me.codeleep.jsondiff.common.model.JsonComparedOption;
4+
import me.codeleep.jsondiff.core.utils.JsonNeatFactory;
45

56
/**
67
* @author: codeleep
@@ -19,6 +20,15 @@ public class JsonDiffOption {
1920
*/
2021
private static boolean uniqueOption = false;
2122

23+
/**
24+
* 默认的比较器工厂
25+
*/
26+
private final static JsonNeatFactory jsonNeatFactory = new JsonNeatFactory();
27+
28+
public static JsonNeatFactory getJsonNeatFactory() {
29+
return jsonNeatFactory;
30+
}
31+
2232

2333
public static JsonComparedOption getGloballyUniqueOption() {
2434
if (globallyUniqueOption == null) {

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import me.codeleep.jsondiff.common.model.Defects;
88
import me.codeleep.jsondiff.common.model.JsonCompareResult;
99
import me.codeleep.jsondiff.common.model.TravelPath;
10-
import me.codeleep.jsondiff.common.utils.RunTimeDataFactory;
10+
import me.codeleep.jsondiff.core.utils.RunTimeDataFactory;
1111
import me.codeleep.jsondiff.core.handle.AbstractTypeCheck;
1212
import me.codeleep.jsondiff.common.model.neat.ArrayJsonNeat;
1313

@@ -57,20 +57,6 @@ public boolean check(Object expect, Object actual, JsonCompareResult result, Tra
5757
return false;
5858
}
5959

60-
int expectSize = ((JSONArray) expect).size();
61-
int actualSize = ((JSONArray) actual).size();
62-
63-
// 长度不一致
64-
if (expectSize != actualSize) {
65-
Defects defects = new Defects()
66-
.setActual(actualSize)
67-
.setExpect(expectSize)
68-
.setTravelPath(travelPath)
69-
.setIllustrateTemplate(INCONSISTENT_ARRAY_LENGTH, String.valueOf(expectSize), String.valueOf(actualSize));
70-
result.addDefects(defects);
71-
return false;
72-
}
73-
7460
return true;
7561
}
7662

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import me.codeleep.jsondiff.common.model.Defects;
55
import me.codeleep.jsondiff.common.model.JsonCompareResult;
66
import me.codeleep.jsondiff.common.model.TravelPath;
7-
import me.codeleep.jsondiff.common.utils.RunTimeDataFactory;
7+
import me.codeleep.jsondiff.core.utils.RunTimeDataFactory;
88
import me.codeleep.jsondiff.common.model.neat.JsonNeat;
99
import me.codeleep.jsondiff.core.utils.ClassUtil;
1010
import me.codeleep.jsondiff.core.utils.JsonDiffUtil;
1111

1212
import static me.codeleep.jsondiff.common.model.Constant.DATA_TYPE_INCONSISTENT;
13+
import static me.codeleep.jsondiff.common.model.Constant.INCONSISTENT_ARRAY_LENGTH;
1314

1415
/**
1516
* @author: codeleep
@@ -29,6 +30,19 @@ public JsonCompareResult detectDiff(JSONArray expect, JSONArray actual) {
2930
if (!check(expect, actual, result, travelPath)) {
3031
return result;
3132
}
33+
// 长度不一致
34+
int expectSize = ((JSONArray) expect).size();
35+
int actualSize = ((JSONArray) actual).size();
36+
if (expectSize != actualSize) {
37+
Defects defects = new Defects()
38+
.setActual(actualSize)
39+
.setExpect(expectSize)
40+
.setTravelPath(travelPath)
41+
.setIllustrateTemplate(INCONSISTENT_ARRAY_LENGTH, String.valueOf(expectSize), String.valueOf(actualSize));
42+
result.addDefects(defects);
43+
return result;
44+
}
45+
3246
boolean ignoreOrder = RunTimeDataFactory.getOptionInstance().isIgnoreOrder();
3347
// 测试
3448
if (ignoreOrder) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.codeleep.jsondiff.core.handle.custom;
2+
3+
import com.alibaba.fastjson2.JSONArray;
4+
import me.codeleep.jsondiff.common.model.JsonCompareResult;
5+
import me.codeleep.jsondiff.core.handle.array.ComplexArrayJsonNeat;
6+
7+
import java.util.Collections;
8+
9+
/**
10+
* @author: codeleep
11+
* @createTime: 2023/04/25 22:22
12+
* @description: 将补齐不整齐的数组
13+
*/
14+
public class AlignArrayJsonDiff extends ComplexArrayJsonNeat {
15+
16+
/**
17+
* 比较数组.调用入口。需要自己去分别调用 ignoreOrder 和 keepOrder。
18+
* @param expect 期望的json对象
19+
* @param actual 实际的json对象
20+
* @return 返回比较结果
21+
*/
22+
@Override
23+
public JsonCompareResult detectDiff(JSONArray expect, JSONArray actual) {
24+
JsonCompareResult result = new JsonCompareResult();
25+
// 前置校验失败
26+
if (!check(expect, actual, result, travelPath)) {
27+
return result;
28+
}
29+
// 长度不一致
30+
int expectSize = ((JSONArray) expect).size();
31+
int actualSize = ((JSONArray) actual).size();
32+
if (expectSize == actualSize) {
33+
return super.detectDiff(expect, actual);
34+
}
35+
JSONArray expectIts = new JSONArray(expect);
36+
JSONArray actualIts = new JSONArray(actual);
37+
// 让期望的数组长度和实际的数组长度一致。谁短补齐谁
38+
if (expectSize > actualSize) {
39+
actualIts.addAll(Collections.nCopies(expectSize - actualSize, null));
40+
} else {
41+
expectIts.addAll(Collections.nCopies(actualSize - expectSize, null));
42+
}
43+
return super.detectDiff(expectIts, actualIts);
44+
}
45+
46+
47+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import me.codeleep.jsondiff.common.exception.JsonDiffException;
77
import me.codeleep.jsondiff.common.model.JsonCompareResult;
88
import me.codeleep.jsondiff.common.model.TravelPath;
9-
import me.codeleep.jsondiff.common.utils.RunTimeDataFactory;
9+
import me.codeleep.jsondiff.core.utils.RunTimeDataFactory;
1010
import me.codeleep.jsondiff.core.handle.AbstractTypeCheck;
1111
import me.codeleep.jsondiff.common.model.neat.ObjectJsonNeat;
1212

0 commit comments

Comments
 (0)