Skip to content

Commit 06782a4

Browse files
committed
解决误用 apijson.framework.APIJSONCreator,解决反序列化自定义 List<T> 报错 TypeToken 中泛型未明确类型
1 parent 1b65517 commit 06782a4

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

src/main/java/apijson/gson/APIJSONApplication.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
package apijson.gson;
1616

1717
import apijson.NotNull;
18-
import apijson.framework.APIJSONCreator;
1918

19+
import java.lang.reflect.Array;
20+
import java.util.ArrayList;
21+
import java.util.LinkedHashMap;
2022
import java.util.List;
2123
import java.util.Map;
2224
import com.google.gson.Gson;
@@ -35,15 +37,20 @@ public class APIJSONApplication extends apijson.framework.APIJSONApplication {
3537
public static TypeToken<?> JSON_ARRAY_TOKEN;
3638
public static Class<?> JSON_ARRAY_CLASS;
3739
static {
38-
JSON_OBJECT_TOKEN = new TypeToken<Map<String, Object>>(){};
40+
APIJSONParser.IS_RETURN_STACK_TRACE = false;
41+
42+
GSON = new Gson();
43+
44+
JSON_OBJECT_TOKEN = new TypeToken<LinkedHashMap<String, Object>>(){};
3945
JSON_OBJECT_CLASS = JSON_OBJECT_TOKEN.getRawType();
4046

41-
JSON_ARRAY_TOKEN = new TypeToken<List<Object>>(){};
47+
JSON_ARRAY_TOKEN = new TypeToken<ArrayList<Object>>(){};
4248
JSON_ARRAY_CLASS = JSON_ARRAY_TOKEN.getRawType();
4349

44-
GSON = apijson.gson.APIJSONApplication.GSON;
4550
// apijson.JSON.DEFAULT_JSON_PARSER = JSON.DEFAULT_JSON_PARSER; // 解决 DEFAULT_JSON_PARSER 初始化前就自测导致抛异常
4651

52+
DEFAULT_APIJSON_CREATOR = new APIJSONCreator<>();
53+
4754
JSON.DEFAULT_JSON_PARSER = new JSONParser() {
4855
@Override
4956
public Map<String, Object> createJSONObject() {
@@ -94,7 +101,22 @@ public List<Object> parseArray(Object json) {
94101

95102
@Override
96103
public <T> List<T> parseArray(Object json, Class<T> clazz) {
97-
return GSON.fromJson(toJSONString(json), new TypeToken<List<T>>(){}.getType());
104+
List<Object> list = parseArray(json);
105+
if (list == null) {
106+
return null;
107+
}
108+
109+
List<T> list2 = new ArrayList<>();
110+
for (int i = 0; i < list.size(); i++) {
111+
Object obj = list.get(i);
112+
if (obj != null && clazz != null && ! clazz.isAssignableFrom(obj.getClass())) {
113+
String str = toJSONString(obj);
114+
obj = clazz.isAssignableFrom(String.class) ? str : GSON.fromJson(str, clazz);
115+
}
116+
list2.add((T) obj);
117+
}
118+
119+
return list2;
98120
}
99121
};
100122
}
@@ -124,8 +146,7 @@ public static void init(boolean shutdownWhenServerError) throws Exception {
124146
* @return
125147
* @throws Exception
126148
*/
127-
public static <T, M extends Map<String, Object>, L extends List<Object>> void init(
128-
@NotNull APIJSONCreator<T, M, L> creator) throws Exception {
149+
public static <T> void init(@NotNull APIJSONCreator<T> creator) throws Exception {
129150
init(true, creator);
130151
}
131152
/**初始化,加载所有配置并校验
@@ -134,8 +155,7 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> void in
134155
* @return
135156
* @throws Exception
136157
*/
137-
public static <T, M extends Map<String, Object>, L extends List<Object>> void init(
138-
boolean shutdownWhenServerError, @NotNull APIJSONCreator<T, M, L> creator) throws Exception {
158+
public static <T> void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator<T> creator) throws Exception {
139159
apijson.framework.APIJSONApplication.init(shutdownWhenServerError, creator);
140160
}
141161

src/main/java/apijson/gson/APIJSONParser.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ public APIJSONFunctionParser<T> getFunctionParser() {
9999
public APIJSONObjectParser<T> createObjectParser(Map<String, Object> request, String parentPath
100100
, SQLConfig<T, Map<String, Object>, List<Object>> arrayConfig
101101
, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {
102-
return (APIJSONObjectParser<T>) super.createObjectParser(
103-
request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable
104-
);
102+
return new APIJSONObjectParser<T>(
103+
this.getSession(), request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable
104+
) {}.setMethod(this.getMethod()).setParser(this);
105105
}
106106

107107
public static Map<String, Object> parseRequest(String request) {
@@ -114,4 +114,11 @@ public static Map<String, Object> parseRequest(String request) {
114114
}
115115
}
116116

117+
// 没用,提前打日志已经序列化了
118+
//@Override
119+
//public Map<String, Object> parseResponse(Map<String, Object> request) {
120+
// Map<String, Object> response = super.parseResponse(request);
121+
// response.remove("trace:stack"); // FIXME 暂时先这样避免 gson 序列化报错,考虑后续提前转成 JSON Object
122+
// return response;
123+
//}
117124
}

src/main/java/apijson/gson/APIJSONSQLConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ public APIJSONSQLConfig(RequestMethod method, int count, int page) {
6969
// , Map<String, Object>, List<Object>>> joinList, boolean isProcedure) throws Exception {
7070
// return newSQLConfig(method, table, alias, request, joinList, isProcedure, new SimpleCallback<T>() {});
7171
//}
72-
public static <T> SQLConfig<T, Map<String, Object>, List<Object>> newSQLConfig(
72+
public static <T> SQLConfig<T, Map<String, Object>, List<Object>> newSQLConfig2(
7373
RequestMethod method, String table, String alias
74-
, LinkedHashMap<String, Object> request, List<Join<T, Map<String, Object>, List<Object>>> joinList, boolean isProcedure) throws Exception {
74+
, Map<String, Object> request, List<Join<T, Map<String, Object>, List<Object>>> joinList, boolean isProcedure
75+
) throws Exception {
7576
return newSQLConfig(method, table, alias, request, joinList, isProcedure, new SimpleCallback<T>() {});
7677
}
7778

0 commit comments

Comments
 (0)