forked from TommyLemon/APIJSON-Android-RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONResponse.java
More file actions
468 lines (418 loc) · 11.9 KB
/
JSONResponse.java
File metadata and controls
468 lines (418 loc) · 11.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
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package zuo.biao.apijson;
import static zuo.biao.apijson.StringUtil.bigAlphaPattern;
import java.util.List;
import java.util.Set;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**parser for response JSON String
* @author Lemon
* @see #getList
* @see #toArray
* @use JSONResponse response = new JSONResponse(...);
* <br> JSONArray array = JSONResponse.toArray(response.getJSONObject(KEY_ARRAY));//not a must
* <br> User user = JSONResponse.getObject(response, User.class);//not a must
* <br> List<Comment> list = JSONResponse.getList(response.getJSONObject("Comment[]"), Comment.class);//not a must
*/
@SuppressWarnings("serial")
public class JSONResponse extends zuo.biao.apijson.JSONObject {
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 STATUS_SUCCEED = 200;
public static final String KEY_ID = "id";
public static final String KEY_STATUS = "status";
public static final String KEY_COUNT = "count";
public static final String KEY_TOTAL = "total";
public static final String KEY_MESSAGE = "message";
/**获取id
* @return
*/
public long getId() {
return getLongValue(KEY_ID);
}
/**获取状态
* @return
*/
public int getStatus() {
return getIntValue(KEY_STATUS);
}
/**获取数量
* @return
*/
public int getCount() {
return getIntValue(KEY_COUNT);
}
/**获取数量
* @return
*/
public int getTotal() {
try {
return getIntValue(KEY_TOTAL);
} catch (Exception e) {
// TODO: handle exception
}
return 0;
}
/**获取信息
* @return
*/
public String getMessage() {
return getString(KEY_MESSAGE);
}
/**是否成功
* @return
*/
public boolean isSucceed() {
return isSucceed(getStatus());
}
/**是否成功
* @param status
* @return
*/
public static boolean isSucceed(int status) {
return status == STATUS_SUCCEED;
}
/**是否成功
* @param response
* @return
*/
public static boolean isSucceed(JSONResponse response) {
return response != null && response.isSucceed();
}
/**校验服务端是否存在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);
}
//状态信息,非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(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) {
Object obj = object == null ? null : object.get(replaceArray(key));
if (obj == null) {
return null;
}
return obj instanceof JSONArray ? JSON.parseArray((JSONArray) obj, clazz) : toList((JSONObject) obj, clazz);
}
/**
* @param clazz
* @return
*/
public <T> List<T> toList(Class<T> clazz) {
return toList(this, clazz);
}
/**
* @param arrayObject
* @param clazz
* @return
*/
public static <T> List<T> toList(JSONObject arrayObject, Class<T> clazz) {
return clazz == null ? null : JSON.parseArray(JSON.toJSONString(
toArray(arrayObject, clazz.getSimpleName())), clazz);
}
/**
* key = KEY_ARRAY
* @param className
* @return
*/
public JSONArray getArray(String className) {
return getArray(KEY_ARRAY, className);
}
/**
* @param key
* @param className
* @return
*/
public JSONArray getArray(String key, String className) {
return getArray(this, key, className);
}
/**
* @param object
* @param key
* @param className
* @return
*/
public static JSONArray getArray(JSONObject object, String className) {
return getArray(object, KEY_ARRAY, className);
}
/**
* key = KEY_ARRAY
* @param object
* @param className
* @return
*/
public static JSONArray getArray(JSONObject object, String key, String className) {
Object obj = object == null ? null : object.get(replaceArray(key));
if (obj == null) {
return null;
}
return obj instanceof JSONArray ? (JSONArray) obj : toArray((JSONObject) obj, className);
}
/**
* @param className
* @return
*/
public JSONArray toArray(String className) {
return toArray(this, className);
}
/**
* array.set(index, isContainer ? value : value.getJSONObject(className));
* @param arrayObject
* @param className className.isEmpty() == false && value.containsKey(className) >> isContainer = true;
* @return
*/
public static JSONArray toArray(JSONObject arrayObject, String className) {
Set<String> set = arrayObject == null ? null : arrayObject.keySet();
if (set == null || set.isEmpty()) {
return null;
}
// [{...},{...},...]
String parentString = StringUtil.getTrimedString(JSON.toJSONString(arrayObject));
if (parentString.isEmpty()) {
return null;
}
if (parentString.startsWith("[")) {
if (parentString.endsWith("]") == false) {
parentString += "]";
}
return JSON.parseArray(parentString);
}
//{"0":{Table:{...}}, "1":{Table:{...}}...}
className = StringUtil.getTrimedString(className);
boolean isContainer = true;
JSONArray array = new JSONArray(set.size());
JSONObject value;
boolean isFirst = true;
int index;
for (String key : set) {//0, 1, 2,...
value = StringUtil.isNumer(key) == false ? null : arrayObject.getJSONObject(key);// Table:{}
if (value != null) {
try {
index = Integer.valueOf(0 + key);
if (isFirst && className.isEmpty() == false && value.containsKey(className)) {// 判断是否需要提取table
isContainer = false;
}
array.set(index, isContainer ? value : value.getJSONObject(className));
} catch (Exception e) {
e.printStackTrace();
}
}
isFirst = false;
}
return array;
}
// /**将Table[]:{0:{Table:{}}, 1:{Table:{}}...} 转化为 tableList:[{}, {}]
// * @return
// */
// public JSONObject format() {
// return format(this);
// }
/**将Table[]:{0:{Table:{}}, 1:{Table:{}}...} 转化为 tableList:[{}, {}]
* @param target
* @param response
* @return
*/
public static JSONObject format(final JSONObject response) {
Log.i(TAG, "format response = \n" + JSON.toJSONString(response));
if (response == null || response.isEmpty()) {
Log.i(TAG, "format response == null || response.isEmpty() >> return response;");
return response;
}
JSONObject transferredObject = new JSONObject(true);
Set<String> set = response.keySet();
if (set != null) {
Object value;
String arrayKey;
for (String key : set) {
value = response.get(key);
if (value instanceof JSONArray) {//转化JSONArray内部的APIJSON Array
transferredObject.put(getSimpleName(key), format((JSONArray) value));
} else if (value instanceof JSONObject) {//APIJSON Array转为常规JSONArray
if (isArrayKey(key)) {//APIJSON Array转为常规JSONArray
arrayKey = key.substring(0, key.indexOf(KEY_ARRAY));
transferredObject.put(getArrayKey(getSimpleName(arrayKey))
, format(toArray((JSONObject) value, arrayKey)));//需要将name:alias传至toArray
} else {//常规JSONObject,往下一级提取
transferredObject.put(getSimpleName(key), format((JSONObject) value));
}
} else {//其它Object,直接填充
transferredObject.put(getSimpleName(key), value);
}
}
}
Log.i(TAG, "format return transferredObject = " + JSON.toJSONString(transferredObject));
return transferredObject;
}
/**
* @param responseArray
* @return
*/
public static JSONArray format(final JSONArray responseArray) {
Log.i(TAG, "format responseArray = \n" + JSON.toJSONString(responseArray));
if (responseArray == null || responseArray.isEmpty()) {
Log.i(TAG, "format responseArray == null || responseArray.isEmpty() >> return response;");
return responseArray;
}
JSONArray transferredArray = new JSONArray();
Object value;
for (int i = 0; i < responseArray.size(); i++) {
value = responseArray.get(i);
if (value instanceof JSONArray) {//转化JSONArray内部的APIJSON Array
transferredArray.add(format((JSONArray) value));
} else if (value instanceof JSONObject) {//JSONObject,往下一级提取
transferredArray.add(format((JSONObject) value));
} else {//其它Object,直接填充
transferredArray.add(responseArray.get(i));
}
}
Log.i(TAG, "format return transferredArray = " + JSON.toJSONString(transferredArray));
return transferredArray;
}
/**替换key+KEY_ARRAY为keyList
* @param key
* @return getSimpleName(isArrayKey(key) ? getArrayKey(...) : key) {@link #getSimpleName(String)}
*/
public static String replaceArray(String key) {
if (isArrayKey(key)) {
key = getArrayKey(key.substring(0, key.indexOf(KEY_ARRAY)));
}
return getSimpleName(key);
}
/**获取列表变量名
* @param key => StringUtil.getNoBlankString(key)
* @return empty ? "list" : key + "List" 且首字母小写
*/
public static String getArrayKey(String key) {
key = StringUtil.getNoBlankString(key);
if (key.isEmpty()) {
return "list";
}
String first = key.substring(0, 1);
if (bigAlphaPattern.matcher(first).matches()) {
key = first.toLowerCase() + key.substring(1, key.length());
}
return key + "List";
}
/**获取简单名称
* @param fullName name 或 name:alias
* @return name > name; name:alias > alias
*/
public static String getSimpleName(String fullName) {
//key:alias -> alias; key:alias[] -> alias[]
int index = fullName == null ? -1 : fullName.indexOf(":");
if (index >= 0) {
fullName = fullName.substring(index + 1);
}
return fullName;
}
}