Skip to content

Commit 064229b

Browse files
committed
初步完成,首次提交
1 parent 4d2afc9 commit 064229b

33 files changed

Lines changed: 2664 additions & 1 deletion

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright ©2025 APIJSON(https://github.com/APIJSON)
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# apijson-fastjson2 [![](https://jitpack.io/v/APIJSON/apijson-fastjson2.svg)](https://jitpack.io/#APIJSON/apijson-fastjson2)
2+
腾讯 [APIJSON](https://github.com/Tencent/APIJSON) 8.0.0+ 的 fastjson2 插件,简化使用。<br />
3+
A fastjson2 plugin for Tencent [APIJSON](https://github.com/Tencent/APIJSON) 8.0.0+.<br />
4+
5+
6+
## 添加依赖
7+
## Add Dependency
8+
9+
### Maven
10+
#### 1. 在 pom.xml 中添加 JitPack 仓库
11+
#### 1. Add the JitPack repository to pom.xml
12+
```xml
13+
<repositories>
14+
<repository>
15+
<id>jitpack.io</id>
16+
<url>https://jitpack.io</url>
17+
</repository>
18+
</repositories>
19+
```
20+
21+
![image](https://user-images.githubusercontent.com/5738175/167263399-339dad4f-2884-461e-9781-f2de6d100340.png)
22+
23+
<br />
24+
25+
#### 2. 在 pom.xml 中添加 apijson-fastjson2 依赖
26+
#### 2. Add the apijson-fastjson2 dependency to pom.xml
27+
```xml
28+
<dependency>
29+
<groupId>com.github.APIJSON</groupId>
30+
<artifactId>apijson-fastjson2</artifactId>
31+
<version>LATEST</version>
32+
</dependency>
33+
```
34+
35+
36+
<br />
37+
<br />
38+
39+
### Gradle
40+
#### 1. 在项目根目录 build.gradle 中最后添加 JitPack 仓库
41+
#### 1. Add the JitPack repository in your root build.gradle at the end of repositories
42+
```gradle
43+
allprojects {
44+
repositories {
45+
maven { url 'https://jitpack.io' }
46+
}
47+
}
48+
```
49+
<br />
50+
51+
#### 2. 在项目某个 module 目录(例如 `app`) build.gradle 中添加 apijson-fastjson2 依赖
52+
#### 2. Add the apijson-fastjson2 dependency in one of your modules(such as `app`)
53+
```gradle
54+
dependencies {
55+
implementation 'com.github.APIJSON:apijson-fastjson2:latest'
56+
}
57+
```
58+
59+
<br />
60+
<br />
61+
62+
## 初始化
63+
## Initialization
64+
65+
#### 1.把所有 apijson.framework 中的解析类都替换成 apijson.fastjson2 的
66+
#### 1.Replace all apijson.framework classes to that of apijson.fastjson2 what have same names
67+
68+
```java
69+
import apijson.fastjson2.APIJSONApplication; // apijson.framework.APIJSONApplication;
70+
71+
public class DemoApplication {
72+
public static void main(String[] args) {
73+
// ...
74+
APIJSONApplication.init();
75+
// ...
76+
}
77+
}
78+
```
79+
80+
```java
81+
import apijson.fastjson2.APIJSONController; // apijson.framework.APIJSONController;
82+
83+
public class DemoController extends APIJSONController<Long> { // apijson.framework.APIJSONController<Long, JSONObject, JSONArray>
84+
}
85+
```
86+
87+
```java
88+
import apijson.fastjson2.APIJSONParser; // apijson.framework.APIJSONParser;
89+
90+
public class DemoParser extends APIJSONParser<Long> { // apijson.framework.APIJSONParser<Long, JSONObject, JSONArray>
91+
}
92+
```
93+
94+
```java
95+
import apijson.fastjson2.APIJSONObjectParser; // apijson.framework.APIJSONObjectParser;
96+
97+
public class DemoParser extends APIJSONObjectParser<Long> { // apijson.framework.APIJSONObjectParser<Long, JSONObject, JSONArray>
98+
}
99+
```
100+
101+
102+
```java
103+
import apijson.fastjson2.APIJSONFunctionParser; // apijson.framework.APIJSONFunctionParser;
104+
105+
public class DemoParser extends APIJSONFunctionParser<Long> { // apijson.framework.APIJSONFunctionParser<Long, JSONObject, JSONArray>
106+
}
107+
```
108+
109+
110+
```java
111+
import apijson.fastjson2.APIJSONVerifier; // apijson.framework.APIJSONVerifier;
112+
113+
public class DemoParser extends APIJSONVerifier<Long> { // apijson.framework.APIJSONVerifier<Long, JSONObject, JSONArray>
114+
}
115+
```
116+
117+
118+
```java
119+
import apijson.fastjson2.APIJSONSQLConfig; // apijson.framework.APIJSONSQLConfig;
120+
121+
public class DemoSQLConfig extends APIJSONSQLConfig<Long> { // apijson.framework.APIJSONSQLConfig<Long, JSONObject, JSONArray>
122+
}
123+
```
124+
125+
```java
126+
import apijson.fastjson2.APIJSONSQLExecutor; // apijson.framework.APIJSONSQLExecutor;
127+
128+
public class DemoSQLExecutor extends APIJSONSQLExecutor<Long> { // apijson.framework.APIJSONSQLExecutor<Long, JSONObject, JSONArray>
129+
}
130+
```
131+
132+
...
133+
134+
<br />
135+
136+
137+
参考 [APIJSONController](/src/main/java/apijson/fastjson2/APIJSONController.java) 的注释及 [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot)[DemoController](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoController.java)[DemoApplication](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoApplication.java) <br />
138+
139+
See document in [APIJSONController](/src/main/java/apijson/fastjson2/APIJSONController.java) and [DemoController](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoController.java), [DemoApplication](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoApplication.java) in [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot)
140+
141+
<br />
142+
143+
<br />
144+
有问题可以去 Tencent/APIJSON 提 issue <br />
145+
https://github.com/Tencent/APIJSON/issues/36
146+
147+
<br /><br />
148+
149+
#### 点右上角 ⭐Star 支持一下,谢谢 ^_^
150+
#### Please ⭐Star this project ^_^
151+
https://github.com/APIJSON/apijson-fastjson2

pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>apijson.fastjson2</groupId>
7+
<artifactId>apijson-fastjson2</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>APIJSONRouter</name>
12+
<description>APIJSON plugin with fastjson2 to simplify the use of APIJSON</description>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17+
<java.version>1.8</java.version>
18+
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
19+
</properties>
20+
21+
<dependencies>
22+
<!-- JDK 17+, SpringBoot 3.0+ -->
23+
<dependency>
24+
<groupId>jakarta.servlet</groupId>
25+
<artifactId>jakarta.servlet-api</artifactId>
26+
<version>5.0.0</version>
27+
<scope>provided</scope>
28+
</dependency>
29+
30+
<!-- JDK 1.8~16, SpringBoot 1.4~2.7 -->
31+
<dependency>
32+
<groupId>javax.servlet</groupId>
33+
<artifactId>javax.servlet-api</artifactId>
34+
<version>4.0.1</version>
35+
<scope>provided</scope>
36+
</dependency>
37+
38+
<!-- 可使用 libs 目录的 apijson-orm.jar 来替代,两种方式二选一 -->
39+
<dependency>
40+
<groupId>com.github.Tencent</groupId>
41+
<artifactId>APIJSON</artifactId>
42+
<version>8.0.0.2</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.github.APIJSON</groupId>
46+
<artifactId>apijson-framework</artifactId>
47+
<version>7.2.0.2</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.alibaba.fastjson2</groupId>
51+
<artifactId>fastjson2</artifactId>
52+
<version>2.0.57</version>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<version>3.12.1</version>
62+
<configuration>
63+
<source>1.8</source>
64+
<target>1.8</target>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
70+
<repositories>
71+
<!-- APIJSON 必须用到的托管平台 -->
72+
<repository>
73+
<id>jitpack.io</id>
74+
<url>https://jitpack.io</url>
75+
</repository>
76+
</repositories>
77+
78+
</project>
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*Copyright ©2025 APIJSON(https://github.com/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package apijson.fastjson2;
16+
17+
import apijson.NotNull;
18+
import apijson.framework.APIJSONCreator;
19+
import com.alibaba.fastjson2.JSONArray;
20+
import com.alibaba.fastjson2.JSONObject;
21+
import com.alibaba.fastjson2.JSONWriter;
22+
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
/**启动入口 Application
27+
* 调用 APIJSONApplication.init
28+
* @author Lemon
29+
*/
30+
public class APIJSONApplication extends apijson.framework.APIJSONApplication {
31+
32+
//static {
33+
// apijson.JSON.DEFAULT_JSON_PARSER = JSON.DEFAULT_JSON_PARSER; // 解决 DEFAULT_JSON_PARSER 初始化前就自测导致抛异常
34+
//}
35+
36+
static {
37+
JSON.DEFAULT_JSON_PARSER = new JSONParser() {
38+
@Override
39+
public JSONObject createJSONObject() {
40+
return JSONParser.super.createJSONObject();
41+
}
42+
43+
@Override
44+
public JSONArray createJSONArray() {
45+
return JSONParser.super.createJSONArray();
46+
}
47+
48+
@Override
49+
public String toJSONString(Object obj, boolean format) {
50+
if (obj instanceof String) {
51+
if (! format) {
52+
return (String) obj;
53+
}
54+
55+
obj = com.alibaba.fastjson2.JSON.parseObject((String) obj);
56+
}
57+
58+
if (format) {
59+
return com.alibaba.fastjson2.JSON.toJSONString(obj, JSONWriter.Feature.PrettyFormat);
60+
}
61+
62+
return com.alibaba.fastjson2.JSON.toJSONString(obj);
63+
}
64+
65+
@Override
66+
public Object parse(Object json) {
67+
return com.alibaba.fastjson2.JSON.parse(toJSONString(json));
68+
}
69+
70+
@Override
71+
public JSONObject parseObject(Object json) {
72+
return com.alibaba.fastjson2.JSON.parseObject(toJSONString(json));
73+
}
74+
75+
@Override
76+
public <T> T parseObject(Object json, Class<T> clazz) {
77+
return com.alibaba.fastjson2.JSON.parseObject(toJSONString(json), clazz);
78+
}
79+
80+
@Override
81+
public JSONArray parseArray(Object json) {
82+
return com.alibaba.fastjson2.JSON.parseArray(toJSONString(json));
83+
}
84+
85+
@Override
86+
public <T> List<T> parseArray(Object json, Class<T> clazz) {
87+
return com.alibaba.fastjson2.JSON.parseArray(toJSONString(json), clazz);
88+
}
89+
};
90+
}
91+
92+
93+
//public static <T> APIJSONParser<T> createParser() {
94+
// return (APIJSONParser<T>) DEFAULT_APIJSON_CREATOR.createParser();
95+
//}
96+
97+
/**初始化,加载所有配置并校验
98+
* @return
99+
* @throws Exception
100+
*/
101+
public static void init() throws Exception {
102+
init(true, DEFAULT_APIJSON_CREATOR);
103+
}
104+
/**初始化,加载所有配置并校验
105+
* @param shutdownWhenServerError
106+
* @return
107+
* @throws Exception
108+
*/
109+
public static void init(boolean shutdownWhenServerError) throws Exception {
110+
init(shutdownWhenServerError, DEFAULT_APIJSON_CREATOR);
111+
}
112+
/**初始化,加载所有配置并校验
113+
* @param creator
114+
* @return
115+
* @throws Exception
116+
*/
117+
public static <T, M extends Map<String, Object>, L extends List<Object>> void init(
118+
@NotNull apijson.framework.APIJSONCreator<T, M, L> creator) throws Exception {
119+
init(true, creator);
120+
}
121+
/**初始化,加载所有配置并校验
122+
* @param shutdownWhenServerError
123+
* @param creator
124+
* @return
125+
* @throws Exception
126+
*/
127+
public static <T, M extends Map<String, Object>, L extends List<Object>> void init(
128+
boolean shutdownWhenServerError, @NotNull APIJSONCreator<T, M, L> creator) throws Exception {
129+
apijson.framework.APIJSONApplication.init(shutdownWhenServerError, creator);
130+
}
131+
132+
}

0 commit comments

Comments
 (0)