Skip to content

Commit 1961d34

Browse files
committed
feature(JsonNeat): 临时提交
1. 临时提交
1 parent d414574 commit 1961d34

File tree

12 files changed

+279
-19
lines changed

12 files changed

+279
-19
lines changed

json-diff-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
<version>${project.version}</version>
2020
<scope>compile</scope>
2121
</dependency>
22+
<dependency>
23+
<groupId>cn.xiaoandcai</groupId>
24+
<artifactId>json-diff-impl</artifactId>
25+
<version>${project.version}</version>
26+
<scope>compile</scope>
27+
</dependency>
2228
</dependencies>
2329

2430
<properties>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package me.codeleep.jsondiff.core.config;
2+
3+
import me.codeleep.jsondiff.core.utils.JsonNeatFactory;
4+
import me.codeleep.jsondiff.impl.ImplType;
5+
6+
/**
7+
* @author: codeleep
8+
* @createTime: 2023/04/16 13:33
9+
* @description: 全局配置
10+
*/
11+
public class JsonDiffConfig {
12+
13+
/**
14+
* 默认的比较器工厂
15+
*/
16+
private final static JsonNeatFactory jsonNeatFactory = new JsonNeatFactory();
17+
18+
/**
19+
* 默认的json框架
20+
*/
21+
private final static ImplType DEFAULT_JSON_FRAMEWORK = ImplType.FASTJSON2;
22+
23+
24+
public static JsonNeatFactory getJsonNeatFactory() {
25+
return jsonNeatFactory;
26+
}
27+
28+
}

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,78 +18,78 @@ public class JsonNeatFactory {
1818
/**
1919
* 对象默认比较器
2020
*/
21-
private static Class<? extends JsonNeat> objectJsonNeat = ComplexObjectJsonNeat.class;
21+
private Class<? extends JsonNeat> objectJsonNeat = ComplexObjectJsonNeat.class;
2222

2323
/**
2424
* 数组默认比较器
2525
*/
26-
private static Class<? extends JsonNeat> arrayJsonNeat = ComplexArrayJsonNeat.class;
26+
private Class<? extends JsonNeat> arrayJsonNeat = ComplexArrayJsonNeat.class;
2727

2828
/**
2929
* 基本类型默认比较器
3030
*/
31-
private static Class<? extends JsonNeat> primitiveJsonNeat = PrimitiveTypeJsonNeat.class;
31+
private Class<? extends JsonNeat> primitiveJsonNeat = PrimitiveTypeJsonNeat.class;
3232

3333
/**
3434
* 指定的path使用自定义比较器
3535
* key: 与ignorePath格式一致
3636
* value: 继承 AbstractArrayJsonNeat,AbstractObjectJsonNeat,AbstractPrimitiveJsonNeat. 并且实现对应格式接口的字节码
3737
*/
38-
private static final Map<String, Class<? extends JsonNeat>> customComparator = new HashMap<>();
38+
private final Map<String, Class<? extends JsonNeat>> customComparator = new HashMap<>();
3939

4040

4141

42-
public static Class<? extends JsonNeat> getObjectJsonNeat(boolean defaultNeat) {
42+
public Class<? extends JsonNeat> getObjectJsonNeat(boolean defaultNeat) {
4343
if (defaultNeat) {
4444
return ComplexObjectJsonNeat.class;
4545
}
4646
return objectJsonNeat;
4747
}
4848

49-
public static JsonNeat getObjectJsonNeatInstance(boolean defaultNeat) {
49+
public JsonNeat getObjectJsonNeatInstance(boolean defaultNeat) {
5050
return ClassUtil.getClassNameInstance(getObjectJsonNeat(defaultNeat));
5151
}
5252

53-
public static void setObjectJsonNeat(Class<? extends JsonNeat> objectJsonNeat) {
54-
JsonNeatFactory.objectJsonNeat = objectJsonNeat;
53+
public void setObjectJsonNeat(Class<? extends JsonNeat> objectJsonNeat) {
54+
this.objectJsonNeat = objectJsonNeat;
5555
}
5656

57-
public static Class<? extends JsonNeat> getArrayJsonNeat(boolean defaultNeat) {
57+
public Class<? extends JsonNeat> getArrayJsonNeat(boolean defaultNeat) {
5858
if (defaultNeat) {
5959
return ComplexArrayJsonNeat.class;
6060
}
6161
return arrayJsonNeat;
6262
}
6363

64-
public static JsonNeat getArrayJsonNeatInstance(boolean defaultNeat) {
64+
public JsonNeat getArrayJsonNeatInstance(boolean defaultNeat) {
6565
return ClassUtil.getClassNameInstance(getArrayJsonNeat(defaultNeat));
6666
}
6767

68-
public static void setArrayJsonNeat(Class<? extends JsonNeat> arrayJsonNeat) {
69-
JsonNeatFactory.arrayJsonNeat = arrayJsonNeat;
68+
public void setArrayJsonNeat(Class<? extends JsonNeat> arrayJsonNeat) {
69+
this.arrayJsonNeat = arrayJsonNeat;
7070
}
7171

72-
public static Class<? extends JsonNeat> getPrimitiveJsonNeat(boolean defaultNeat) {
72+
public Class<? extends JsonNeat> getPrimitiveJsonNeat(boolean defaultNeat) {
7373
if (defaultNeat) {
7474
return PrimitiveTypeJsonNeat.class;
7575
}
7676
return primitiveJsonNeat;
7777
}
7878

79-
public static JsonNeat getPrimitiveJsonNeatInstance(boolean defaultNeat) {
79+
public JsonNeat getPrimitiveJsonNeatInstance(boolean defaultNeat) {
8080
return ClassUtil.getClassNameInstance(getPrimitiveJsonNeat(defaultNeat));
8181
}
8282

83-
public static void setPrimitiveJsonNeat(Class<? extends JsonNeat> primitiveJsonNeat) {
84-
JsonNeatFactory.primitiveJsonNeat = primitiveJsonNeat;
83+
public void setPrimitiveJsonNeat(Class<? extends JsonNeat> primitiveJsonNeat) {
84+
this.primitiveJsonNeat = primitiveJsonNeat;
8585
}
8686

87-
public static Class<? extends JsonNeat> getCustomComparator(String path) {
87+
public Class<? extends JsonNeat> getCustomComparator(String path) {
8888
return customComparator.get(path);
8989
}
9090

91-
public static void addCustomComparator(String path, Class<? extends JsonNeat> customComparator) {
92-
JsonNeatFactory.customComparator.put(path, customComparator);
91+
public void addCustomComparator(String path, Class<? extends JsonNeat> customComparator) {
92+
this.customComparator.put(path, customComparator);
9393
}
9494

9595
}

json-diff-impl/pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>json-diff-parent</artifactId>
7+
<groupId>cn.xiaoandcai</groupId>
8+
<version>3.0.3-RC1-RELEASE</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>json-diff-impl</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>11</maven.compiler.source>
16+
<maven.compiler.target>11</maven.compiler.target>
17+
</properties>
18+
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.alibaba.fastjson2</groupId>
23+
<artifactId>fastjson2</artifactId>
24+
<version>2.0.7</version>
25+
<scope>provided</scope>
26+
</dependency>
27+
</dependencies>
28+
29+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package me.codeleep.jsondiff.impl;
2+
3+
/**
4+
* @author: codeleep
5+
* @createTime: 2023/04/16 20:42
6+
* @description: 实现类型
7+
*/
8+
public enum ImplType {
9+
10+
FASTJSON("fastjson"),
11+
FASTJSON2("fastjson2"),
12+
JACKSON("jackson"),
13+
GSON("gson");
14+
15+
private String type;
16+
17+
ImplType(String type) {
18+
this.type = type;
19+
}
20+
21+
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package me.codeleep.jsondiff.impl;
2+
3+
/**
4+
* @author: codeleep
5+
* @createTime: 2023/04/16 21:15
6+
* @description:
7+
*/
8+
public interface JsonDiff {
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package me.codeleep.jsondiff.impl;
2+
3+
/**
4+
* @author: codeleep
5+
* @createTime: 2023/04/16 21:02
6+
* @description: 数组
7+
*/
8+
public interface JsonDiffArray extends JsonDiff{
9+
10+
/**
11+
* 获取key集合
12+
* @return key集合
13+
*/
14+
int size();
15+
16+
17+
18+
/**
19+
* 获取key对应的值
20+
* @param index 索引
21+
* @return 值
22+
*/
23+
Object get(int index);
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package me.codeleep.jsondiff.impl;
2+
3+
import com.alibaba.fastjson2.JSONArray;
4+
import com.alibaba.fastjson2.JSONObject;
5+
import me.codeleep.jsondiff.impl.fastjson2.FastJson2Array;
6+
import me.codeleep.jsondiff.impl.fastjson2.FastJson2Object;
7+
8+
/**
9+
* @author: codeleep
10+
* @createTime: 2023/04/16 21:33
11+
* @description: 对象构造器
12+
*/
13+
public class JsonDiffBuilder {
14+
15+
public JsonDiffObject buildObject(JSONObject jsonObject) {
16+
return new FastJson2Object(jsonObject);
17+
}
18+
19+
public FastJson2Array buildObject(JSONArray jsonArray) {
20+
return new FastJson2Array(jsonArray);
21+
}
22+
23+
// TODO 更多的实现
24+
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package me.codeleep.jsondiff.impl;
2+
3+
import java.util.Set;
4+
5+
/**
6+
* @author: codeleep
7+
* @createTime: 2023/04/16 21:02
8+
* @description: 对象
9+
*/
10+
public interface JsonDiffObject extends JsonDiff{
11+
12+
/**
13+
* 获取key对应的值
14+
* @param key key
15+
* @return 值
16+
*/
17+
Object get(String key);
18+
19+
20+
/**
21+
* 获取key集合
22+
* @return key集合
23+
*/
24+
Set<String> keySet();
25+
26+
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package me.codeleep.jsondiff.impl.fastjson2;
2+
3+
import com.alibaba.fastjson2.JSONArray;
4+
import com.alibaba.fastjson2.JSONObject;
5+
import me.codeleep.jsondiff.impl.JsonDiffArray;
6+
7+
/**
8+
* @author: codeleep
9+
* @createTime: 2023/04/16 21:28
10+
* @description: 数组
11+
*/
12+
public class FastJson2Array implements JsonDiffArray {
13+
14+
private final JSONArray jsonArray;
15+
16+
public FastJson2Array(JSONArray jsonArray) {
17+
this.jsonArray = jsonArray;
18+
}
19+
20+
@Override
21+
public int size() {
22+
if (jsonArray == null) {
23+
return 0;
24+
}
25+
return jsonArray.size();
26+
}
27+
28+
@Override
29+
public Object get(int index) {
30+
if (jsonArray == null) {
31+
return null;
32+
}
33+
Object value = jsonArray.get(index);
34+
if (value instanceof JSONArray) {
35+
return new FastJson2Array((JSONArray) value);
36+
}
37+
if (value instanceof JSONObject) {
38+
return new FastJson2Object((JSONObject) value);
39+
}
40+
return value;
41+
}
42+
}

0 commit comments

Comments
 (0)