Skip to content

Commit 945cbad

Browse files
committed
fix-to(impl): Support for module introduction
1 parent c9e09bb commit 945cbad

File tree

38 files changed

+373
-246
lines changed

38 files changed

+373
-246
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package me.codeleep.jsondiff.common.model.neat;
2+
3+
/**
4+
* @author: codeleep
5+
* @createTime: 2024/04/11 下午7:08
6+
* @description:
7+
*/
8+
public interface JsonBuilder {
9+
10+
JsonDiff builder(String json);
11+
12+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package me.codeleep.jsondiff.common.utils;
2+
3+
import me.codeleep.jsondiff.common.exception.JsonDiffException;
4+
import me.codeleep.jsondiff.common.model.neat.JsonBuilder;
5+
6+
/**
7+
* @author: codeleep
8+
* @createTime: 2023/04/16 20:42
9+
* @description: 实现类型
10+
*/
11+
public enum ImplType {
12+
13+
FASTJSON("fastjson", "me.codeleep.jsondiff.impl.fastjson.FastJsonBuilder"),
14+
FASTJSON2("fastjson2", "me.codeleep.jsondiff.impl.fastjson2.FastJson2Builder"),
15+
JACKSON("jackson", "me.codeleep.jsondiff.impl.gson.GsonBuilder"),
16+
GSON("gson", "me.codeleep.jsondiff.impl.jackson.JacksonBuilder");
17+
18+
private String type;
19+
20+
private String builderClassName;
21+
22+
ImplType(String type, String builderClassName) {
23+
this.type = type;
24+
this.builderClassName = builderClassName;
25+
}
26+
27+
public JsonBuilder getBuilder() {
28+
try {
29+
return (JsonBuilder) Class.forName(builderClassName).newInstance();
30+
} catch (Exception e) {
31+
throw new JsonDiffException("Failed to create JsonBuilder instance for " + type, e);
32+
}
33+
}
34+
35+
/**
36+
* 检测使用哪个json框架
37+
* @return
38+
*/
39+
public static ImplType detectJsonParser() {
40+
for (ImplType implType : ImplType.values()) {
41+
try {
42+
Class.forName(implType.builderClassName);
43+
return implType;
44+
} catch (ClassNotFoundException e) {
45+
// Ignore the exception and try the next class name
46+
}
47+
}
48+
throw new JsonDiffException("No JSON parsing framework found, at least one of which implements. fastjson,fastjson2,jackson,gson");
49+
}
50+
}

json-diff-core/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
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>
2822
</dependencies>
2923

3024
<properties>

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

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

3-
import me.codeleep.jsondiff.impl.ImplType;
3+
import me.codeleep.jsondiff.common.utils.ImplType;
44

55
/**
66
* @author: codeleep

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import me.codeleep.jsondiff.common.model.neat.JsonDiff;
44
import me.codeleep.jsondiff.core.config.JsonDiffOption;
5-
import me.codeleep.jsondiff.impl.FrameworkImplUtil;
6-
import me.codeleep.jsondiff.impl.ImplType;
5+
import me.codeleep.jsondiff.common.utils.ImplType;
6+
7+
import static me.codeleep.jsondiff.common.utils.ImplType.*;
78

89
/**
910
* @author: codeleep
@@ -15,10 +16,10 @@ public class JsonDiffBuilder {
1516
public static JsonDiff buildObject(String jsonStr) {
1617
ImplType defaultJsonFramework = JsonDiffOption.getDefaultJsonFramework();
1718
switch (defaultJsonFramework) {
18-
case FASTJSON: return FrameworkImplUtil.fastJson(jsonStr);
19-
case FASTJSON2: return FrameworkImplUtil.fastJson2(jsonStr);
20-
case GSON: return FrameworkImplUtil.gson(jsonStr);
21-
case JACKSON: return FrameworkImplUtil.jackson(jsonStr);
19+
case FASTJSON: return FASTJSON.getBuilder().builder(jsonStr);
20+
case FASTJSON2: return FASTJSON2.getBuilder().builder(jsonStr);
21+
case GSON: return GSON.getBuilder().builder(jsonStr);
22+
case JACKSON: return JACKSON.getBuilder().builder(jsonStr);
2223
}
2324
return null;
2425
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>cn.xiaoandcai</groupId>
8+
<artifactId>json-diff-parent</artifactId>
9+
<version>4.0.5-RC1-RELEASE</version>
10+
<relativePath>../../pom.xml</relativePath>
11+
</parent>
12+
13+
<version>4.0.5-RC1-RELEASE</version>
14+
<artifactId>json-diff-impl-fastjson</artifactId>
15+
16+
<properties>
17+
<maven.compiler.source>8</maven.compiler.source>
18+
<maven.compiler.target>8</maven.compiler.target>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>cn.xiaoandcai</groupId>
25+
<artifactId>json-diff-core</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.alibaba</groupId>
30+
<artifactId>fastjson</artifactId>
31+
<version>1.2.74</version>
32+
</dependency>
33+
</dependencies>
34+
35+
</project>

json-diff-impl/src/main/java/me/codeleep/jsondiff/impl/fastjson/FastJsonArray.java renamed to json-diff-impl/json-diff-impl-fastjson/src/main/java/me/codeleep/jsondiff/impl/fastjson/FastJsonArray.java

File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package me.codeleep.jsondiff.impl.fastjson;
2+
3+
import me.codeleep.jsondiff.common.model.neat.JsonDiff;
4+
import me.codeleep.jsondiff.common.model.neat.JsonBuilder;
5+
6+
/**
7+
* @author: codeleep
8+
* @createTime: 2024/04/11 下午7:10
9+
* @description:
10+
*/
11+
public class FastJsonBuilder implements JsonBuilder {
12+
@Override
13+
public JsonDiff builder(String json) {
14+
Object parse = com.alibaba.fastjson.JSON.parse(json);
15+
if (parse instanceof com.alibaba.fastjson.JSONObject) {
16+
return new FastJsonObject((com.alibaba.fastjson.JSONObject)parse);
17+
}
18+
if (parse instanceof com.alibaba.fastjson.JSONArray) {
19+
return new FastJsonArray((com.alibaba.fastjson.JSONArray)parse);
20+
}
21+
return null;
22+
}
23+
}

json-diff-impl/src/main/java/me/codeleep/jsondiff/impl/fastjson/FastJsonObject.java renamed to json-diff-impl/json-diff-impl-fastjson/src/main/java/me/codeleep/jsondiff/impl/fastjson/FastJsonObject.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package me.codeleep.jsondiff.impl.fastjson;
22

3-
import com.alibaba.fastjson.JSONArray;
3+
import com.alibaba.fastjson.JSON;
44
import com.alibaba.fastjson.JSONObject;
5-
import com.alibaba.fastjson2.JSON;
65
import me.codeleep.jsondiff.common.model.neat.JsonDiff;
76
import me.codeleep.jsondiff.common.model.neat.JsonDiffObject;
87

9-
import java.util.HashMap;
108
import java.util.HashSet;
119
import java.util.Set;
1210

@@ -41,7 +39,7 @@ public Set<String> keySet() {
4139

4240
@Override
4341
public Object format() {
44-
return JSON.to(HashMap.class, jsonObject);
42+
return JSON.toJSONString(jsonObject);
4543
}
4644

4745
@Override

json-diff-impl/src/main/java/me/codeleep/jsondiff/impl/fastjson/FastJsonOther.java renamed to json-diff-impl/json-diff-impl-fastjson/src/main/java/me/codeleep/jsondiff/impl/fastjson/FastJsonOther.java

File renamed without changes.

0 commit comments

Comments
 (0)