Skip to content

Commit ae023cb

Browse files
committed
所有流程类都统一JSON 的序列化和反序列化,避免转换报错 cannot cast apijson.JSONObject to com.alibaba.fastjson.JSONObject 等
1 parent 7ef7b26 commit ae023cb

File tree

12 files changed

+181
-137
lines changed

12 files changed

+181
-137
lines changed

APIJSONORM/src/main/java/apijson/JSON.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,15 @@ public <T> List<T> parseArray(Object json, Class<T> clazz) {
6363

6464
};
6565

66-
public static JSONCreator<? extends Map<String, Object>, ? extends List<Object>> DEFAULT_JSON_CREATOR = DEFAULT_JSON_PARSER;
66+
// public static JSONCreator<? extends Map<String, Object>, ? extends List<Object>> DEFAULT_JSON_CREATOR = DEFAULT_JSON_PARSER;
6767

68+
public static Object createJSONObject() {
69+
return DEFAULT_JSON_PARSER.createJSONObject();
70+
}
71+
72+
public static Object createJSONArray() {
73+
return DEFAULT_JSON_PARSER.createJSONArray();
74+
}
6875

6976
public static Object parseJSON(Object json) {
7077
if (json instanceof Boolean || json instanceof Number || json instanceof Enum<?>) {
@@ -565,4 +572,5 @@ public static String getString(Map<String, Object> map, String key) {
565572

566573
return value.toString();
567574
}
575+
568576
}

APIJSONORM/src/main/java/apijson/JSONArray.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import java.util.*;
88

9-
import apijson.orm.exception.UnsupportedDataTypeException;
10-
119
/**
1210
* Custom JSONArray implementation based on ArrayList to replace com.alibaba.fastjson.JSONArray
1311
* Maintains same API as fastjson but uses standard Java List implementation
@@ -68,13 +66,12 @@ public JSONObject getJSONObject(int index) {
6866
}
6967

7068
Object obj = get(index);
71-
if (obj instanceof Map) {
72-
@SuppressWarnings("unchecked")
73-
Map<String, Object> map = (Map<String, Object>) obj;
74-
return new JSONObject(map);
75-
} else if (obj instanceof JSONObject) {
69+
if (obj instanceof JSONObject) {
7670
return (JSONObject) obj;
7771
}
72+
else if (obj instanceof Map) {
73+
return new JSONObject(obj);
74+
}
7875
return null;
7976
}
8077

APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @author Lemon
2323
*/
2424
public abstract class AbstractFunctionParser<T, M extends Map<String, Object>, L extends List<Object>>
25-
implements FunctionParser<T, M, L>, JSONParser<M, L> {
25+
implements FunctionParser<T, M, L> { //, JSONParser<M, L> {
2626
private static final String TAG = "AbstractFunctionParser";
2727

2828
/**是否解析参数 key 的对应的值,不用手动编码 curObj.getString(key)

APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @author Lemon
3131
*/
3232
public abstract class AbstractObjectParser<T, M extends Map<String, Object>, L extends List<Object>>
33-
implements ObjectParser<T, M, L>, JSONParser<M, L> {
33+
implements ObjectParser<T, M, L> { //, JSONParser<M, L> {
3434
private static final String TAG = "AbstractObjectParser";
3535

3636
@NotNull
@@ -207,11 +207,11 @@ public AbstractObjectParser<T, M, L> parse(String name, boolean isReuse) throws
207207

208208
breakParse = false;
209209

210-
response = createJSONObject(); // must init
210+
response = (M) JSON.createJSONObject(); // must init
211211
sqlResponse = null; // must init
212212

213213
if (isReuse == false) {
214-
sqlRequest = createJSONObject(); // must init
214+
sqlRequest = (M) JSON.createJSONObject(); // must init
215215

216216
customMap = null; // must init
217217
functionMap = null; // must init
@@ -672,7 +672,7 @@ public void onPUTArrayParse(@NotNull String key, @NotNull L array) throws Except
672672
//GET > add all 或 remove all > PUT > remove key
673673

674674
//GET <<<<<<<<<<<<<<<<<<<<<<<<<
675-
M rq = createJSONObject();
675+
M rq = (M) JSON.createJSONObject();
676676
rq.put(JSONRequest.KEY_ID, request.get(JSONRequest.KEY_ID));
677677
rq.put(JSONRequest.KEY_COLUMN, realKey);
678678
M rp = parseResponse(RequestMethod.GET, table, null, rq, null, false);
@@ -716,7 +716,7 @@ public void onPUTArrayParse(@NotNull String key, @NotNull L array) throws Except
716716
);
717717
}
718718

719-
targetArray = createJSONArray();
719+
targetArray = (L) JSON.createJSONArray();
720720
}
721721

722722
for (int i = 0; i < array.size(); i++) {
@@ -769,7 +769,7 @@ public void onTableArrayParse(String key, L valueArray) throws Exception {
769769
String childKey = key.substring(0, key.length() - JSONRequest.KEY_ARRAY.length());
770770

771771
int allCount = 0;
772-
L ids = createJSONArray();
772+
L ids = (L) JSON.createJSONArray();
773773

774774
int version = parser.getVersion();
775775
int maxUpdateCount = parser.getMaxUpdateCount();
@@ -784,7 +784,7 @@ public void onTableArrayParse(String key, L valueArray) throws Exception {
784784

785785
cfg.setTable(childKey); // Request 表 structure 中配置 "ALLOW_PARTIAL_UPDATE_FAILED": "Table[],key[],key:alias[]" 自动配置
786786
boolean allowPartialFailed = cfg.allowPartialUpdateFailed();
787-
L failedIds = allowPartialFailed ? createJSONArray() : null;
787+
L failedIds = allowPartialFailed ? (L) JSON.createJSONArray() : null;
788788

789789
int firstFailIndex = -1;
790790
M firstFailReq = null;
@@ -805,7 +805,7 @@ public void onTableArrayParse(String key, L valueArray) throws Exception {
805805
}
806806

807807
Object id = item.get(idKey);
808-
M req = createJSONObject();
808+
M req = (M) JSON.createJSONObject();
809809
req.put(childKey, item);
810810

811811
M result = null;
@@ -865,7 +865,7 @@ public void onTableArrayParse(String key, L valueArray) throws Exception {
865865
allResult.put("failedCount", failedCount);
866866
allResult.put("failedIdList", failedIds);
867867

868-
M failObj = createJSONObject();
868+
M failObj = (M) JSON.createJSONObject();
869869
failObj.put("index", firstFailIndex);
870870
failObj.put(childKey, firstFailReq);
871871

@@ -983,7 +983,7 @@ public AbstractObjectParser<T, M, L> setSQLConfig(int count, int page, int posit
983983
public AbstractObjectParser<T, M, L> executeSQL() throws Exception {
984984
//执行SQL操作数据库
985985
if (isTable == false) {//提高性能
986-
sqlResponse = createJSONObject();
986+
sqlResponse = (M) JSON.createJSONObject();
987987
sqlResponse.putAll(sqlRequest);
988988
}
989989
else {

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* @author Lemon
3939
*/
4040
public abstract class AbstractParser<T, M extends Map<String, Object>, L extends List<Object>>
41-
implements Parser<T, M, L>, ParserCreator<T, M, L>, VerifierCreator<T, M, L>, SQLCreator<T, M, L>, JSONParser<M, L> {
41+
implements Parser<T, M, L>, ParserCreator<T, M, L>, VerifierCreator<T, M, L>, SQLCreator<T, M, L> { //, JSONParser<M, L> {
4242
protected static final String TAG = "AbstractParser";
4343

4444
/**
@@ -463,7 +463,7 @@ public M parseResponse(String request) {
463463
+ requestMethod + "/parseResponse request = \n" + request + "\n\n");
464464

465465
try {
466-
requestObject = parseObject(request);
466+
requestObject = (M) JSON.parseObject(request);
467467
if (requestObject == null) {
468468
throw new UnsupportedEncodingException("JSON格式不合法!");
469469
}
@@ -577,7 +577,17 @@ public M parseResponse(M request) {
577577

578578
requestObject = error == null ? extendSuccessResult(requestObject, warn, isRoot) : extendErrorResult(requestObject, error, requestMethod, getRequestURL(), isRoot);
579579

580-
M res = (globalFormat != null && globalFormat) && JSONResponse.isSuccess(requestObject) ? JSONResponse.format(requestObject, this) : requestObject;
580+
M res = (globalFormat != null && globalFormat) && JSONResponse.isSuccess(requestObject) ? JSONResponse.format(requestObject, new JSONCreator<M, List<Object>>() {
581+
@Override
582+
public M createJSONObject() {
583+
return (M) JSON.createJSONObject();
584+
}
585+
586+
@Override
587+
public List<Object> createJSONArray() {
588+
return (L) JSON.createJSONArray();
589+
}
590+
}) : requestObject;
581591

582592
long endTime = System.currentTimeMillis();
583593
long duration = endTime - startTime;
@@ -692,7 +702,7 @@ public M wrapRequest(RequestMethod method, String tag, M object, boolean isStruc
692702
String arrKey = key + "[]";
693703

694704
if (target.containsKey(arrKey) == false) {
695-
target.put(arrKey, createJSONArray());
705+
target.put(arrKey, JSON.createJSONArray());
696706
}
697707

698708
try {
@@ -792,7 +802,7 @@ public M extendResult(M object, int code, String msg, String warn, boolean isRoo
792802
msg = index >= 0 ? msg.substring(0, index) : msg;
793803

794804
if (object == null) {
795-
object = createJSONObject();
805+
object = (M) JSON.createJSONObject();
796806
}
797807

798808
if (object.get(JSONResponse.KEY_OK) == null) {
@@ -1386,7 +1396,7 @@ else if (childKeys.length == 1 && JSONRequest.isTableKey(childKeys[0])) { //
13861396

13871397
boolean isExtract = true;
13881398

1389-
response = createJSONArray();
1399+
response = (L) JSON.createJSONArray();
13901400
//生成size个
13911401
for (int i = 0; i < (isSubquery ? 1 : size); i++) {
13921402
parent = onObjectParse(request, isSubquery ? parentPath : path, isSubquery ? name : "" + i, config.setType(SQLConfig.TYPE_ITEM).setPosition(i), isSubquery, null);
@@ -2043,7 +2053,7 @@ public M executeSQL(SQLConfig<T, M, L> config, boolean isSubquery) throws Except
20432053
config.setTag(getTag());
20442054

20452055
if (isSubquery) {
2046-
M sqlObj = createJSONObject();
2056+
M sqlObj = (M) JSON.createJSONObject();
20472057
sqlObj.put(KEY_CONFIG, config);
20482058
return sqlObj;//容易丢失信息 JSON.parseObject(config);
20492059
}
@@ -2066,13 +2076,13 @@ public M executeSQL(SQLConfig<T, M, L> config, boolean isSubquery) throws Except
20662076
result = res;
20672077
}
20682078
else {
2069-
result = createJSONObject();
2079+
result = (M) JSON.createJSONObject();
20702080
result.put(KEY_EXPLAIN, explainResult);
20712081
result.putAll(res);
20722082
}
20732083
}
20742084
else {//如果是更新请求,不执行explain,但可以返回sql
2075-
result = createJSONObject();
2085+
result = (M) JSON.createJSONObject();
20762086
result.put(KEY_SQL, config.getSQL(false));
20772087
result.putAll(res);
20782088
}
@@ -2227,7 +2237,7 @@ protected M getRequestStructure(RequestMethod method, String tag, int version) t
22272237
}
22282238

22292239
protected M batchVerify(RequestMethod method, String tag, int version, String name, @NotNull M request, int maxUpdateCount, SQLCreator<T, M, L> creator) throws Exception {
2230-
M correctRequest = createJSONObject();
2240+
M correctRequest = (M) JSON.createJSONObject();
22312241
List<String> removeTmpKeys = new ArrayList<>(); // 请求json里面的临时变量,不需要带入后面的业务中,比如 @post、@get等
22322242

22332243
Set<String> reqSet = request == null ? null : request.keySet();
@@ -2427,7 +2437,7 @@ protected M batchVerify(RequestMethod method, String tag, int version, String na
24272437
String _tag = buildTag(request, key, method, tag);
24282438
M object = getRequestStructure(_method, _tag, version);
24292439
if (method == RequestMethod.CRUD && StringUtil.isEmpty(tag, true)) {
2430-
M requestItem = createJSONObject();
2440+
M requestItem = (M) JSON.createJSONObject();
24312441
requestItem.put(key, obj);
24322442
Map<String, Object> ret = objectVerify(_method, _tag, version, name, requestItem, maxUpdateCount, creator, object);
24332443
correctRequest.put(key, ret.get(key));
@@ -2490,7 +2500,17 @@ protected String buildTag(Map<String, Object> request, String key, RequestMethod
24902500
protected M objectVerify(RequestMethod method, String tag, int version, String name, @NotNull M request
24912501
, int maxUpdateCount, SQLCreator<T, M, L> creator, M object) throws Exception {
24922502
// 获取指定的JSON结构 >>>>>>>>>>>>>>
2493-
M target = wrapRequest(method, tag, object, true, this);
2503+
M target = wrapRequest(method, tag, object, true, new JSONCreator<M, L>() {
2504+
@Override
2505+
public M createJSONObject() {
2506+
return (M) JSON.createJSONObject();
2507+
}
2508+
2509+
@Override
2510+
public L createJSONArray() {
2511+
return (L) JSON.createJSONArray();
2512+
}
2513+
});
24942514
// Map<String, Object> clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
24952515
return getVerifier().verifyRequest(method, name, target, request, maxUpdateCount, getGlobalDatabase(), getGlobalSchema(), creator);
24962516
}

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* @author Lemon
4747
*/
4848
public abstract class AbstractSQLConfig<T, M extends Map<String, Object>, L extends List<Object>>
49-
implements SQLConfig<T, M, L>, JSONCreator<M, L> {
49+
implements SQLConfig<T, M, L> { // }, JSONCreator<M, L> {
5050
private static final String TAG = "AbstractSQLConfig";
5151

5252
/**
@@ -4744,7 +4744,7 @@ public static String getCondition(boolean not, String condition, boolean addOute
47444744
*/
47454745
@NotNull
47464746
public L newJSONArray(Object obj) {
4747-
L array = createJSONArray();
4747+
L array = (L) JSON.createJSONArray();
47484748
if (obj != null) {
47494749
if (obj instanceof Collection) {
47504750
array.addAll((Collection<?>) obj);
@@ -6049,13 +6049,13 @@ else if (w.startsWith("!")) {
60496049
}
60506050
}
60516051
}
6052-
else if (newHaving instanceof JSONObject) {
6052+
else if (newHaving instanceof Map<?, ?>) {
60536053
if (isHavingAnd) {
60546054
throw new IllegalArgumentException(table + ":{ " + havingKey + ":value } 里的 value 类型不合法!"
60556055
+ "@having&:value 中 value 只能是 String,@having:value 中 value 只能是 String 或 JSONObject !");
60566056
}
60576057

6058-
JSONObject havingObj = (JSONObject) newHaving;
6058+
JSONObject havingObj = new JSONObject(newHaving);
60596059
Set<Entry<String, Object>> havingSet = havingObj.entrySet();
60606060
for (Entry<String, Object> entry : havingSet) {
60616061
String k = entry == null ? null : entry.getKey();

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @author Lemon
2727
*/
2828
public abstract class AbstractSQLExecutor<T, M extends Map<String, Object>, L extends List<Object>>
29-
implements SQLExecutor<T, M, L>, JSONParser<M, L> {
29+
implements SQLExecutor<T, M, L> { // , JSONParser<M, L> {
3030
private static final String TAG = "AbstractSQLExecutor";
3131
//是否返回 值为null的字段
3232
public static boolean ENABLE_OUTPUT_NULL_COLUMN = false;

0 commit comments

Comments
 (0)