Visitar URL original
去除 fastjson 并支持各种 JSON 库 by TommyLemon · Pull Request #810 · APIJSON/APIJSON · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
完善和优化 JSON 处理,解决 fastjson 等 JSON 库自动序列化 AbstractSQLConfig.getSetStrin…
…g 等方法抛异常;优化代码
  • Loading branch information
TommyLemon committed Apr 6, 2025
commit 42ca34237475fb5742fe413ecf535dc99ec2489f
440 changes: 270 additions & 170 deletions APIJSONORM/src/main/java/apijson/JSON.java

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions APIJSONORM/src/main/java/apijson/JSONCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,31 @@ public interface JSONCreator<M extends Map<String, Object>, L extends List<Objec
M createJSONObject();

@NotNull
default M createJSONObject(Map<? extends String, ?> m) {
default M createJSONObject(String key, Object value) {
M obj = createJSONObject();
if (m != null && ! m.isEmpty()) {
obj.putAll(m);
obj.put(key, value);
return obj;
}

@NotNull
default M createJSONObject(Map<? extends String, ?> map) {
M obj = createJSONObject();
if (map != null && ! map.isEmpty()) {
obj.putAll(map);
}
return obj;
}

@NotNull
L createJSONArray();

@NotNull
default L createJSONArray(Object obj){
L arr = createJSONArray();
arr.add(obj);
return arr;
}

@NotNull
default L createJSONArray(List<?> l){
L arr = createJSONArray();
Expand Down
19 changes: 7 additions & 12 deletions APIJSONORM/src/main/java/apijson/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@
package apijson;

import java.util.*;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;

import apijson.orm.exception.UnsupportedDataTypeException;

/**use this class instead of com.alibaba.fastjson.JSONObject
* @author Lemon
* @see #put
* @see #puts
* @see #putsAll
*/
public class JSONObject extends JSON implements Map<String, Object> {
public class JSONObject extends JSON implements Map<String, Object> {
private static final String TAG = "JSONObject";

private final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
Expand Down Expand Up @@ -699,7 +694,7 @@ public JSONObject putsAll(Map<? extends String, ? extends Object> map) {
public boolean getBooleanValue(String key) {
try {
return JSON.getBooleanValue(this, key);
} catch (UnsupportedDataTypeException e) {
} catch (IllegalArgumentException e) {
return false;
}
}
Expand All @@ -712,7 +707,7 @@ public boolean getBooleanValue(String key) {
public int getIntValue(String key) {
try {
return JSON.getIntValue(this, key);
} catch (UnsupportedDataTypeException e) {
} catch (IllegalArgumentException e) {
return 0;
}
}
Expand All @@ -725,7 +720,7 @@ public int getIntValue(String key) {
public long getLongValue(String key) {
try {
return JSON.getLongValue(this, key);
} catch (UnsupportedDataTypeException e) {
} catch (IllegalArgumentException e) {
return 0L;
}
}
Expand All @@ -738,7 +733,7 @@ public long getLongValue(String key) {
public double getDoubleValue(String key) {
try {
return JSON.getDoubleValue(this, key);
} catch (UnsupportedDataTypeException e) {
} catch (IllegalArgumentException e) {
return 0.0;
}
}
Expand All @@ -762,7 +757,7 @@ public JSONObject getJSONObject(String key) {
try {
Map<String, Object> map = JSON.getMap(this, key);
return map != null ? new JSONObject(map) : null;
} catch (UnsupportedDataTypeException e) {
} catch (IllegalArgumentException e) {
return null;
}
}
Expand All @@ -776,7 +771,7 @@ public JSONArray getJSONArray(String key) {
try {
List<Object> list = JSON.getList(this, key);
return list != null ? new JSONArray(list) : null;
} catch (UnsupportedDataTypeException e) {
} catch (IllegalArgumentException e) {
return null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion APIJSONORM/src/main/java/apijson/JSONRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Lemon
* @see #puts
* @see #toArray
* @use JSONRequest request = new JSONRequest(...);
* @use JSONRequest request = JSON.createJSONObject(...);
* <br> request.puts(...);//not a must
* <br> request.toArray(...);//not a must
*/
Expand Down
56 changes: 42 additions & 14 deletions APIJSONORM/src/main/java/apijson/JSONResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package apijson;

import apijson.orm.exception.UnsupportedDataTypeException;

import java.util.*;

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

// 节约性能和减少 bug,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
Expand Down Expand Up @@ -113,11 +111,11 @@ public String getMsg() {
return getString(KEY_MSG);
}
/**获取状态描述
* @param reponse
* @param response
* @return
*/
public static String getMsg(Map<String, Object> reponse) {
return reponse == null ? null : JSON.getString(reponse, KEY_MSG);
public static String getMsg(Map<String, Object> response) {
return response == null ? null : JSON.getString(response, KEY_MSG);
}
/**获取id
* @return
Expand Down Expand Up @@ -171,7 +169,7 @@ public static boolean isSuccess(int code) {
* @param response
* @return
*/
public static boolean isSuccess(JSONResponse response) {
public static boolean isSuccess(JSONResponse<?, ?> response) {
return response != null && response.isSuccess();
}
/**是否成功
Expand All @@ -181,7 +179,7 @@ public static boolean isSuccess(JSONResponse response) {
public static boolean isSuccess(Map<String, Object> response) {
try {
return response != null && isSuccess(JSON.getIntValue(response, KEY_CODE));
} catch (UnsupportedDataTypeException e) {
} catch (IllegalArgumentException e) {
return false;
}
}
Expand All @@ -203,10 +201,17 @@ public static boolean isExist(int count) {
* @param response
* @return
*/
public static boolean isExist(JSONResponse response) {
public static boolean isExist(JSONResponse<?, ?> response) {
return response != null && response.isExist();
}

/**获取内部的JSONResponse
* @param key
* @return
*/
public JSONResponse<M, L> getJSONResponse(String key) {
return getObject(key, JSONResponse.class, null);
}
/**获取内部的JSONResponse
* @param key
* @return
Expand All @@ -220,12 +225,20 @@ public JSONResponse<M, L> getJSONResponse(String key, JSONParser<M, L> parser) {
// * @param key
// * @return
// */
// public static JSONResponse getJSONResponse(M response, String key) {
// public static JSONResponse getJSONResponse(JSONRequest response, String key) {
// return response == null ? null : response.getObject(key, JSONResponse.class);
// }
//状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


/**
* key = clazz.getSimpleName()
* @param clazz
* @return
*/
public <T> T getObject(Class<T> clazz) {
return getObject(clazz == null ? "" : clazz.getSimpleName(), clazz, (JSONParser<M, L>) DEFAULT_JSON_PARSER);
}
/**
* key = clazz.getSimpleName()
* @param clazz
Expand All @@ -234,6 +247,14 @@ public JSONResponse<M, L> getJSONResponse(String key, JSONParser<M, L> parser) {
public <T> T getObject(Class<T> clazz, JSONParser<M, L> parser) {
return getObject(clazz == null ? "" : clazz.getSimpleName(), clazz, parser);
}
/**
* @param key
* @param clazz
* @return
*/
public <T> T getObject(String key, Class<T> clazz) {
return getObject(this, key, clazz, (JSONParser<M, L>) DEFAULT_JSON_PARSER);
}
/**
* @param key
* @param clazz
Expand All @@ -253,6 +274,13 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> T getOb
return toObject(object == null ? null : JSON.get(object, formatObjectKey(key)), clazz, parser);
}

/**
* @param clazz
* @return
*/
public <T> T toObject(Class<T> clazz) {
return toObject(clazz, null);
}
/**
* @param clazz
* @return
Expand All @@ -267,7 +295,7 @@ public <T> T toObject(Class<T> clazz, JSONParser<M, L> parser) {
*/
public static <T, M extends Map<String, Object>, L extends List<Object>> T toObject(
Map<String, Object> object, Class<T> clazz, JSONParser<M, L> parser) {
return parseObject(JSON.toJSONString(object), clazz, parser);
return parseObject(object, clazz, parser);
}


Expand Down Expand Up @@ -345,7 +373,7 @@ public static JSONArray getArray(Map<String, Object> object, String key) {
// /**
// * @return
// */
// public M format() {
// public JSONRequest format() {
// return format(this);
// }
/**格式化key名称
Expand Down Expand Up @@ -390,7 +418,7 @@ public static <M extends Map<String, Object>, L extends List<Object>> M format(f
if (value instanceof List<?>) {//JSONArray,遍历来format内部项
formatedObject.put(formatArrayKey(key), format((L) value, creator));
}
else if (value instanceof Map<?, ?>) {//M,往下一级提取
else if (value instanceof Map<?, ?>) {//JSONRequest,往下一级提取
formatedObject.put(formatObjectKey(key), format((M) value, creator));
}
else {//其它Object,直接填充
Expand Down Expand Up @@ -436,7 +464,7 @@ public static <M extends Map<String, Object>, L extends List<Object>> L format(f
if (value instanceof List<?>) {//JSONArray,遍历来format内部项
formatedArray.add(format((L) value, creator));
}
else if (value instanceof Map<?, ?>) {//M,往下一级提取
else if (value instanceof Map<?, ?>) {//JSONRequest,往下一级提取
formatedArray.add(format((M) value, creator));
}
else {//其它Object,直接填充
Expand Down
2 changes: 1 addition & 1 deletion APIJSONORM/src/main/java/apijson/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Log {

public static boolean DEBUG = true;

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

public static final String OS_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.*;

import static apijson.orm.AbstractSQLConfig.PATTERN_SCHEMA;
import static apijson.orm.SQLConfig.TYPE_ITEM;

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

// <methodName, JSONObject>
// <isContain, <arguments:"array,key", tag:null, methods:null>>
public static Map<String, ScriptExecutor> SCRIPT_EXECUTOR_MAP;
public static Map<String, ScriptExecutor<?, ? extends Map<String, Object>, ? extends List<Object>>> SCRIPT_EXECUTOR_MAP;
public static Map<String, Map<String, Object>> FUNCTION_MAP;

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

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

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

if (clazz == null) {
return (V) obj;
Expand Down
Loading