forked from TommyLemon/APIAuto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONResponse.js
More file actions
284 lines (236 loc) · 8.31 KB
/
JSONResponse.js
File metadata and controls
284 lines (236 loc) · 8.31 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
/*Copyright ©2017 TommyLemon(https://github.com/TommyLemon/APIJSONAuto)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use JSONResponse 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.*/
/**parser for json response
* @author Lemon
*/
//状态信息,非GET请求获得的信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const CODE_SUCCESS = 200; //成功
const CODE_UNSUPPORTED_ENCODING = 400; //编码错误
const CODE_ILLEGAL_ACCESS = 401; //权限错误
const CODE_UNSUPPORTED_OPERATION = 403; //禁止操作
const CODE_NOT_FOUND = 404; //未找到
const CODE_ILLEGAL_ARGUMENT = 406; //参数错误
const CODE_NOT_LOGGED_IN = 407; //未登录
const CODE_TIME_OUT = 408; //超时
const CODE_CONFLICT = 409; //重复,已存在
const CODE_CONDITION_ERROR = 412; //条件错误,如密码错误
const CODE_UNSUPPORTED_TYPE = 415; //类型错误
const CODE_OUT_OF_RANGE = 416; //超出范围
const CODE_NULL_POINTER = 417; //对象为空
const CODE_SERVER_ERROR = 500; //服务器内部错误
const MSG_SUCCEED = "success"; //成功
const MSG_SERVER_ERROR = "Internal Server Error!"; //服务器内部错误
const KEY_CODE = "code";
const KEY_MSG = "msg";
const KEY_ID = "id";
const KEY_ID_IN = KEY_ID + "{}";
const KEY_COUNT = "count";
const KEY_TOTAL = "total";
var JSONResponse = {
TAG: 'JSONResponse',
/**是否成功
* @param code
* @return
*/
isSuccess: function(code) {
return code == CODE_SUCCESS;
},
/**校验服务端是否存在table
* @param count
* @return
*/
isExist: function(count) {
return count > 0;
},
/**格式化key名称
* @param object
* @return
*/
formatObject: function(object) {
//太长查看不方便,不如debug log(JSONResponse.TAG, "format object = \n" + JSON.toJSONString(object));
if (object == null || object == '') {
log(JSONResponse.TAG, "format object == null || object == '' >> return object;");
return object;
}
var formattedObject = {};
var value;
for (var key in object) {
value = object[key];
if (value instanceof Array) { // JSONArray,遍历来format内部项
formattedObject[JSONResponse.replaceArray(key)] = JSONResponse.formatArray(value);
}
else if (value instanceof Object) { // JSONObject,往下一级提取
formattedObject[JSONResponse.getSimpleName(key)] = JSONResponse.formatObject(value);
}
else { // 其它Object,直接填充
formattedObject[JSONResponse.getSimpleName(key)] = value;
}
}
//太长查看不方便,不如debug log(JSONResponse.TAG, "format return formattedObject = " + JSON.toJSONString(formattedObject));
return formattedObject;
},
/**格式化key名称
* @param array
* @return
*/
formatArray: function(array) {
//太长查看不方便,不如debug log(JSONResponse.TAG, "format array = \n" + JSON.toJSONString(array));
if (array == null || array == '') {
log(JSONResponse.TAG, "format array == null || array == '' >> return array;");
return array;
}
var formattedArray = [];
var value;
for (var i = 0; i < array.length; i++) {
value = array[i];
if (value instanceof Array) { // JSONArray,遍历来format内部项
formattedArray.push(JSONResponse.formatArray(value));
}
else if (value instanceof Object) { // JSONObject,往下一级提取
formattedArray.push(JSONResponse.formatObject(value));
}
else { // 其它Object,直接填充
formattedArray.push(value);
}
}
//太长查看不方便,不如debug log(JSONResponse.TAG, "format return formattedArray = " + JSON.toJSONString(formattedArray));
return formattedArray;
},
/**替换key+KEY_ARRAY为keyList
* @param key
* @return getSimpleName(isArrayKey(key) ? getArrayKey(...) : key) {@link #getSimpleName(String)}
*/
replaceArray: function(key) {
if (JSONObject.isArrayKey(key)) {
key = JSONResponse.getArrayKey(key.substring(0, key.lastIndexOf('[]')));
}
return JSONResponse.getSimpleName(key);
},
/**获取列表变量名
* @param key => getNoBlankString(key)
* @return empty ? "list" : key + "List" 且首字母小写
*/
getArrayKey: function(key) {
return StringUtil.addSuffix(key, "List");
},
/**获取简单名称
* @param fullName name 或 name:alias
* @return name => name; name:alias => alias
*/
getSimpleName: function(fullName) {
//key:alias -> alias; key:alias[] -> alias[]
var index = fullName == null ? -1 : fullName.indexOf(":");
if (index >= 0) {
fullName = fullName.substring(index + 1);
}
return fullName;
},
COMPARE_NO_STANDARD: -1,
COMPARE_EQUAL: 0,
COMPARE_KEY_MORE: 1,
COMPARE_VALUE_CHANGE: 2,
COMPARE_KEY_LESS: 3,
COMPARE_TYPE_CHANGE: 4,
COMPARE_NUMBER_TYPE_CHANGE: 3,
COMPARE_CODE_CHANGE: 4,
/**测试compare: 对比 新的请求与上次请求的结果
0-相同,无颜色;
1-对象新增字段或数组新增值,绿色;
2-值改变,蓝色;
3-对象缺少字段/整数变小数,黄色;
4-类型/code改变,红色;
*/
compareResponse: function(target, real) {
if (target == null || target.code == null) {
return JSONResponse.COMPARE_NO_STANDARD; //未上传对比标准(正确的结果)
}
if (target.code != real.code) {
return JSONResponse.COMPARE_CODE_CHANGE;
}
delete target.code;
delete real.code;
delete target.message;
delete real.message;
return JSONResponse.compare(target, real);
},
/**测试compare: 对比 新的请求与上次请求的结果
0-相同,无颜色;
1-对象新增字段或数组新增值,绿色;
2-值改变,蓝色;
3-对象缺少字段/整数变小数,黄色;
4-类型/success/errCode改变,红色;
*/
compare: function(target, real) {
if (target == null) {
return real == null ? JSONResponse.COMPARE_EQUAL : JSONResponse.COMPARE_KEY_MORE;
}
if (real == null) { //少了key
return JSONResponse.COMPARE_KEY_LESS;
}
var type = typeof target;
if (type != typeof real) { //类型改变
return JSONResponse.COMPARE_TYPE_CHANGE;
}
var max = JSONResponse.COMPARE_EQUAL;
var each = JSONResponse.COMPARE_EQUAL;
if (target instanceof Array) { // JSONArray
if (target.length != real.length) {
max = JSONResponse.COMPARE_VALUE_CHANGE;
}
var length = Math.min(target.length, real.length); //多或少都不影响
//TODO 需要把所有值合并,得到最全的,至于类型,Java是很稳定的,同样的key在所有Object的type都相同。
for (var i = 0; i < length; i++) { //遍历并递归下一层
each = JSONResponse.compare(target[i], real[i]);
if (max < each) {
max = each;
}
if (max >= JSONResponse.COMPARE_TYPE_CHANGE) {
break;
}
}
}
else if (target instanceof Object) { // JSONObject
var tks = Object.keys(target);
var key;
for (var i = 0; i< tks.length; i++) { //遍历并递归下一层
key = tks[i];
if (key == null) {
continue;
}
each = JSONResponse.compare(target[key], real[key]);
if (max < each) {
max = each;
}
if (max >= JSONResponse.COMPARE_TYPE_CHANGE) {
break;
}
}
if (max < JSONResponse.COMPARE_KEY_MORE) { //多出key
for (var k in real) {
if (k != null && tks.indexOf(k) < 0) {
max = JSONResponse.COMPARE_KEY_MORE;
}
}
}
}
else { // Boolean, Number, String
if (type == 'number') { //数字类型由整数变为小数
if (String(target).indexOf('.') < 0 && String(real).indexOf('.') >= 0) {
return JSONResponse.COMPARE_NUMBER_TYPE_CHANGE;
}
}
if (target !== real) { //值不同
return JSONResponse.COMPARE_VALUE_CHANGE;
}
}
return max;
}
}