Skip to content

Commit c9e09bb

Browse files
committed
docs(readme): update readme
1 parent 1da2b19 commit c9e09bb

File tree

1 file changed

+10
-96
lines changed

1 file changed

+10
-96
lines changed

Readme.md

Lines changed: 10 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</dependency>
4242
```
4343
[版本查看](./VersionHistory.md)
44-
2022-04-19 最新版本:3.1.2-RC1-RELEASE
44+
2024-04-11 最新版本:4.0.6-RC1-RELEASE
4545

4646
```java
4747
/**
@@ -58,7 +58,7 @@ public class UseExample {
5858
JsonComparedOption jsonComparedOption = new JsonComparedOption().setIgnoreOrder(true);
5959
JsonCompareResult jsonCompareResult = new DefaultJsonDifference()
6060
.option(jsonComparedOption)
61-
.detectDiff(JSON.parseArray(array1), JSON.parseArray(array2));
61+
.detectDiff(array1, array2);
6262
System.out.println(JSON.toJSONString(jsonCompareResult));
6363
}
6464
}
@@ -178,105 +178,19 @@ ignoreKey.add("high");
178178

179179
#### 3.5 自定义比较器
180180

181-
在我们一个大json文件下。可能遇到某些节点希望实现自定义比较。可以通过 `customComparator` 来进行实现。
181+
在某些特俗场景下,我们需要自己实现比较器。这时可以通过 `me.codeleep.jsondiff.core.handle.HandleFactory` 来进行实现。可以参考 `me.codeleep.jsondiff.core.handle.AbstractHandleFactory` 实现
182182

183-
它配置的key是一个 travelPath 。具体格式参照 ignorePath 。value 则是一个自定义比较器。对于自定义比较器需要继承对应的抽象类。并且实现具体的抽象接口。具体如下:
183+
将我们的实现设置到JsonComparedOption中即可生效。
184184

185-
> 从 3.1.1-RC1-RELEASE 版本开始,自定义比较器的设置移至 JsonDiffOption.getJsonNeatFactory()
186-
> 不再在原先的Option中设置。如果需要使用自定义比较器。请使用 JsonDiffOption.getJsonNeatFactory().addCustomComparator() 进行设置。
185+
对于各个类型的比较器可以尝试继承系统比较器来进行使用:
187186

188-
```java
189-
190-
对象比较:
191-
192-
需要继承 `me.codeleep.jsondiff.core.handle.array.AbstractObjectJsonNeat` 并且重写以下方法。
193-
194-
```java
195-
/**
196-
* 比较对象
197-
* @param expect 期望的json对象
198-
* @param actual 实际的json对象
199-
* @return 返回比较结果
200-
* @throws IllegalAccessException 发生异常直接抛出
201-
*/
202-
JsonCompareResult detectDiff(JSONObject expect, JSONObject actual);
203-
204-
```
205-
206-
数组比较:
207-
208-
需要继承 `me.codeleep.jsondiff.core.handle.object.AbstractArrayJsonNeat` 并且重写以下方法。
209-
210-
```java
211-
/**
212-
* 比较数组.调用入口。需要自己去分别调用 ignoreOrder 和 keepOrder。
213-
* @param expect 期望的json对象
214-
* @param actual 实际的json对象
215-
* @return 返回比较结果
216-
*/
217-
JsonCompareResult detectDiff(JSONArray expect, JSONArray actual);
218-
219-
// 忽略顺序的比较
220-
JsonCompareResult ignoreOrder(JSONArray expect, JSONArray actual);
221-
222-
// 保持顺序比较
223-
JsonCompareResult keepOrder(JSONArray expect, JSONArray actual);
224-
225-
```
226-
227-
基本类型比较:
228-
229-
基本类型指的是java基础类型的包装类型以及Number的实现类型。
230-
231-
需要继承 `me.codeleep.jsondiff.core.handle.primitive.AbstractPrimitiveJsonNeat` 并且重写以下方法。
232-
233-
```java
234-
/**
235-
* 比较数组
236-
* @param expect 基础类型对象
237-
* @param actual 基础类型对象
238-
* @return 返回比较结果
239-
*/
240-
JsonCompareResult detectDiff(Object expect, Object actual);
241-
```
242-
243-
244-
245-
用户可以自己根据 travelPath 来决定使用何种自定义比较。三种比较器都返回 JsonCompareResult 对象作为当前节点的比较结果。对于JsonCompareResult对象。需要填入以下信息:
246-
247-
```java
248-
// 示例
249-
JsonCompareResult result = new JsonCompareResult();
250-
Defects defects = new Defects()
251-
.setActual(actualDiffJson)
252-
.setExpect(expectDiffJson)
253-
.setTravelPath(nextTravelPath)
254-
.setIllustrateTemplate(DATA_TYPE_INCONSISTENT, ClassUtil.getClassName(expectDiffJson), ClassUtil.getClassName(actualDiffJson));
255-
result.addDefects(defects);
256-
```
257-
258-
259-
260-
如果遇到在自定义节点中,还需要使用系统自带的比较器时。
261-
262-
```java
263-
// 该值可以在上述三个抽象类中获得。但需要经自行处理
264-
String abstractTravelPath = "root";
265-
// 下一级是对象
266-
TravelPath nextTravelPath = new TravelPath(abstractTravelPath, mappingKey);
267-
// 下一级是数组
268-
TravelPath nextTravelPath = new TravelPath(abstractTravelPath, expectIndex, actualIndex);
269-
// 获得比较器
270-
JsonDiffUtil.getJsonNeat(expectDiffJson, actualDiffJson, nextTravelPath);
271-
// 执行比较获得结果
272-
JsonCompareResult diff = jsonNeat.diff(expectDiffJson, actualDiffJson, nextTravelPath);
273-
// 本级创建的 JsonCompareResult result 将下一级结果合并
274-
this.result.mergeDefects(diff.getDefectsList());
275-
```
187+
- me.codeleep.jsondiff.core.handle.array.ComplexArrayJsonNeat
188+
- me.codeleep.jsondiff.core.handle.object.ComplexObjectJsonNeat
189+
- me.codeleep.jsondiff.core.handle.other.ComplexOtherJsonNeat
190+
- me.codeleep.jsondiff.core.handle.primitive.ComplexPrimitiveJsonNeat
276191

277-
可以使用上述代码获取系统自带的比较器
192+
在工具中,目前提供了一个 me.codeleep.jsondiff.core.handle.custom.AlignArrayJsonDiff 来实现当数组长度不一致时,进行补齐,从而避免直接返回错误信息
278193

279-
> 自定义比较器值得注意的是: 从匹配到 travelPath 之后,根据不再接管比较操作。一切行为由用户自行定义。但工具依然预留默认的比较器给用户处理后续字段。这需要用户自行进行组合调用。
280194

281195

282196

0 commit comments

Comments
 (0)