forked from Tencent/APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONResponse.java
More file actions
executable file
·516 lines (460 loc) · 13.9 KB
/
JSONResponse.java
File metadata and controls
executable file
·516 lines (460 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**parser for response
* @author Lemon
* @see #getObject
* @see #getList
* @use JSONResponse response = new JSONResponse(json);
* <br> User user = response.getObject(User.class);//not a must
* <br> List<Comment> commenntList = response.getList("Comment[]", Comment.class);//not a must
*/
public class JSONResponse extends apijson.JSONObject {
private static final long serialVersionUID = 1L;
private static final String TAG = "JSONResponse";
public JSONResponse() {
super();
}
public JSONResponse(String json) {
this(parseObject(json));
}
public JSONResponse(JSONObject object) {
super(format(object));
}
//状态信息,非GET请求获得的信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public static final int CODE_SUCCESS = 200; //成功
public static final int CODE_UNSUPPORTED_ENCODING = 400; //编码错误
public static final int CODE_ILLEGAL_ACCESS = 401; //权限错误
public static final int CODE_UNSUPPORTED_OPERATION = 403; //禁止操作
public static final int CODE_NOT_FOUND = 404; //未找到
public static final int CODE_ILLEGAL_ARGUMENT = 406; //参数错误
public static final int CODE_NOT_LOGGED_IN = 407; //未登录
public static final int CODE_TIME_OUT = 408; //超时
public static final int CODE_CONFLICT = 409; //重复,已存在
public static final int CODE_CONDITION_ERROR = 412; //条件错误,如密码错误
public static final int CODE_UNSUPPORTED_TYPE = 415; //类型错误
public static final int CODE_OUT_OF_RANGE = 416; //超出范围
public static final int CODE_NULL_POINTER = 417; //对象为空
public static final int CODE_SERVER_ERROR = 500; //服务器内部错误
public static final String MSG_SUCCEED = "success"; //成功
public static final String MSG_SERVER_ERROR = "Internal Server Error!"; //服务器内部错误
public static String KEY_OK = "ok";
public static String KEY_CODE = "code";
public static String KEY_MSG = "msg";
public static final String KEY_COUNT = "count";
public static final String KEY_TOTAL = "total";
public static final String KEY_INFO = "info"; //详细的分页信息
public static final String KEY_FIRST = "first"; //是否为首页
public static final String KEY_LAST = "last"; //是否为尾页
public static final String KEY_MAX = "max"; //最大页码
public static final String KEY_MORE = "more"; //是否有更多
/**获取状态
* @return
*/
public int getCode() {
try {
return getIntValue(KEY_CODE);
} catch (Exception e) {
//empty
}
return 0;
}
/**获取状态
* @return
*/
public static int getCode(JSONObject reponse) {
try {
return reponse.getIntValue(KEY_CODE);
} catch (Exception e) {
//empty
}
return 0;
}
/**获取状态描述
* @return
*/
public String getMsg() {
return getString(KEY_MSG);
}
/**获取状态描述
* @param reponse
* @return
*/
public static String getMsg(JSONObject reponse) {
return reponse == null ? null : reponse.getString(KEY_MSG);
}
/**获取id
* @return
*/
public long getId() {
try {
return getLongValue(KEY_ID);
} catch (Exception e) {
//empty
}
return 0;
}
/**获取数量
* @return
*/
public int getCount() {
try {
return getIntValue(KEY_COUNT);
} catch (Exception e) {
//empty
}
return 0;
}
/**获取总数
* @return
*/
public int getTotal() {
try {
return getIntValue(KEY_TOTAL);
} catch (Exception e) {
//empty
}
return 0;
}
/**是否成功
* @return
*/
public boolean isSuccess() {
return isSuccess(getCode());
}
/**是否成功
* @param code
* @return
*/
public static boolean isSuccess(int code) {
return code == CODE_SUCCESS;
}
/**是否成功
* @param response
* @return
*/
public static boolean isSuccess(JSONResponse response) {
return response != null && response.isSuccess();
}
/**是否成功
* @param response
* @return
*/
public static boolean isSuccess(JSONObject response) {
return response != null && isSuccess(response.getIntValue(KEY_CODE));
}
/**校验服务端是否存在table
* @return
*/
public boolean isExist() {
return isExist(getCount());
}
/**校验服务端是否存在table
* @param count
* @return
*/
public static boolean isExist(int count) {
return count > 0;
}
/**校验服务端是否存在table
* @param response
* @return
*/
public static boolean isExist(JSONResponse response) {
return response != null && response.isExist();
}
/**获取内部的JSONResponse
* @param key
* @return
*/
public JSONResponse getJSONResponse(String key) {
return getObject(key, JSONResponse.class);
}
//cannot get javaBeanDeserizer
// /**获取内部的JSONResponse
// * @param response
// * @param key
// * @return
// */
// public static JSONResponse getJSONResponse(JSONObject response, String key) {
// return response == null ? null : response.getObject(key, JSONResponse.class);
// }
//状态信息,非GET请求获得的信息>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/**
* key = clazz.getSimpleName()
* @param clazz
* @return
*/
public <T> T getObject(Class<T> clazz) {
return getObject(clazz == null ? "" : clazz.getSimpleName(), clazz);
}
/**
* @param key
* @param clazz
* @return
*/
public <T> T getObject(String key, Class<T> clazz) {
return getObject(this, key, clazz);
}
/**
* @param object
* @param key
* @param clazz
* @return
*/
public static <T> T getObject(JSONObject object, String key, Class<T> clazz) {
return toObject(object == null ? null : object.getJSONObject(formatObjectKey(key)), clazz);
}
/**
* @param clazz
* @return
*/
public <T> T toObject(Class<T> clazz) {
return toObject(this, clazz);
}
/**
* @param object
* @param clazz
* @return
*/
public static <T> T toObject(JSONObject object, Class<T> clazz) {
return JSON.parseObject(JSON.toJSONString(object), clazz);
}
/**
* key = KEY_ARRAY
* @param clazz
* @return
*/
public <T> List<T> getList(Class<T> clazz) {
return getList(KEY_ARRAY, clazz);
}
/**
* arrayObject = this
* @param key
* @param clazz
* @return
*/
public <T> List<T> getList(String key, Class<T> clazz) {
return getList(this, key, clazz);
}
/**
* key = KEY_ARRAY
* @param object
* @param clazz
* @return
*/
public static <T> List<T> getList(JSONObject object, Class<T> clazz) {
return getList(object, KEY_ARRAY, clazz);
}
/**
* @param object
* @param key
* @param clazz
* @return
*/
public static <T> List<T> getList(JSONObject object, String key, Class<T> clazz) {
return object == null ? null : JSON.parseArray(object.getString(formatArrayKey(key)), clazz);
}
/**
* key = KEY_ARRAY
* @return
*/
public JSONArray getArray() {
return getArray(KEY_ARRAY);
}
/**
* @param key
* @return
*/
public JSONArray getArray(String key) {
return getArray(this, key);
}
/**
* @param object
* @return
*/
public static JSONArray getArray(JSONObject object) {
return getArray(object, KEY_ARRAY);
}
/**
* key = KEY_ARRAY
* @param object
* @param key
* @return
*/
public static JSONArray getArray(JSONObject object, String key) {
return object == null ? null : object.getJSONArray(formatArrayKey(key));
}
// /**
// * @return
// */
// public JSONObject format() {
// return format(this);
// }
/**格式化key名称
* @param object
* @return
*/
public static JSONObject format(final JSONObject object) {
//太长查看不方便,不如debug Log.i(TAG, "format object = \n" + JSON.toJSONString(object));
if (object == null || object.isEmpty()) {
Log.i(TAG, "format object == null || object.isEmpty() >> return object;");
return object;
}
JSONObject formatedObject = new JSONObject(true);
Set<String> set = object.keySet();
if (set != null) {
Object value;
for (String key : set) {
value = object.get(key);
if (value instanceof JSONArray) {//JSONArray,遍历来format内部项
formatedObject.put(formatArrayKey(key), format((JSONArray) value));
}
else if (value instanceof JSONObject) {//JSONObject,往下一级提取
formatedObject.put(formatObjectKey(key), format((JSONObject) value));
}
else {//其它Object,直接填充
formatedObject.put(formatOtherKey(key), value);
}
}
}
//太长查看不方便,不如debug Log.i(TAG, "format return formatedObject = " + JSON.toJSONString(formatedObject));
return formatedObject;
}
/**格式化key名称
* @param array
* @return
*/
public static JSONArray format(final JSONArray array) {
//太长查看不方便,不如debug Log.i(TAG, "format array = \n" + JSON.toJSONString(array));
if (array == null || array.isEmpty()) {
Log.i(TAG, "format array == null || array.isEmpty() >> return array;");
return array;
}
JSONArray formatedArray = new JSONArray();
Object value;
for (int i = 0; i < array.size(); i++) {
value = array.get(i);
if (value instanceof JSONArray) {//JSONArray,遍历来format内部项
formatedArray.add(format((JSONArray) value));
}
else if (value instanceof JSONObject) {//JSONObject,往下一级提取
formatedArray.add(format((JSONObject) value));
}
else {//其它Object,直接填充
formatedArray.add(value);
}
}
//太长查看不方便,不如debug Log.i(TAG, "format return formatedArray = " + JSON.toJSONString(formatedArray));
return formatedArray;
}
/**获取表名称
* @param fullName name 或 name:alias
* @return name => name; name:alias => alias
*/
public static String getTableName(String fullName) {
//key:alias -> alias; key:alias[] -> alias[]
int index = fullName == null ? -1 : fullName.indexOf(":");
return index < 0 ? fullName : fullName.substring(0, index);
}
/**获取变量名
* @param fullName
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = true, formatAt = true, formatHyphen = true, firstCase = true
*/
public static String getVariableName(String fullName) {
if (isArrayKey(fullName)) {
fullName = StringUtil.addSuffix(fullName.substring(0, fullName.length() - 2), "list");
}
return formatKey(fullName, true, true, true, true);
}
/**格式化数组的名称 key[] => keyList; key:alias[] => aliasList; Table-column[] => tableColumnList
* @param key empty ? "list" : key + "List" 且首字母小写
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = true, firstCase = true
*/
public static String formatArrayKey(String key) {
if (isArrayKey(key)) {
key = StringUtil.addSuffix(key.substring(0, key.length() - 2), "list");
}
int index = key == null ? -1 : key.indexOf(":");
if (index >= 0) {
return key.substring(index + 1); //不处理自定义的
}
return formatKey(key, false, true, true, true); //节约性能,除了数组对象 Table-column:alias[] ,一般都符合变量命名规范
}
/**格式化对象的名称 name => name; name:alias => alias
* @param key name 或 name:alias
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = true
*/
public static String formatObjectKey(String key) {
int index = key == null ? -1 : key.indexOf(":");
if (index >= 0) {
return key.substring(index + 1); //不处理自定义的
}
return formatKey(key, false, true, false, true); //节约性能,除了表对象 Table:alias ,一般都符合变量命名规范
}
/**格式化普通值的名称 name => name; name:alias => alias
* @param fullName name 或 name:alias
* @return {@link #formatKey(String, boolean, boolean, boolean, boolean)} formatColon = false, formatAt = true, formatHyphen = false, firstCase = false
*/
public static String formatOtherKey(String fullName) {
return formatKey(fullName, false, true, false, false); //节约性能,除了关键词 @key ,一般都符合变量命名规范,不符合也原样返回便于调试
}
/**格式化名称
* @param fullName name 或 name:alias
* @param formatAt 去除前缀 @ , @a => a
* @param formatColon 去除分隔符 : , A:b => b
* @param formatHyphen 去除分隔符 - , A-b-cd-Efg => aBCdEfg
* @param firstCase 第一个单词首字母小写,后面的首字母大写, Ab => ab ; A-b-Cd => aBCd
* @return name => name; name:alias => alias
*/
public static String formatKey(String fullName, boolean formatColon, boolean formatAt, boolean formatHyphen, boolean firstCase) {
if (fullName == null) {
Log.w(TAG, "formatKey fullName == null >> return null;");
return null;
}
if (formatColon) {
fullName = formatColon(fullName);
}
if (formatAt) { //关键词只去掉前缀,不格式化单词,例如 @a-b 返回 a-b ,最后不会调用 setter
fullName = formatAt(fullName);
}
if (formatHyphen) {
fullName = formatHyphen(fullName, firstCase);
}
return firstCase ? StringUtil.firstCase(fullName) : fullName; //不格式化普通 key:value (value 不为 [], {}) 的 key
}
/**"@key" => "key"
* @param key
* @return
*/
public static String formatAt(@NotNull String key) {
return key.startsWith("@") ? key.substring(1) : key;
}
/**key:alias => alias
* @param key
* @return
*/
public static String formatColon(@NotNull String key) {
int index = key.indexOf(":");
return index < 0 ? key : key.substring(index + 1);
}
/**A-b-cd-Efg => ABCdEfg
* @param key
* @return
*/
public static String formatHyphen(@NotNull String key, boolean firstCase) {
String name = "";
StringTokenizer parts = new StringTokenizer(key, "-");
name += parts.nextToken();
while(parts.hasMoreTokens()) {
String part = parts.nextToken();
name += firstCase ? StringUtil.firstCase(part, true) : part;
}
return name;
}
}