Skip to content

Commit 42ca342

Browse files
committed
完善和优化 JSON 处理,解决 fastjson 等 JSON 库自动序列化 AbstractSQLConfig.getSetString 等方法抛异常;优化代码
1 parent aa35bf6 commit 42ca342

17 files changed

+997
-867
lines changed

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

+270-170
Large diffs are not rendered by default.

APIJSONORM/src/main/java/apijson/JSONCreator.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,31 @@ public interface JSONCreator<M extends Map<String, Object>, L extends List<Objec
2020
M createJSONObject();
2121

2222
@NotNull
23-
default M createJSONObject(Map<? extends String, ?> m) {
23+
default M createJSONObject(String key, Object value) {
2424
M obj = createJSONObject();
25-
if (m != null && ! m.isEmpty()) {
26-
obj.putAll(m);
25+
obj.put(key, value);
26+
return obj;
27+
}
28+
29+
@NotNull
30+
default M createJSONObject(Map<? extends String, ?> map) {
31+
M obj = createJSONObject();
32+
if (map != null && ! map.isEmpty()) {
33+
obj.putAll(map);
2734
}
2835
return obj;
2936
}
3037

3138
@NotNull
3239
L createJSONArray();
3340

41+
@NotNull
42+
default L createJSONArray(Object obj){
43+
L arr = createJSONArray();
44+
arr.add(obj);
45+
return arr;
46+
}
47+
3448
@NotNull
3549
default L createJSONArray(List<?> l){
3650
L arr = createJSONArray();

APIJSONORM/src/main/java/apijson/JSONObject.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@
66
package apijson;
77

88
import java.util.*;
9-
import java.util.Map.Entry;
10-
import java.util.function.BiConsumer;
11-
import java.util.function.BiFunction;
12-
import java.util.function.Function;
139

14-
import apijson.orm.exception.UnsupportedDataTypeException;
1510

1611
/**use this class instead of com.alibaba.fastjson.JSONObject
1712
* @author Lemon
1813
* @see #put
1914
* @see #puts
2015
* @see #putsAll
2116
*/
22-
public class JSONObject extends JSON implements Map<String, Object> {
17+
public class JSONObject extends JSON implements Map<String, Object> {
2318
private static final String TAG = "JSONObject";
2419

2520
private final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
@@ -699,7 +694,7 @@ public JSONObject putsAll(Map<? extends String, ? extends Object> map) {
699694
public boolean getBooleanValue(String key) {
700695
try {
701696
return JSON.getBooleanValue(this, key);
702-
} catch (UnsupportedDataTypeException e) {
697+
} catch (IllegalArgumentException e) {
703698
return false;
704699
}
705700
}
@@ -712,7 +707,7 @@ public boolean getBooleanValue(String key) {
712707
public int getIntValue(String key) {
713708
try {
714709
return JSON.getIntValue(this, key);
715-
} catch (UnsupportedDataTypeException e) {
710+
} catch (IllegalArgumentException e) {
716711
return 0;
717712
}
718713
}
@@ -725,7 +720,7 @@ public int getIntValue(String key) {
725720
public long getLongValue(String key) {
726721
try {
727722
return JSON.getLongValue(this, key);
728-
} catch (UnsupportedDataTypeException e) {
723+
} catch (IllegalArgumentException e) {
729724
return 0L;
730725
}
731726
}
@@ -738,7 +733,7 @@ public long getLongValue(String key) {
738733
public double getDoubleValue(String key) {
739734
try {
740735
return JSON.getDoubleValue(this, key);
741-
} catch (UnsupportedDataTypeException e) {
736+
} catch (IllegalArgumentException e) {
742737
return 0.0;
743738
}
744739
}
@@ -762,7 +757,7 @@ public JSONObject getJSONObject(String key) {
762757
try {
763758
Map<String, Object> map = JSON.getMap(this, key);
764759
return map != null ? new JSONObject(map) : null;
765-
} catch (UnsupportedDataTypeException e) {
760+
} catch (IllegalArgumentException e) {
766761
return null;
767762
}
768763
}
@@ -776,7 +771,7 @@ public JSONArray getJSONArray(String key) {
776771
try {
777772
List<Object> list = JSON.getList(this, key);
778773
return list != null ? new JSONArray(list) : null;
779-
} catch (UnsupportedDataTypeException e) {
774+
} catch (IllegalArgumentException e) {
780775
return null;
781776
}
782777
}

APIJSONORM/src/main/java/apijson/JSONRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @author Lemon
1616
* @see #puts
1717
* @see #toArray
18-
* @use JSONRequest request = new JSONRequest(...);
18+
* @use JSONRequest request = JSON.createJSONObject(...);
1919
* <br> request.puts(...);//not a must
2020
* <br> request.toArray(...);//not a must
2121
*/

APIJSONORM/src/main/java/apijson/JSONResponse.java

+42-14
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
package apijson;
77

8-
import apijson.orm.exception.UnsupportedDataTypeException;
9-
108
import java.util.*;
119

1210
import static apijson.JSON.parseObject;
@@ -19,7 +17,7 @@
1917
* <br> User user = response.getObject(User.class);//not a must
2018
* <br> List<Comment> commenntList = response.getList("Comment[]", Comment.class);//not a must
2119
*/
22-
public class JSONResponse<M extends Map<String, Object>, L extends List<Object>> extends apijson.JSONObject {
20+
public class JSONResponse<M extends Map<String, Object>, L extends List<Object>> extends apijson.JSONObject implements Map<String, Object> {
2321
private static final long serialVersionUID = 1L;
2422

2523
// 节约性能和减少 bug,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
@@ -113,11 +111,11 @@ public String getMsg() {
113111
return getString(KEY_MSG);
114112
}
115113
/**获取状态描述
116-
* @param reponse
114+
* @param response
117115
* @return
118116
*/
119-
public static String getMsg(Map<String, Object> reponse) {
120-
return reponse == null ? null : JSON.getString(reponse, KEY_MSG);
117+
public static String getMsg(Map<String, Object> response) {
118+
return response == null ? null : JSON.getString(response, KEY_MSG);
121119
}
122120
/**获取id
123121
* @return
@@ -171,7 +169,7 @@ public static boolean isSuccess(int code) {
171169
* @param response
172170
* @return
173171
*/
174-
public static boolean isSuccess(JSONResponse response) {
172+
public static boolean isSuccess(JSONResponse<?, ?> response) {
175173
return response != null && response.isSuccess();
176174
}
177175
/**是否成功
@@ -181,7 +179,7 @@ public static boolean isSuccess(JSONResponse response) {
181179
public static boolean isSuccess(Map<String, Object> response) {
182180
try {
183181
return response != null && isSuccess(JSON.getIntValue(response, KEY_CODE));
184-
} catch (UnsupportedDataTypeException e) {
182+
} catch (IllegalArgumentException e) {
185183
return false;
186184
}
187185
}
@@ -203,10 +201,17 @@ public static boolean isExist(int count) {
203201
* @param response
204202
* @return
205203
*/
206-
public static boolean isExist(JSONResponse response) {
204+
public static boolean isExist(JSONResponse<?, ?> response) {
207205
return response != null && response.isExist();
208206
}
209207

208+
/**获取内部的JSONResponse
209+
* @param key
210+
* @return
211+
*/
212+
public JSONResponse<M, L> getJSONResponse(String key) {
213+
return getObject(key, JSONResponse.class, null);
214+
}
210215
/**获取内部的JSONResponse
211216
* @param key
212217
* @return
@@ -220,12 +225,20 @@ public JSONResponse<M, L> getJSONResponse(String key, JSONParser<M, L> parser) {
220225
// * @param key
221226
// * @return
222227
// */
223-
// public static JSONResponse getJSONResponse(M response, String key) {
228+
// public static JSONResponse getJSONResponse(JSONRequest response, String key) {
224229
// return response == null ? null : response.getObject(key, JSONResponse.class);
225230
// }
226231
//状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
227232

228233

234+
/**
235+
* key = clazz.getSimpleName()
236+
* @param clazz
237+
* @return
238+
*/
239+
public <T> T getObject(Class<T> clazz) {
240+
return getObject(clazz == null ? "" : clazz.getSimpleName(), clazz, (JSONParser<M, L>) DEFAULT_JSON_PARSER);
241+
}
229242
/**
230243
* key = clazz.getSimpleName()
231244
* @param clazz
@@ -234,6 +247,14 @@ public JSONResponse<M, L> getJSONResponse(String key, JSONParser<M, L> parser) {
234247
public <T> T getObject(Class<T> clazz, JSONParser<M, L> parser) {
235248
return getObject(clazz == null ? "" : clazz.getSimpleName(), clazz, parser);
236249
}
250+
/**
251+
* @param key
252+
* @param clazz
253+
* @return
254+
*/
255+
public <T> T getObject(String key, Class<T> clazz) {
256+
return getObject(this, key, clazz, (JSONParser<M, L>) DEFAULT_JSON_PARSER);
257+
}
237258
/**
238259
* @param key
239260
* @param clazz
@@ -253,6 +274,13 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> T getOb
253274
return toObject(object == null ? null : JSON.get(object, formatObjectKey(key)), clazz, parser);
254275
}
255276

277+
/**
278+
* @param clazz
279+
* @return
280+
*/
281+
public <T> T toObject(Class<T> clazz) {
282+
return toObject(clazz, null);
283+
}
256284
/**
257285
* @param clazz
258286
* @return
@@ -267,7 +295,7 @@ public <T> T toObject(Class<T> clazz, JSONParser<M, L> parser) {
267295
*/
268296
public static <T, M extends Map<String, Object>, L extends List<Object>> T toObject(
269297
Map<String, Object> object, Class<T> clazz, JSONParser<M, L> parser) {
270-
return parseObject(JSON.toJSONString(object), clazz, parser);
298+
return parseObject(object, clazz, parser);
271299
}
272300

273301

@@ -345,7 +373,7 @@ public static JSONArray getArray(Map<String, Object> object, String key) {
345373
// /**
346374
// * @return
347375
// */
348-
// public M format() {
376+
// public JSONRequest format() {
349377
// return format(this);
350378
// }
351379
/**格式化key名称
@@ -390,7 +418,7 @@ public static <M extends Map<String, Object>, L extends List<Object>> M format(f
390418
if (value instanceof List<?>) {//JSONArray,遍历来format内部项
391419
formatedObject.put(formatArrayKey(key), format((L) value, creator));
392420
}
393-
else if (value instanceof Map<?, ?>) {//M,往下一级提取
421+
else if (value instanceof Map<?, ?>) {//JSONRequest,往下一级提取
394422
formatedObject.put(formatObjectKey(key), format((M) value, creator));
395423
}
396424
else {//其它Object,直接填充
@@ -436,7 +464,7 @@ public static <M extends Map<String, Object>, L extends List<Object>> L format(f
436464
if (value instanceof List<?>) {//JSONArray,遍历来format内部项
437465
formatedArray.add(format((L) value, creator));
438466
}
439-
else if (value instanceof Map<?, ?>) {//M,往下一级提取
467+
else if (value instanceof Map<?, ?>) {//JSONRequest,往下一级提取
440468
formatedArray.add(format((M) value, creator));
441469
}
442470
else {//其它Object,直接填充

APIJSONORM/src/main/java/apijson/Log.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Log {
1414

1515
public static boolean DEBUG = true;
1616

17-
public static final String VERSION = "7.9.0";
17+
public static final String VERSION = "8.0.0";
1818
public static final String KEY_SYSTEM_INFO_DIVIDER = "\n---|-----APIJSON SYSTEM INFO-----|---\n";
1919

2020
public static final String OS_NAME;

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.util.*;
1717

1818
import static apijson.orm.AbstractSQLConfig.PATTERN_SCHEMA;
19-
import static apijson.orm.SQLConfig.TYPE_ITEM;
2019

2120
/**可远程调用的函数类
2221
* @author Lemon
@@ -38,7 +37,7 @@ public abstract class AbstractFunctionParser<T, M extends Map<String, Object>, L
3837

3938
// <methodName, JSONObject>
4039
// <isContain, <arguments:"array,key", tag:null, methods:null>>
41-
public static Map<String, ScriptExecutor> SCRIPT_EXECUTOR_MAP;
40+
public static Map<String, ScriptExecutor<?, ? extends Map<String, Object>, ? extends List<Object>>> SCRIPT_EXECUTOR_MAP;
4241
public static Map<String, Map<String, Object>> FUNCTION_MAP;
4342

4443
static {
@@ -288,7 +287,7 @@ public <T extends Object> T getArgVal(String path, Class<T> clazz) {
288287
/**根据路径取值
289288
* @param path
290289
* @param clazz
291-
* @param tryAll false-仅当前对象,true-本次请求的全局对象以及 Parser<T, M, L> 缓存值
290+
* @param tryAll false-仅当前对象,true-本次请求的全局对象以及 Parser<T, JSONRequest, L> 缓存值
292291
* @return
293292
* @param <T>
294293
*/
@@ -420,7 +419,7 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> Object
420419
}
421420

422421
if (lang != null && SCRIPT_EXECUTOR_MAP.get(lang) == null) {
423-
throw new ClassNotFoundException("找不到脚本语言 " + lang + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser<T, M, L> 中注册!");
422+
throw new ClassNotFoundException("找不到脚本语言 " + lang + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser<T, JSONRequest, L> 中注册!");
424423
}
425424

426425
int version = row.get("version") != null ? Integer.parseInt(row.get("version").toString()) : 0;
@@ -904,7 +903,7 @@ public <V> V getArgVal(@NotNull M req, String key, Class<V> clazz) throws Except
904903
* @throws Exception
905904
*/
906905
public <V> V getArgVal(String key, Class<V> clazz, boolean defaultValue) throws Exception {
907-
Object obj = parser != null && JSONRequest.isArrayKey(key) ? AbstractParser.getValue(request, key.split("\\,")) : request.get(key);
906+
Object obj = parser != null && apijson.JSONObject.isArrayKey(key) ? AbstractParser.getValue(request, key.split("\\,")) : request.get(key);
908907

909908
if (clazz == null) {
910909
return (V) obj;

0 commit comments

Comments
 (0)