55
66package apijson ;
77
8- import apijson .orm .exception .UnsupportedDataTypeException ;
9-
108import java .util .*;
119
1210import static apijson .JSON .parseObject ;
1917 * <br> User user = response.getObject(User.class);//not a must
2018 * <br> List<Comment> commenntList = response.getList("Comment[]", Comment.class);//not a must
2119 */
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 > {
2321 private static final long serialVersionUID = 1L ;
2422
2523 // 节约性能和减少 bug,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
@@ -113,11 +111,11 @@ public String getMsg() {
113111 return getString (KEY_MSG );
114112 }
115113 /**获取状态描述
116- * @param reponse
114+ * @param response
117115 * @return
118116 */
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 );
121119 }
122120 /**获取id
123121 * @return
@@ -171,7 +169,7 @@ public static boolean isSuccess(int code) {
171169 * @param response
172170 * @return
173171 */
174- public static boolean isSuccess (JSONResponse response ) {
172+ public static boolean isSuccess (JSONResponse <?, ?> response ) {
175173 return response != null && response .isSuccess ();
176174 }
177175 /**是否成功
@@ -181,7 +179,7 @@ public static boolean isSuccess(JSONResponse response) {
181179 public static boolean isSuccess (Map <String , Object > response ) {
182180 try {
183181 return response != null && isSuccess (JSON .getIntValue (response , KEY_CODE ));
184- } catch (UnsupportedDataTypeException e ) {
182+ } catch (IllegalArgumentException e ) {
185183 return false ;
186184 }
187185 }
@@ -203,10 +201,17 @@ public static boolean isExist(int count) {
203201 * @param response
204202 * @return
205203 */
206- public static boolean isExist (JSONResponse response ) {
204+ public static boolean isExist (JSONResponse <?, ?> response ) {
207205 return response != null && response .isExist ();
208206 }
209207
208+ /**获取内部的JSONResponse
209+ * @param key
210+ * @return
211+ */
212+ public JSONResponse <M , L > getJSONResponse (String key ) {
213+ return getObject (key , JSONResponse .class , null );
214+ }
210215 /**获取内部的JSONResponse
211216 * @param key
212217 * @return
@@ -220,12 +225,20 @@ public JSONResponse<M, L> getJSONResponse(String key, JSONParser<M, L> parser) {
220225 // * @param key
221226 // * @return
222227 // */
223- // public static JSONResponse getJSONResponse(M response, String key) {
228+ // public static JSONResponse getJSONResponse(JSONRequest response, String key) {
224229 // return response == null ? null : response.getObject(key, JSONResponse.class);
225230 // }
226231 //状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
227232
228233
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+ }
229242 /**
230243 * key = clazz.getSimpleName()
231244 * @param clazz
@@ -234,6 +247,14 @@ public JSONResponse<M, L> getJSONResponse(String key, JSONParser<M, L> parser) {
234247 public <T > T getObject (Class <T > clazz , JSONParser <M , L > parser ) {
235248 return getObject (clazz == null ? "" : clazz .getSimpleName (), clazz , parser );
236249 }
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+ }
237258 /**
238259 * @param key
239260 * @param clazz
@@ -253,6 +274,13 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> T getOb
253274 return toObject (object == null ? null : JSON .get (object , formatObjectKey (key )), clazz , parser );
254275 }
255276
277+ /**
278+ * @param clazz
279+ * @return
280+ */
281+ public <T > T toObject (Class <T > clazz ) {
282+ return toObject (clazz , null );
283+ }
256284 /**
257285 * @param clazz
258286 * @return
@@ -267,7 +295,7 @@ public <T> T toObject(Class<T> clazz, JSONParser<M, L> parser) {
267295 */
268296 public static <T , M extends Map <String , Object >, L extends List <Object >> T toObject (
269297 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 );
271299 }
272300
273301
@@ -345,7 +373,7 @@ public static JSONArray getArray(Map<String, Object> object, String key) {
345373 // /**
346374 // * @return
347375 // */
348- // public M format() {
376+ // public JSONRequest format() {
349377 // return format(this);
350378 // }
351379 /**格式化key名称
@@ -390,7 +418,7 @@ public static <M extends Map<String, Object>, L extends List<Object>> M format(f
390418 if (value instanceof List <?>) {//JSONArray,遍历来format内部项
391419 formatedObject .put (formatArrayKey (key ), format ((L ) value , creator ));
392420 }
393- else if (value instanceof Map <?, ?>) {//M ,往下一级提取
421+ else if (value instanceof Map <?, ?>) {//JSONRequest ,往下一级提取
394422 formatedObject .put (formatObjectKey (key ), format ((M ) value , creator ));
395423 }
396424 else {//其它Object,直接填充
@@ -436,7 +464,7 @@ public static <M extends Map<String, Object>, L extends List<Object>> L format(f
436464 if (value instanceof List <?>) {//JSONArray,遍历来format内部项
437465 formatedArray .add (format ((L ) value , creator ));
438466 }
439- else if (value instanceof Map <?, ?>) {//M ,往下一级提取
467+ else if (value instanceof Map <?, ?>) {//JSONRequest ,往下一级提取
440468 formatedArray .add (format ((M ) value , creator ));
441469 }
442470 else {//其它Object,直接填充
0 commit comments