5
5
6
6
package apijson ;
7
7
8
- import apijson .orm .exception .UnsupportedDataTypeException ;
9
-
10
8
import java .util .*;
11
9
12
10
import static apijson .JSON .parseObject ;
19
17
* <br> User user = response.getObject(User.class);//not a must
20
18
* <br> List<Comment> commenntList = response.getList("Comment[]", Comment.class);//not a must
21
19
*/
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 > {
23
21
private static final long serialVersionUID = 1L ;
24
22
25
23
// 节约性能和减少 bug,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
@@ -113,11 +111,11 @@ public String getMsg() {
113
111
return getString (KEY_MSG );
114
112
}
115
113
/**获取状态描述
116
- * @param reponse
114
+ * @param response
117
115
* @return
118
116
*/
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 );
121
119
}
122
120
/**获取id
123
121
* @return
@@ -171,7 +169,7 @@ public static boolean isSuccess(int code) {
171
169
* @param response
172
170
* @return
173
171
*/
174
- public static boolean isSuccess (JSONResponse response ) {
172
+ public static boolean isSuccess (JSONResponse <?, ?> response ) {
175
173
return response != null && response .isSuccess ();
176
174
}
177
175
/**是否成功
@@ -181,7 +179,7 @@ public static boolean isSuccess(JSONResponse response) {
181
179
public static boolean isSuccess (Map <String , Object > response ) {
182
180
try {
183
181
return response != null && isSuccess (JSON .getIntValue (response , KEY_CODE ));
184
- } catch (UnsupportedDataTypeException e ) {
182
+ } catch (IllegalArgumentException e ) {
185
183
return false ;
186
184
}
187
185
}
@@ -203,10 +201,17 @@ public static boolean isExist(int count) {
203
201
* @param response
204
202
* @return
205
203
*/
206
- public static boolean isExist (JSONResponse response ) {
204
+ public static boolean isExist (JSONResponse <?, ?> response ) {
207
205
return response != null && response .isExist ();
208
206
}
209
207
208
+ /**获取内部的JSONResponse
209
+ * @param key
210
+ * @return
211
+ */
212
+ public JSONResponse <M , L > getJSONResponse (String key ) {
213
+ return getObject (key , JSONResponse .class , null );
214
+ }
210
215
/**获取内部的JSONResponse
211
216
* @param key
212
217
* @return
@@ -220,12 +225,20 @@ public JSONResponse<M, L> getJSONResponse(String key, JSONParser<M, L> parser) {
220
225
// * @param key
221
226
// * @return
222
227
// */
223
- // public static JSONResponse getJSONResponse(M response, String key) {
228
+ // public static JSONResponse getJSONResponse(JSONRequest response, String key) {
224
229
// return response == null ? null : response.getObject(key, JSONResponse.class);
225
230
// }
226
231
//状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
227
232
228
233
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
+ }
229
242
/**
230
243
* key = clazz.getSimpleName()
231
244
* @param clazz
@@ -234,6 +247,14 @@ public JSONResponse<M, L> getJSONResponse(String key, JSONParser<M, L> parser) {
234
247
public <T > T getObject (Class <T > clazz , JSONParser <M , L > parser ) {
235
248
return getObject (clazz == null ? "" : clazz .getSimpleName (), clazz , parser );
236
249
}
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
+ }
237
258
/**
238
259
* @param key
239
260
* @param clazz
@@ -253,6 +274,13 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> T getOb
253
274
return toObject (object == null ? null : JSON .get (object , formatObjectKey (key )), clazz , parser );
254
275
}
255
276
277
+ /**
278
+ * @param clazz
279
+ * @return
280
+ */
281
+ public <T > T toObject (Class <T > clazz ) {
282
+ return toObject (clazz , null );
283
+ }
256
284
/**
257
285
* @param clazz
258
286
* @return
@@ -267,7 +295,7 @@ public <T> T toObject(Class<T> clazz, JSONParser<M, L> parser) {
267
295
*/
268
296
public static <T , M extends Map <String , Object >, L extends List <Object >> T toObject (
269
297
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 );
271
299
}
272
300
273
301
@@ -345,7 +373,7 @@ public static JSONArray getArray(Map<String, Object> object, String key) {
345
373
// /**
346
374
// * @return
347
375
// */
348
- // public M format() {
376
+ // public JSONRequest format() {
349
377
// return format(this);
350
378
// }
351
379
/**格式化key名称
@@ -390,7 +418,7 @@ public static <M extends Map<String, Object>, L extends List<Object>> M format(f
390
418
if (value instanceof List <?>) {//JSONArray,遍历来format内部项
391
419
formatedObject .put (formatArrayKey (key ), format ((L ) value , creator ));
392
420
}
393
- else if (value instanceof Map <?, ?>) {//M ,往下一级提取
421
+ else if (value instanceof Map <?, ?>) {//JSONRequest ,往下一级提取
394
422
formatedObject .put (formatObjectKey (key ), format ((M ) value , creator ));
395
423
}
396
424
else {//其它Object,直接填充
@@ -436,7 +464,7 @@ public static <M extends Map<String, Object>, L extends List<Object>> L format(f
436
464
if (value instanceof List <?>) {//JSONArray,遍历来format内部项
437
465
formatedArray .add (format ((L ) value , creator ));
438
466
}
439
- else if (value instanceof Map <?, ?>) {//M ,往下一级提取
467
+ else if (value instanceof Map <?, ?>) {//JSONRequest ,往下一级提取
440
468
formatedArray .add (format ((M ) value , creator ));
441
469
}
442
470
else {//其它Object,直接填充
0 commit comments