1
+ /*Copyright ©2017 TommyLemon(https://github.com/TommyLemon/APIJSONAuto)
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use JSONResponse file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.*/
14
+
15
+
16
+ /**parser for json response
17
+ * @author Lemon
18
+ */
19
+
20
+
21
+ //状态信息,非GET请求获得的信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
+
23
+ const CODE_SUCCESS = 200 ; //成功
24
+ const CODE_UNSUPPORTED_ENCODING = 400 ; //编码错误
25
+ const CODE_ILLEGAL_ACCESS = 401 ; //权限错误
26
+ const CODE_UNSUPPORTED_OPERATION = 403 ; //禁止操作
27
+ const CODE_NOT_FOUND = 404 ; //未找到
28
+ const CODE_ILLEGAL_ARGUMENT = 406 ; //参数错误
29
+ const CODE_NOT_LOGGED_IN = 407 ; //未登录
30
+ const CODE_TIME_OUT = 408 ; //超时
31
+ const CODE_CONFLICT = 409 ; //重复,已存在
32
+ const CODE_CONDITION_ERROR = 412 ; //条件错误,如密码错误
33
+ const CODE_UNSUPPORTED_TYPE = 415 ; //类型错误
34
+ const CODE_OUT_OF_RANGE = 416 ; //超出范围
35
+ const CODE_NULL_POINTER = 417 ; //对象为空
36
+ const CODE_SERVER_ERROR = 500 ; //服务器内部错误
37
+
38
+
39
+ const MSG_SUCCEED = "success" ; //成功
40
+ const MSG_SERVER_ERROR = "Internal Server Error!" ; //服务器内部错误
41
+
42
+
43
+ const KEY_CODE = "code" ;
44
+ const KEY_MSG = "msg" ;
45
+ const KEY_ID = "id" ;
46
+ const KEY_ID_IN = KEY_ID + "{}" ;
47
+ const KEY_COUNT = "count" ;
48
+ const KEY_TOTAL = "total" ;
49
+
50
+ var JSONResponse = {
51
+ TAG : 'JSONResponse' ,
52
+
53
+ /**是否成功
54
+ * @param code
55
+ * @return
56
+ */
57
+ isSuccess : function ( code ) {
58
+ return code == CODE_SUCCESS ;
59
+ } ,
60
+
61
+ /**校验服务端是否存在table
62
+ * @param count
63
+ * @return
64
+ */
65
+ isExist : function ( count ) {
66
+ return count > 0 ;
67
+ } ,
68
+
69
+
70
+
71
+
72
+
73
+ /**格式化key名称
74
+ * @param object
75
+ * @return
76
+ */
77
+ formatObject : function ( object ) {
78
+ //太长查看不方便,不如debug log(JSONResponse.TAG, "format object = \n" + JSON.toJSONString(object));
79
+ if ( object == null || object == '' ) {
80
+ log ( JSONResponse . TAG , "format object == null || object == '' >> return object;" ) ;
81
+ return object ;
82
+ }
83
+ var formattedObject = { } ;
84
+
85
+ var value ;
86
+ for ( var key in object ) {
87
+ value = object [ key ] ;
88
+
89
+ if ( value instanceof Array ) { // JSONArray,遍历来format内部项
90
+ formattedObject [ JSONResponse . replaceArray ( key ) ] = JSONResponse . formatArray ( value ) ;
91
+ }
92
+ else if ( value instanceof Object ) { // JSONObject,往下一级提取
93
+ formattedObject [ JSONResponse . getSimpleName ( key ) ] = JSONResponse . formatObject ( value ) ;
94
+ }
95
+ else { // 其它Object,直接填充
96
+ formattedObject [ JSONResponse . getSimpleName ( key ) ] = value ;
97
+ }
98
+ }
99
+
100
+ //太长查看不方便,不如debug log(JSONResponse.TAG, "format return formattedObject = " + JSON.toJSONString(formattedObject));
101
+ return formattedObject ;
102
+ } ,
103
+
104
+ /**格式化key名称
105
+ * @param array
106
+ * @return
107
+ */
108
+ formatArray : function ( array ) {
109
+ //太长查看不方便,不如debug log(JSONResponse.TAG, "format array = \n" + JSON.toJSONString(array));
110
+ if ( array == null || array == '' ) {
111
+ log ( JSONResponse . TAG , "format array == null || array == '' >> return array;" ) ;
112
+ return array ;
113
+ }
114
+ var formattedArray = [ ] ;
115
+
116
+ var value ;
117
+ for ( var i = 0 ; i < array . length ; i ++ ) {
118
+ value = array [ i ] ;
119
+ if ( value instanceof Array ) { // JSONArray,遍历来format内部项
120
+ formattedArray . push ( JSONResponse . formatArray ( value ) ) ;
121
+ }
122
+ else if ( value instanceof Object ) { // JSONObject,往下一级提取
123
+ formattedArray . push ( JSONResponse . formatObject ( value ) ) ;
124
+ }
125
+ else { // 其它Object,直接填充
126
+ formattedArray . push ( value ) ;
127
+ }
128
+ }
129
+
130
+ //太长查看不方便,不如debug log(JSONResponse.TAG, "format return formattedArray = " + JSON.toJSONString(formattedArray));
131
+ return formattedArray ;
132
+ } ,
133
+
134
+ /**替换key+KEY_ARRAY为keyList
135
+ * @param key
136
+ * @return getSimpleName(isArrayKey(key) ? getArrayKey(...) : key) {@link #getSimpleName(String)}
137
+ */
138
+ replaceArray : function ( key ) {
139
+ if ( JSONObject . isArrayKey ( key ) ) {
140
+ key = JSONResponse . getArrayKey ( key . substring ( 0 , key . lastIndexOf ( '[]' ) ) ) ;
141
+ }
142
+ return JSONResponse . getSimpleName ( key ) ;
143
+ } ,
144
+ /**获取列表变量名
145
+ * @param key => getNoBlankString(key)
146
+ * @return empty ? "list" : key + "List" 且首字母小写
147
+ */
148
+ getArrayKey : function ( key ) {
149
+ return StringUtil . addSuffix ( key , "List" ) ;
150
+ } ,
151
+
152
+ /**获取简单名称
153
+ * @param fullName name 或 name:alias
154
+ * @return name => name; name:alias => alias
155
+ */
156
+ getSimpleName : function ( fullName ) {
157
+ //key:alias -> alias; key:alias[] -> alias[]
158
+ var index = fullName == null ? - 1 : fullName . indexOf ( ":" ) ;
159
+ if ( index >= 0 ) {
160
+ fullName = fullName . substring ( index + 1 ) ;
161
+ }
162
+ return fullName ;
163
+ }
164
+
165
+
166
+ }
0 commit comments