Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
- 高效的
- 精准定位差异
- 轻量级
- 依赖非常干净,只依赖fastJson
- 依赖非常干净,只依赖fastJson,fastjson2,gson,jackson其中之一

目前支持功能

- [x] 支持json-path表示路径
- [x] 支持配置化比较行为。详情见下文
- [x] 支持拓展自定义比较器,用于满足特殊需求
- [x] 支持不同json框架选择。



Expand All @@ -39,9 +46,16 @@
<!-- 旧版本可能存在某些缺陷。版本请以maven仓库最版为准。 -->
<version>${version}</version>
</dependency>

<!-- 选择json解析框架。fastjson, fastjson2,gson,jackson 之一 -->
<dependency>
<groupId>cn.xiaoandcai</groupId>
<artifactId>json-diff-impl-fastjson</artifactId>
<version>${version}</version>
</dependency>
```
[版本查看](./VersionHistory.md)
2024-04-11 最新版本:4.0.6-RC1-RELEASE
2024-04-11 最新版本:4.1.1-RC1-RELEASE

```java
/**
Expand Down Expand Up @@ -99,10 +113,7 @@ public class UseExample {

工具提供了四个配置,来之对比过程中一些其他的要求。工具还在积极开发中,如果有新的需求,可以给作者提一个issuse。

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

```
在开发中。很多时候对比配置一致。可以使用 `JsonDiffOption` 进行开启唯一配置。这样也将获取更好的性能

### 3.进阶

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public TravelPath(TravelPath parentPath, MappingKey mappingKey) {
// 抽象的路径
this.abstractTravelPath = PathUtil.getObjectPath(parentPath.getAbstractTravelPath()) + (mappingKey.getExpectKey() != null ? mappingKey.getExpectKey() : mappingKey.getActualKey());
// 实际遍历的路径
this.actualTravelPath = PathUtil.getObjectPath(parentPath.actualTravelPath) + mappingKey.getActualKey();
this.actualTravelPath = PathUtil.getObjectPath(parentPath.getActualTravelPath()) + mappingKey.getActualKey();
this.expectTravelPath = PathUtil.getObjectPath(parentPath.getExpectTravelPath()) + mappingKey.getExpectKey();
}

public TravelPath(TravelPath parentPath, int expectIndex, int actualIndex) {
// 抽象的路径
this.abstractTravelPath = parentPath.getAbstractTravelPath() + PathUtil.getIndexPath("*");
this.abstractTravelPath = parentPath.getAbstractTravelPath() + PathUtil.getAbstractIndexPath(null);
// 实际遍历的路径
this.actualTravelPath = parentPath.getActualTravelPath() + PathUtil.getIndexPath(String.valueOf(actualIndex));
this.expectTravelPath = parentPath.getExpectTravelPath() + PathUtil.getIndexPath(String.valueOf(expectIndex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ public class PathUtil {
private static final String ARRAY_SING_RIGHT = "]";


public static String getAbstractIndexPath(String index) {
return ARRAY_SING_LEFT + "*" + ARRAY_SING_RIGHT;
}

public static String getIndexPath(String index) {
return ARRAY_SING_LEFT + index + ARRAY_SING_RIGHT;
}

public static String getObjectPath(String parentPath) {
return parentPath.replaceAll("\\[\\d+]", "[*]") + OBJECT_SING;
return parentPath + OBJECT_SING;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
*/
public class AbstractHandleFactory implements HandleFactory{
@Override
public JsonNeat<? extends JsonDiff> generate(JsonDiff actual, JsonDiff expect, TravelPath travelPath) {
public JsonNeat<? extends JsonDiff> generate(JsonDiff expect, JsonDiff actual, TravelPath travelPath) {
if (!ClassUtil.isSameClass(expect, actual)) {
return null;
}
// TODO 返回系统默认处理器
if (expect instanceof JsonDiffObject && actual instanceof JsonDiffObject) {
return new ComplexObjectJsonNeat(travelPath, actual, expect);
return new ComplexObjectJsonNeat(travelPath, expect, actual);
}
if (expect instanceof JsonDiffArray && actual instanceof JsonDiffArray) {
return new ComplexArrayJsonNeat(travelPath, actual, expect);
return new ComplexArrayJsonNeat(travelPath, expect, actual);
}
if (expect.isLeaf() && actual.isLeaf()) {
return new ComplexPrimitiveJsonNeat(travelPath, actual, expect);
return new ComplexPrimitiveJsonNeat(travelPath, expect, actual);
}
return new ComplexOtherJsonNeat(travelPath, actual, expect);
return new ComplexOtherJsonNeat(travelPath, expect, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
*/
public interface HandleFactory {

JsonNeat<? extends JsonDiff> generate(JsonDiff actual, JsonDiff expect, TravelPath travelPath);
JsonNeat<? extends JsonDiff> generate(JsonDiff expect, JsonDiff actual, TravelPath travelPath);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public abstract class AbstractArrayJsonNeat<T extends JsonDiffArray> extends Abs

protected final JsonDiffArray expect;

protected AbstractArrayJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
protected AbstractArrayJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
super(travelPath);
if (!(actual instanceof JsonDiffArray) || !(expect instanceof JsonDiffArray)) {
if (!(expect instanceof JsonDiffArray) || !(actual instanceof JsonDiffArray)) {
throw new IllegalArgumentException("Parameter type error, actual and expect must be JsonDiffArray");
}
this.actual = (JsonDiffArray) actual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/
public class ComplexArrayJsonNeat extends AbstractArrayJsonNeat<JsonDiffArray> {

public ComplexArrayJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
super(travelPath, actual, expect);
public ComplexArrayJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
super(travelPath, expect, actual);
}

protected JsonCompareResult ignoreOrder(JsonDiffArray expect, JsonDiffArray actual) {
Expand All @@ -39,7 +39,7 @@ protected JsonCompareResult ignoreOrder(JsonDiffArray expect, JsonDiffArray actu
continue;
}
TravelPath nextTravelPath = new TravelPath(this.travelPath, expectIndex, actualIndex);
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(actual.get(actualIndex), expect.get(expectIndex), nextTravelPath);
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate( expect.get(expectIndex), actual.get(actualIndex), nextTravelPath);
if (jsonNeat == null) {
continue;
}
Expand All @@ -65,7 +65,7 @@ protected JsonCompareResult ignoreOrder(JsonDiffArray expect, JsonDiffArray actu
JsonDiff actualItem = actual.get(actualIndex);
TravelPath nextTravelPath = new TravelPath(this.travelPath, expectIndex, actualIndex);
// 判断类型, 根据类型去实例化JsonNeat。
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(actualItem, expectItem, nextTravelPath);
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expectItem, actualItem, nextTravelPath);
// 类型不一致
if (jsonNeat != null) {
JsonCompareResult diff = jsonNeat.diff();
Expand Down Expand Up @@ -93,7 +93,7 @@ protected JsonCompareResult keepOrder(JsonDiffArray expect, JsonDiffArray actual
JsonDiff actualItem = actual.get(i);
TravelPath nextTravelPath = new TravelPath(this.travelPath, i, i);
// 判断类型, 根据类型去实例化JsonNeat。
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(actualItem, expectItem, nextTravelPath);
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expectItem, actualItem, nextTravelPath);
// 类型不一致
if (jsonNeat == null) {
Defects defects = new Defects()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
public class AlignArrayJsonDiff extends ComplexArrayJsonNeat {

protected AlignArrayJsonDiff(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
super(travelPath, actual, expect);
protected AlignArrayJsonDiff(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
super(travelPath, expect, actual);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public abstract class AbstractObjectJsonNeat<T extends JsonDiffObject> extends A

protected final JsonDiffObject expect;

protected AbstractObjectJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
protected AbstractObjectJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
super(travelPath);
if (!(actual instanceof JsonDiffObject) || !(expect instanceof JsonDiffObject)) {
if (!(expect instanceof JsonDiffObject) || !(actual instanceof JsonDiffObject)) {
throw new IllegalArgumentException("Parameter type error, actual and expect must be JsonDiffObject");
}
this.actual = (JsonDiffObject) actual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class ComplexObjectJsonNeat extends AbstractObjectJsonNeat<JsonDiffObject
*/
private final List<MappingKey> keyMap = new ArrayList<>();

public ComplexObjectJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
super(travelPath, actual, expect);
public ComplexObjectJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
super(travelPath, expect, actual);
}

/**
Expand Down Expand Up @@ -111,7 +111,7 @@ protected JsonCompareResult diff1() {
JsonDiff actualDiffJson = actual.get(mappingKey.getActualKey());
TravelPath nextTravelPath = new TravelPath(travelPath, mappingKey);
// 判断类型, 根据类型去实例化JsonNeat。
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(actualDiffJson, expectDiffJson, nextTravelPath);
JsonNeat<? extends JsonDiff> jsonNeat = RunTimeDataFactory.getOptionInstance().getJsonNeatFactory().generate(expectDiffJson, actualDiffJson, nextTravelPath);
if (jsonNeat == null) {
Defects defects = new Defects()
.setActual(actualDiffJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public abstract class AbstractOtherJsonNeat<T extends JsonDiffOther> extends Abs

protected final JsonDiffOther expect;

protected AbstractOtherJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
protected AbstractOtherJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
super(travelPath);
if (!(actual instanceof JsonDiffOther) || !(expect instanceof JsonDiffOther)) {
if (!(expect instanceof JsonDiffOther) || !(actual instanceof JsonDiffOther)) {
throw new IllegalArgumentException("Parameter type error, actual and expect must be JsonDiffOther");
}
this.actual = (JsonDiffOther) actual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
public class ComplexOtherJsonNeat extends AbstractOtherJsonNeat<JsonDiffOther> {

public ComplexOtherJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
super(travelPath, actual, expect);
public ComplexOtherJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
super(travelPath, expect, actual);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import me.codeleep.jsondiff.common.model.JsonCompareResult;
import me.codeleep.jsondiff.common.model.TravelPath;
import me.codeleep.jsondiff.common.model.neat.JsonDiff;
import me.codeleep.jsondiff.common.model.neat.JsonDiffObject;
import me.codeleep.jsondiff.common.model.neat.JsonDiffPrimitive;
import me.codeleep.jsondiff.core.handle.AbstractJsonNeat;

Expand All @@ -19,9 +18,9 @@ public abstract class AbstractPrimitiveJsonNeat<T extends JsonDiffPrimitive> ext

protected final JsonDiffPrimitive expect;

protected AbstractPrimitiveJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
protected AbstractPrimitiveJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
super(travelPath);
if (!(actual instanceof JsonDiffPrimitive) || !(expect instanceof JsonDiffPrimitive)) {
if (!(expect instanceof JsonDiffPrimitive) || !(actual instanceof JsonDiffPrimitive)) {
throw new IllegalArgumentException("Parameter type error, actual and expect must be JsonDiffPrimitive");
}
this.actual = (JsonDiffPrimitive) actual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
public class ComplexPrimitiveJsonNeat extends AbstractPrimitiveJsonNeat<JsonDiffPrimitive> {

public ComplexPrimitiveJsonNeat(TravelPath travelPath, JsonDiff actual, JsonDiff expect) {
super(travelPath, actual, expect);
public ComplexPrimitiveJsonNeat(TravelPath travelPath, JsonDiff expect, JsonDiff actual) {
super(travelPath, expect, actual);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package me.codeleep.jsondiff.impl.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import me.codeleep.jsondiff.common.model.neat.JsonDiff;
import me.codeleep.jsondiff.common.model.neat.JsonDiffArray;

import java.util.ArrayList;
import java.util.Collection;

/**
Expand Down Expand Up @@ -47,7 +49,10 @@ public void addAll(Collection<?> c) {

@Override
public Object format() {
return String.valueOf(jsonArray);
if (jsonArray == null) {
return null;
}
return jsonArray.toJSONString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import me.codeleep.jsondiff.common.model.neat.JsonDiff;
import me.codeleep.jsondiff.common.model.neat.JsonDiffObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -39,7 +41,10 @@ public Set<String> keySet() {

@Override
public Object format() {
return JSON.toJSONString(jsonObject);
if (jsonObject == null) {
return null;
}
return jsonObject.toJSONString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public boolean isEquals(JsonDiffOther jsonDiffOther) {

@Override
public Object format() {
if (object == null || object instanceof String) {
return object;
}
return String.valueOf(object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public boolean isEquals(JsonDiffPrimitive jsonDiffPrimitive) {

@Override
public Object format() {
if (object == null || object instanceof String) {
return object;
}
return String.valueOf(object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public void addAll(Collection<?> c) {

@Override
public Object format() {
return JSON.to(ArrayList.class, jsonArray);
if (jsonArray == null) {
return null;
}
return jsonArray.toJSONString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public Set<String> keySet() {

@Override
public Object format() {
return JSON.to(HashMap.class, jsonObject);
if (this.jsonObject == null) {
return null;
}
return jsonObject.toJSONString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public boolean isEquals(JsonDiffOther jsonDiffOther) {

@Override
public Object format() {
if (object == null || object instanceof String) {
return object;
}
return String.valueOf(object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public boolean isEquals(JsonDiffPrimitive jsonDiffPrimitive) {

@Override
public Object format() {
if (object == null || object instanceof String) {
return object;
}
return String.valueOf(object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public void addAll(Collection<?> cs) {

@Override
public Object format() {
return gson.fromJson(jsonArray, ArrayList.class);
if (jsonArray == null) {
return null;
}
return gson.toJson(jsonArray);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public Set<String> keySet() {

@Override
public Object format() {
return gson.fromJson(jsonObject, HashMap.class);
if (jsonObject == null) {
return null;
}
return gson.toJson(jsonObject);
}

@Override
Expand Down
Loading