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
新增 JSON.format
  • Loading branch information
TommyLemon committed Apr 12, 2025
commit 6526493c7e817d1568cb00ee6fba2a75dae62403
16 changes: 13 additions & 3 deletions APIJSONORM/src/main/java/apijson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public JSONArray createJSONArray() {
}

@Override
public String toJSONString(Object obj) {
return JSON.toJSONString(obj);
public String toJSONString(Object obj, boolean format) {
throw new UnsupportedOperationException();
}

@Override
Expand Down Expand Up @@ -178,11 +178,21 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> List<T>
return null;
}

/**
* @param obj
* @return
*/
public static String format(Object obj) {
return toJSONString(obj, true);
}
/**
* @param obj
* @return
*/
public static String toJSONString(Object obj) {
return toJSONString(obj, false);
}
public static String toJSONString(Object obj, boolean format) {
if (obj == null) {
return null;
}
Expand Down Expand Up @@ -235,7 +245,7 @@ public static String toJSONString(Object obj) {
// return sb.toString();
//}

return DEFAULT_JSON_PARSER.toJSONString(obj);
return DEFAULT_JSON_PARSER.toJSONString(obj, format);
}


Expand Down
8 changes: 7 additions & 1 deletion APIJSONORM/src/main/java/apijson/JSONParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ public interface JSONParser<M extends Map<String, Object>, L extends List<Object

<T> List<T> parseArray(Object json, Class<T> clazz);

String toJSONString(Object obj);
default String format(Object obj) {
return toJSONString(obj, true);
}
default String toJSONString(Object obj) {
return toJSONString(obj, false);
}
String toJSONString(Object obj, boolean format);
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> Object
return invokeScript(parser, methodName, parameterTypes, args, returnType, current, scriptExecutor);
}

Method m = parser.getClass().getMethod(methodName, parameterTypes); // 不用判空,拿不到就会抛异常
Class<? extends AbstractFunctionParser> cls = parser.getClass();
Method m = cls.getMethod(methodName, parameterTypes); // 不用判空,拿不到就会抛异常

if (Log.DEBUG) {
String rt = Log.DEBUG && m.getReturnType() != null ? m.getReturnType().getSimpleName() : null;
Expand Down Expand Up @@ -656,6 +657,7 @@ else if (v instanceof Collection) { // 泛型兼容? // JSONArray
}
else {
types = new Class<?>[length + 1];
//types[0] = Object.class; // 泛型擦除 JSON.JSON_OBJECT_CLASS;
types[0] = JSON.JSON_OBJECT_CLASS;

values = new Object[length + 1];
Expand Down