Skip to content

Commit 6526493

Browse files
committed
新增 JSON.format
1 parent 1662e21 commit 6526493

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public JSONArray createJSONArray() {
3232
}
3333

3434
@Override
35-
public String toJSONString(Object obj) {
36-
return JSON.toJSONString(obj);
35+
public String toJSONString(Object obj, boolean format) {
36+
throw new UnsupportedOperationException();
3737
}
3838

3939
@Override
@@ -178,11 +178,21 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> List<T>
178178
return null;
179179
}
180180

181+
/**
182+
* @param obj
183+
* @return
184+
*/
185+
public static String format(Object obj) {
186+
return toJSONString(obj, true);
187+
}
181188
/**
182189
* @param obj
183190
* @return
184191
*/
185192
public static String toJSONString(Object obj) {
193+
return toJSONString(obj, false);
194+
}
195+
public static String toJSONString(Object obj, boolean format) {
186196
if (obj == null) {
187197
return null;
188198
}
@@ -235,7 +245,7 @@ public static String toJSONString(Object obj) {
235245
// return sb.toString();
236246
//}
237247

238-
return DEFAULT_JSON_PARSER.toJSONString(obj);
248+
return DEFAULT_JSON_PARSER.toJSONString(obj, format);
239249
}
240250

241251

APIJSONORM/src/main/java/apijson/JSONParser.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@ public interface JSONParser<M extends Map<String, Object>, L extends List<Object
2323

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

26-
String toJSONString(Object obj);
26+
default String format(Object obj) {
27+
return toJSONString(obj, true);
28+
}
29+
default String toJSONString(Object obj) {
30+
return toJSONString(obj, false);
31+
}
32+
String toJSONString(Object obj, boolean format);
2733
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> Object
495495
return invokeScript(parser, methodName, parameterTypes, args, returnType, current, scriptExecutor);
496496
}
497497

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

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

661663
values = new Object[length + 1];

0 commit comments

Comments
 (0)