Skip to content

Commit 8eefb0b

Browse files
committed
优化 JSON 相关序列化、反序列化等处理;所有 APIJSONCreator, JSONCreator, JSONParser 都集中处理,避免不一致导致的 ClassCastException
1 parent d73341a commit 8eefb0b

27 files changed

+779
-568
lines changed

src/main/java/apijson/framework/APIJSONApplication.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
1+
/*Copyright ©2016 APIJSON(https://github.com/APIJSON)
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -32,11 +32,35 @@ public class APIJSONApplication {
3232
public static final String TAG = "APIJSONApplication";
3333

3434
@NotNull
35-
public static APIJSONCreator<?, ? extends Map<String, Object>, ? extends List<Object>> DEFAULT_APIJSON_CREATOR;
35+
public static APIJSONCreator<?, ? extends Map<String, Object>, ? extends List<Object>> DEFAULT_APIJSON_CREATOR;
3636
static {
3737
DEFAULT_APIJSON_CREATOR = new APIJSONCreator<>();
3838
}
3939

40+
@SuppressWarnings("unchecked")
41+
public static <T, M extends Map<String, Object>, L extends List<Object>> APIJSONParser<T, M, L> createParser() {
42+
return (APIJSONParser<T, M, L>) DEFAULT_APIJSON_CREATOR.createParser();
43+
}
44+
@SuppressWarnings("unchecked")
45+
public static <T, M extends Map<String, Object>, L extends List<Object>> APIJSONFunctionParser<T, M, L> createFunctionParser() {
46+
return (APIJSONFunctionParser<T, M, L>) DEFAULT_APIJSON_CREATOR.createFunctionParser();
47+
}
48+
49+
@SuppressWarnings("unchecked")
50+
public static <T, M extends Map<String, Object>, L extends List<Object>> APIJSONVerifier<T, M, L> createVerifier() {
51+
return (APIJSONVerifier<T, M, L>) DEFAULT_APIJSON_CREATOR.createVerifier();
52+
}
53+
54+
@SuppressWarnings("unchecked")
55+
public static <T, M extends Map<String, Object>, L extends List<Object>> APIJSONSQLConfig<T, M, L> createSQLConfig() {
56+
return (APIJSONSQLConfig<T, M, L>) DEFAULT_APIJSON_CREATOR.createSQLConfig();
57+
}
58+
59+
@SuppressWarnings("unchecked")
60+
public static <T, M extends Map<String, Object>, L extends List<Object>> APIJSONSQLExecutor<T, M, L> createSQLExecutor() {
61+
return (APIJSONSQLExecutor<T, M, L>) DEFAULT_APIJSON_CREATOR.createSQLExecutor();
62+
}
63+
4064

4165
/**初始化,加载所有配置并校验
4266
* @return
@@ -73,13 +97,6 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> void in
7397
System.out.println("\n\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON 开始启动 >>>>>>>>>>>>>>>>>>>>>>>>\n");
7498
DEFAULT_APIJSON_CREATOR = creator;
7599

76-
// 统一用同一个 creator
77-
APIJSONSQLConfig.APIJSON_CREATOR = creator;
78-
APIJSONParser.APIJSON_CREATOR = creator;
79-
APIJSONController.APIJSON_CREATOR = creator;
80-
APIJSONVerifier.APIJSON_CREATOR = creator;
81-
APIJSONFunctionParser.APIJSON_CREATOR = creator;
82-
83100
if (APIJSONVerifier.ENABLE_VERIFY_ROLE) {
84101
System.out.println("\n\n\n开始初始化: Access 权限校验配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
85102
try {
@@ -173,8 +190,10 @@ protected static void onServerError(String msg, boolean shutdown) throws ServerE
173190
}
174191
}
175192

176-
public static void addScriptExecutor(String language, ScriptExecutor scriptExecutor) {
193+
public static <T, M extends Map<String, Object>, L extends List<Object>> void addScriptExecutor(String language, ScriptExecutor<T, M, L> scriptExecutor) {
177194
scriptExecutor.init();
178195
AbstractFunctionParser.SCRIPT_EXECUTOR_MAP.put(language, scriptExecutor);
179196
}
197+
198+
180199
}

src/main/java/apijson/framework/APIJSONConstant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
1+
/*Copyright ©2016 APIJSON(https://github.com/APIJSON)
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

src/main/java/apijson/framework/APIJSONController.java

Lines changed: 140 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
1+
/*Copyright ©2016 APIJSON(https://github.com/APIJSON)
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
import apijson.JSONRequest;
1919
import apijson.orm.*;
2020

21+
import apijson.orm.exception.CommonException;
2122
import jakarta.servlet.http.HttpSession;
2223

2324
import java.rmi.ServerException;
@@ -38,32 +39,154 @@
3839
public class APIJSONController<T, M extends Map<String, Object>, L extends List<Object>> {
3940
public static final String TAG = "APIJSONController";
4041

41-
@NotNull
42-
public static APIJSONCreator<?, ? extends Map<String, Object>, ? extends List<Object>> APIJSON_CREATOR;
43-
static {
44-
APIJSON_CREATOR = new APIJSONCreator<Object, JSONObject, JSONArray>();
45-
}
46-
4742
public String getRequestURL() {
4843
return null;
4944
}
5045

5146
public APIJSONParser<T, M, L> newParser(HttpSession session, RequestMethod method) {
52-
@SuppressWarnings("unchecked")
53-
APIJSONParser<T, M, L> parser = (APIJSONParser<T, M, L>) APIJSON_CREATOR.createParser();
47+
APIJSONParser<T, M, L> parser = APIJSONApplication.createParser();
5448
parser.setMethod(method);
5549
parser.setSession(session);
5650
parser.setRequestURL(getRequestURL());
5751
return parser;
5852
}
5953

54+
public static APIJSONParser<?, ? extends Map<String, Object>, ? extends List<Object>> ERR_PARSER = APIJSONApplication.createParser();
55+
56+
/**新建带状态内容的JSONObject
57+
* @param code
58+
* @param msg
59+
* @return
60+
*/
61+
public static <M extends Map<String, Object>> M newResult(int code, String msg) {
62+
return newResult(code, msg, null);
63+
}
64+
65+
/**
66+
* 添加JSONObject的状态内容,一般用于错误提示结果
67+
*
68+
* @param code
69+
* @param msg
70+
* @param warn
71+
* @return
72+
*/
73+
public static <M extends Map<String, Object>> M newResult(int code, String msg, String warn) {
74+
return newResult(code, msg, warn, false);
75+
}
76+
77+
/**
78+
* 新建带状态内容的JSONObject
79+
*
80+
* @param code
81+
* @param msg
82+
* @param warn
83+
* @param isRoot
84+
* @return
85+
*/
86+
public static <M extends Map<String, Object>> M newResult(int code, String msg, String warn, boolean isRoot) {
87+
return extendResult(null, code, msg, warn, isRoot);
88+
}
89+
90+
/**
91+
* 添加JSONObject的状态内容,一般用于错误提示结果
92+
*
93+
* @param object
94+
* @param code
95+
* @param msg
96+
* @return
97+
*/
98+
public static <M extends Map<String, Object>> M extendResult(M object, int code, String msg, String warn, boolean isRoot) {
99+
return (M) ERR_PARSER.extendResult(JSON.createJSONObject(object), code, msg, warn, isRoot);
100+
}
101+
102+
103+
/**
104+
* 添加请求成功的状态内容
105+
*
106+
* @param object
107+
* @return
108+
*/
109+
public M extendSuccessResult(M object) {
110+
return extendSuccessResult(object, false);
111+
}
112+
113+
public M extendSuccessResult(M object, boolean isRoot) {
114+
return extendSuccessResult(object, null, isRoot);
115+
}
116+
117+
/**添加请求成功的状态内容
118+
* @param object
119+
* @param isRoot
120+
* @return
121+
*/
122+
public static <M extends Map<String, Object>> M extendSuccessResult(M object, String warn, boolean isRoot) {
123+
return extendResult(object, JSONResponse.CODE_SUCCESS, JSONResponse.MSG_SUCCEED, warn, isRoot);
124+
}
125+
126+
/**获取请求成功的状态内容
127+
* @return
128+
*/
129+
public static <M extends Map<String, Object>> M newSuccessResult() {
130+
return newSuccessResult(null);
131+
}
132+
133+
/**获取请求成功的状态内容
134+
* @param warn
135+
* @return
136+
*/
137+
public static <M extends Map<String, Object>> M newSuccessResult(String warn) {
138+
return newSuccessResult(warn, false);
139+
}
140+
141+
/**获取请求成功的状态内容
142+
* @param warn
143+
* @param isRoot
144+
* @return
145+
*/
146+
public static <M extends Map<String, Object>> M newSuccessResult(String warn, boolean isRoot) {
147+
return newResult(JSONResponse.CODE_SUCCESS, JSONResponse.MSG_SUCCEED, warn, isRoot);
148+
}
149+
150+
/**添加请求成功的状态内容
151+
* @param object
152+
* @param e
153+
* @return
154+
*/
155+
public static <M extends Map<String, Object>> M extendErrorResult(M object, Throwable e) {
156+
return extendErrorResult(object, e, false);
157+
}
158+
/**添加请求成功的状态内容
159+
* @param object
160+
* @param e
161+
* @param isRoot
162+
* @return
163+
*/
164+
public static <M extends Map<String, Object>> M extendErrorResult(M object, Throwable e, boolean isRoot) {
165+
return extendErrorResult(object, e, null, null, isRoot);
166+
}
167+
/**添加请求成功的状态内容
168+
* @param object
169+
* @return
170+
*/
171+
public static <M extends Map<String, Object>> M extendErrorResult(M object, Throwable e, RequestMethod requestMethod, String url, boolean isRoot) {
172+
return (M) ERR_PARSER.extendErrorResult(JSON.createJSONObject(object), e, requestMethod, url, isRoot);
173+
}
174+
175+
public static <M extends Map<String, Object>> M newErrorResult(Exception e) {
176+
return newErrorResult(e, false);
177+
}
178+
public static <M extends Map<String, Object>> M newErrorResult(Exception e, boolean isRoot) {
179+
return (M) ERR_PARSER.newErrorResult(e, isRoot);
180+
}
181+
182+
60183
public String parse(RequestMethod method, String request, HttpSession session) {
61184
return newParser(session, method).parse(request);
62185
}
63186

64187
public String parseByTag(RequestMethod method, String tag, Map<String, String> params, String request, HttpSession session) {
65188
APIJSONParser<T, M, L> parser = newParser(null, null);
66-
M req = parser.wrapRequest(method, tag, JSON.parseObject(request), false, (JSONCreator<M, L>) APIJSON_CREATOR);
189+
M req = parser.wrapRequest(method, tag, JSON.parseObject(request), false);
67190
if (req == null) {
68191
req = JSON.createJSONObject();
69192
}
@@ -208,7 +331,7 @@ public String crudByTag(String method, String tag, Map<String, String> params, S
208331
// * @see {@link RequestMethod#GET}
209332
// */
210333
// public String listByTag(String tag, String request, HttpSession session) {
211-
// return parseByTag(GET, tag + apijson.JSONObject.KEY_ARRAY, request, session);
334+
// return parseByTag(GET, tag + apijson.JSONMap.KEY_ARRAY, request, session);
212335
// }
213336

214337
/**获取
@@ -396,7 +519,7 @@ public String router(String method, String tag, Map<String, String> params, Stri
396519
}
397520

398521
@SuppressWarnings("unchecked")
399-
APIJSONCreator<T, M, L> creator = (APIJSONCreator<T, M, L>) APIJSONParser.APIJSON_CREATOR;
522+
APIJSONCreator<T, M, L> creator = (APIJSONCreator<T, M, L>) APIJSONApplication.DEFAULT_APIJSON_CREATOR;
400523
if (result == null && Log.DEBUG && APIJSONVerifier.DOCUMENT_MAP.isEmpty()) {
401524

402525
//获取指定的JSON结构 <<<<<<<<<<<<<<
@@ -576,7 +699,7 @@ public M reload(String type) {
576699
* @param defaults
577700
* @return 返回类型设置为 Object 是为了子类重写时可以有返回值,避免因为冲突而另写一个有返回值的登录方法
578701
*/
579-
public Object login(@NotNull HttpSession session, Visitor<Long> visitor, Integer version, Boolean format, M defaults) {
702+
public Object login(@NotNull HttpSession session, @NotNull Visitor<Long> visitor, Integer version, Boolean format, M defaults) {
580703
//登录状态保存至session
581704
session.setAttribute(VISITOR_ID, visitor.getId()); //用户id
582705
session.setAttribute(VISITOR_, visitor); //用户
@@ -599,7 +722,7 @@ public Object logout(@NotNull HttpSession session) {
599722

600723

601724

602-
// public JSONObject listMethod(String request) {
725+
// public JSONMap listMethod(String request) {
603726
// if (Log.DEBUG == false) {
604727
// return APIJSONParser.newErrorResult(new IllegalAccessException("非 DEBUG 模式下不允许使用 UnitAuto 单元测试!"));
605728
// }
@@ -610,10 +733,10 @@ public Object logout(@NotNull HttpSession session) {
610733
// AsyncContext asyncContext = servletRequest.startAsync();
611734
//
612735
// final boolean[] called = new boolean[] { false };
613-
// MethodUtil.Listener<JSONObject> listener = new MethodUtil.Listener<JSONObject>() {
736+
// MethodUtil.Listener<JSONMap> listener = new MethodUtil.Listener<JSONMap>() {
614737
//
615738
// @Override
616-
// public void complete(JSONObject data, Method method, InterfaceProxy proxy, Object... extras) throws Exception {
739+
// public void complete(JSONMap data, Method method, InterfaceProxy proxy, Object... extras) throws Exception {
617740
//
618741
// ServletResponse servletResponse = called[0] ? null : asyncContext.getResponse();
619742
// if (servletResponse == null) { // || servletResponse.isCommitted()) { // isCommitted 在高并发时可能不准,导致写入多次
@@ -646,7 +769,7 @@ public Object logout(@NotNull HttpSession session) {
646769
// MethodUtil.invokeMethod(request, null, listener);
647770
// }
648771
// catch (Exception e) {
649-
// Log.e(TAG, "invokeMethod try { JSONObject req = JSON.parseObject(request); ... } catch (Exception e) { \n" + e.getMessage());
772+
// Log.e(TAG, "invokeMethod try { JSONMap req = JSON.parseObject(request); ... } catch (Exception e) { \n" + e.getMessage());
650773
// try {
651774
// listener.complete(MethodUtil.JSON_CALLBACK.newErrorResult(e));
652775
// }

src/main/java/apijson/framework/APIJSONCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
1+
/*Copyright ©2016 APIJSON(https://github.com/APIJSON)
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

src/main/java/apijson/framework/APIJSONFunctionParser.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
1+
/*Copyright ©2016 APIJSON(https://github.com/APIJSON)
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -37,12 +37,9 @@
3737
public class APIJSONFunctionParser<T, M extends Map<String, Object>, L extends List<Object>> extends AbstractFunctionParser<T, M, L> {
3838
public static final String TAG = "APIJSONFunctionParser";
3939

40-
@NotNull
41-
public static APIJSONCreator<?, ? extends Map<String, Object>, ? extends List<Object>> APIJSON_CREATOR;
4240
@NotNull
4341
public static final String[] ALL_METHODS;
4442
static {
45-
APIJSON_CREATOR = new APIJSONCreator<>();
4643
ALL_METHODS = new String[]{ GET.name(), HEAD.name(), GETS.name(), HEADS.name(), POST.name(), PUT.name(), DELETE.name() };
4744
}
4845

@@ -125,10 +122,8 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M init(
125122
public static <T, M extends Map<String, Object>, L extends List<Object>> M init(boolean shutdownWhenServerError
126123
, APIJSONCreator<T, M, L> creator, M table) throws ServerException {
127124
if (creator == null) {
128-
creator = (APIJSONCreator<T, M, L>) APIJSON_CREATOR;
125+
creator = (APIJSONCreator<T, M, L>) APIJSONApplication.DEFAULT_APIJSON_CREATOR;
129126
}
130-
APIJSON_CREATOR = creator;
131-
132127

133128
boolean isAll = table == null || table.isEmpty();
134129

@@ -294,7 +289,7 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> M init(
294289
}
295290

296291
for (String method : methods) {
297-
APIJSONParser<T, M, L> parser = (APIJSONParser<T, M, L>) APIJSON_CREATOR.createParser();
292+
APIJSONParser<T, M, L> parser = APIJSONApplication.createParser();
298293
M r = parser.setMethod(RequestMethod.valueOf(method))
299294
.setNeedVerify(false)
300295
.parseResponse(demo);
@@ -342,7 +337,7 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> void te
342337
request.put("id", 10);
343338
request.put("i0", i0);
344339
request.put("i1", i1);
345-
JSONArray arr = new JSONArray();
340+
L arr = JSON.createJSONArray();
346341
arr.add(JSON.createJSONObject());
347342
request.put("arr", arr);
348343

@@ -361,18 +356,18 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> void te
361356
object.put("key", "success");
362357
request.put("object", object);
363358

364-
APIJSONParser<T, M, L> parser = (APIJSONParser<T, M, L>) APIJSON_CREATOR.createParser();
359+
APIJSONParser<T, M, L> parser = APIJSONApplication.createParser();
365360
parser.setRequest(request);
366361
if (functionParser == null) {
367-
functionParser = (APIJSONFunctionParser<T, M, L>) APIJSON_CREATOR.createFunctionParser();
362+
functionParser = APIJSONApplication.createFunctionParser();
368363
functionParser.setParser(parser);
369364
functionParser.setMethod(parser.getMethod());
370365
functionParser.setTag(parser.getTag());
371366
functionParser.setVersion(parser.getVersion());
372367
functionParser.setRequest(parser.getRequest());
373368

374369
//if (functionParser instanceof APIJSONFunctionParser) {
375-
((APIJSONFunctionParser) functionParser).setSession(parser.getSession());
370+
((APIJSONFunctionParser<?, ?, ?>) functionParser).setSession(parser.getSession());
376371
//}
377372
}
378373

src/main/java/apijson/framework/APIJSONObjectParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
1+
/*Copyright ©2016 APIJSON(https://github.com/APIJSON)
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)