-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathJSONObject.java
executable file
·490 lines (429 loc) · 13.2 KB
/
JSONObject.java
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
/*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 apijson;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import apijson.server.JSONRequest;
/**use this class instead of com.alibaba.fastjson.JSONObject
* @author Lemon
* @see #put
* @see #puts
* @see #putsAll
*/
public class JSONObject extends com.alibaba.fastjson.JSONObject {
private static final long serialVersionUID = 1L;
private static final String TAG = "JSONObject";
/**ordered
*/
public JSONObject() {
super(true);
}
/**transfer Object to JSONObject
* @param object
* @see {@link #JSONObject(Object)}
*/
public JSONObject(Object object) {
this(toJSONString(object));
}
/**parse JSONObject with JSON String
* @param json
* @see {@link #JSONObject(String)}
*/
public JSONObject(String json) {
this(parseObject(json));
}
/**transfer com.alibaba.fastjson.JSONObject to JSONObject
* @param object
* @see {@link #putsAll(Map<? extends String, ? extends Object>)}
*/
public JSONObject(com.alibaba.fastjson.JSONObject object) {
this();
putsAll(object);
}
//judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public static final String KEY_ARRAY = "[]";
/**判断是否为Array的key
* @param key
* @return
*/
public static boolean isArrayKey(String key) {
return key != null && key.endsWith(KEY_ARRAY);
}
/**判断是否为对应Table的key
* @param key
* @return
*/
public static boolean isTableKey(String key) {
return StringUtil.isBigName(key);
}
/**判断是否为对应Table数组的 key
* @param key
* @return
*/
public static boolean isTableArray(String key) {
return isArrayKey(key) && isTableKey(key.substring(0, key.length() - KEY_ARRAY.length()));
}
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//JSONObject内关键词 key <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public static String KEY_ID = "id";
public static String KEY_ID_IN = KEY_ID + "{}";
public static String KEY_USER_ID = "userId";
public static String KEY_USER_ID_IN = KEY_USER_ID + "{}";
/**set "id":id in Table layer
* @param id
* @return
*/
public JSONObject setId(Long id) {
return puts(KEY_ID, id);
}
/**set "id{}":[] in Table layer
* @param list
* @return
*/
public JSONObject setIdIn(List<Object> list) {
return puts(KEY_ID_IN, list);
}
/**set "userId":userId in Table layer
* @param id
* @return
*/
public JSONObject setUserId(Long id) {
return puts(KEY_USER_ID, id);
}
/**set "userId{}":[] in Table layer
* @param list
* @return
*/
public JSONObject setUserIdIn(List<Object> list) {
return puts(KEY_USER_ID_IN, list);
}
public static final int CACHE_ALL = 0;
public static final int CACHE_ROM = 1;
public static final int CACHE_RAM = 2;
public static final String CACHE_ALL_STRING = "ALL";
public static final String CACHE_ROM_STRING = "ROM";
public static final String CACHE_RAM_STRING = "RAM";
//@key关键字都放这个类 <<<<<<<<<<<<<<<<<<<<<<
public static final String KEY_TRY = "@try"; //尝试,忽略异常
public static final String KEY_CATCH = "@catch"; //TODO 捕捉到异常后,处理方式 null-不处理;DEFAULT-返回默认值;ORIGIN-返回请求里的原始值
public static final String KEY_DROP = "@drop"; //丢弃,不返回,TODO 应该通过 fastjson 的 ignore 之类的机制来处理,避免导致下面的对象也不返回
// public static final String KEY_KEEP = "@keep"; //一定会返回,为 null 或 空对象时,会使用默认值(非空),解决其它对象因为不关联的第一个对为空导致也不返回
public static final String KEY_DEFULT = "@default"; //TODO 自定义默认值 { "@default":true },@default 可完全替代 @keep
public static final String KEY_ROLE = "@role"; //角色,拥有对某些数据的某些操作的权限
public static final String KEY_DATABASE = "@database"; //数据库类型,默认为MySQL
public static final String KEY_EXPLAIN = "@explain"; //分析 true/false
public static final String KEY_CACHE = "@cache"; //缓存 RAM/ROM/ALL
public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明
public static final String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数
public static final String KEY_FROM = "@from"; //FROM语句
public static final String KEY_COMBINE = "@combine"; //条件组合,每个条件key前面可以放&,|,!逻辑关系 "id!{},&sex,!name&$"
public static final String KEY_GROUP = "@group"; //分组方式
public static final String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用
public static final String KEY_ORDER = "@order"; //排序方式
public static final String KEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出
public static final String KEY_RAW = "@raw"; //自定义where条件拼接
public static final List<String> TABLE_KEY_LIST;
static {
TABLE_KEY_LIST = new ArrayList<String>();
TABLE_KEY_LIST.add(KEY_ROLE);
TABLE_KEY_LIST.add(KEY_DATABASE);
TABLE_KEY_LIST.add(KEY_EXPLAIN);
TABLE_KEY_LIST.add(KEY_CACHE);
TABLE_KEY_LIST.add(KEY_SCHEMA);
TABLE_KEY_LIST.add(KEY_COLUMN);
TABLE_KEY_LIST.add(KEY_FROM);
TABLE_KEY_LIST.add(KEY_COMBINE);
TABLE_KEY_LIST.add(KEY_GROUP);
TABLE_KEY_LIST.add(KEY_HAVING);
TABLE_KEY_LIST.add(KEY_ORDER);
TABLE_KEY_LIST.add(KEY_JSON);
TABLE_KEY_LIST.add(KEY_RAW);
}
//@key关键字都放这个类 >>>>>>>>>>>>>>>>>>>>>>
/**set try, ignore exceptions
* @param tri
* @return this
*/
public JSONObject setTry(Boolean tri) {
return puts(KEY_TRY, tri);
}
/**set catch
* @param isCatch
* @return this
*/
public JSONObject setCatch(String isCatch) {
return puts(KEY_CATCH, isCatch);
}
/**set drop, data dropped will not return
* @param drop
* @return this
*/
public JSONObject setDrop(Boolean drop) {
return puts(KEY_DROP, drop);
}
/**set if has default
* @param hasDefault
* @return this
*/
public JSONObject setDefault(Boolean hasDefault) {
return puts(KEY_DEFULT, hasDefault);
}
/**set role of request sender
* @param role
* @return this
*/
public JSONObject setRole(String role) {
return puts(KEY_ROLE, role);
}
/**set database where table was puts
* @param database
* @return this
*/
public JSONObject setDatabase(String database) {
return puts(KEY_DATABASE, database);
}
/**set if return explain informations
* @param explain
* @return
*/
public JSONObject setExplain(Boolean explain) {
return puts(KEY_EXPLAIN, explain);
}
/**set cache type
* @param cache
* @return
* @see {@link #CACHE_ALL}
* @see {@link #CACHE_RAM}
* @see {@link #CACHE_ROM}
*/
public JSONObject setCache(Integer cache) {
return puts(KEY_CACHE, cache);
}
/**set cache type
* @param cache
* @return
* @see {@link #CACHE_ALL_STRING}
* @see {@link #CACHE_RAM_STRING}
* @see {@link #CACHE_ROM_STRING}
*/
public JSONObject setCache(String cache) {
return puts(KEY_CACHE, cache);
}
/**set schema where table was puts
* @param schema
* @return this
*/
public JSONObject setSchema(String schema) {
return puts(KEY_SCHEMA, schema);
}
/**set keys need to be returned
* @param keys key0, key1, key2 ...
* @return {@link #setColumn(String)}
*/
public JSONObject setColumn(String... keys) {
return setColumn(StringUtil.getString(keys, true));
}
/**set keys need to be returned
* @param keys "key0,key1,key2..."
* @return
*/
public JSONObject setColumn(String keys) {
return puts(KEY_COLUMN, keys);
}
/**set combination of keys for conditions
* @param keys key0,&key1,|key2,!kye3 ...
* @return {@link #setColumn(String)}
*/
public JSONObject setCombine(String... keys) {
return setCombine(StringUtil.getString(keys, true));
}
/**set combination of keys for conditions
* @param keys key0,&key1,|key2,!kye3 ...
* @return
*/
public JSONObject setCombine(String keys) {
return puts(KEY_COMBINE, keys);
}
/**set keys for group by
* @param keys key0, key1, key2 ...
* @return {@link #setGroup(String)}
*/
public JSONObject setGroup(String... keys) {
return setGroup(StringUtil.getString(keys, true));
}
/**set keys for group by
* @param keys "key0,key1,key2..."
* @return
*/
public JSONObject setGroup(String keys) {
return puts(KEY_GROUP, keys);
}
/**set keys for having
* @param keys count(key0) > 1, sum(key1) <= 5, function2(key2) ? value2 ...
* @return {@link #setHaving(String)}
*/
public JSONObject setHaving(String... keys) {
return setHaving(StringUtil.getString(keys, true));
}
/**set keys for having
* @param keys "key0,key1,key2..."
* @return
*/
public JSONObject setHaving(String keys) {
return puts(KEY_HAVING, keys);
}
/**set keys for order by
* @param keys key0, key1+, key2- ...
* @return {@link #setOrder(String)}
*/
public JSONObject setOrder(String... keys) {
return setOrder(StringUtil.getString(keys, true));
}
/**set keys for order by
* @param keys "key0,key1+,key2-..."
* @return
*/
public JSONObject setOrder(String keys) {
return puts(KEY_ORDER, keys);
}
/**set keys to cast to json
* @param keys "key0,key1,key2..."
* @return
*/
public JSONObject setJSON(String keys) {
return puts(KEY_JSON, keys);
}
//JSONObject内关键词 key >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Request <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**
* @param key
* @param keys path = keys[0] + "/" + keys[1] + "/" + keys[2] + ...
* @return {@link #puts(String, Object)}
*/
public JSONObject putsPath(String key, String... keys) {
return puts(key+"@", StringUtil.getString(keys, "/"));
}
/**
* @param key
* @param isNull
* @return {@link #puts(String, Object)}
*/
public JSONObject putsNull(String key, boolean isNull) {
return puts(key+"{}", SQL.isNull(isNull));
}
/**
* trim = false
* @param key
* @param isEmpty
* @return {@link #putsEmpty(String, boolean, boolean)}
*/
public JSONObject putsEmpty(String key, boolean isEmpty) {
return putsEmpty(key, isEmpty, false);
}
/**
* @param key
* @param isEmpty
* @return {@link #puts(String, Object)}
*/
public JSONObject putsEmpty(String key, boolean isEmpty, boolean trim) {
return puts(key+"{}", SQL.isEmpty(key, isEmpty, trim));
}
/**
* @param key
* @param compare <=0, >5 ...
* @return {@link #puts(String, Object)}
*/
public JSONObject putsLength(String key, String compare) {
return puts(key+"{}", SQL.length(key) + compare);
}
/**设置搜索
* type = SEARCH_TYPE_CONTAIN_FULL
* @param key
* @param value
* @return {@link #putsSearch(String, String, int)}
*/
public JSONObject putsSearch(String key, String value) {
return putsSearch(key, value, SQL.SEARCH_TYPE_CONTAIN_FULL);
}
/**设置搜索
* @param key
* @param value
* @param type
* @return {@link #puts(String, Object)}
*/
public JSONObject putsSearch(String key, String value, int type) {
return puts(key+"$", SQL.search(value, type));
}
//Request >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/**puts key-value in object into this
* @param map
* @return this
*/
public JSONObject putsAll(Map<? extends String, ? extends Object> map) {
putAll(map);
return this;
}
@Override
public void putAll(Map<? extends String, ? extends Object> map) {
if (map != null && map.isEmpty() == false) {
super.putAll(map);
}
}
/**put and return this
* @param value must be annotated by {@link MethodAccess}
* @return {@link #puts(String, Object)}
*/
public JSONObject puts(Object value) {
return puts(null, value);
}
/**put and return this
* @param key
* @param value
* @return this
* @see {@link #put(String, Object)}
*/
public JSONObject puts(String key, Object value) {
put(key, value);
return this;
}
/**put and return value
* @param value must be annotated by {@link MethodAccess}
* @return {@link #put(String, Object)}
*/
public Object put(Object value) {
return put(null, value);
}
/**put and return value
* @param key StringUtil.isEmpty(key, true) ? key = value.getClass().getSimpleName();
* @param value
* @return value
*/
@Override
public Object put(String key, Object value) {
if (value == null) {
Log.e(TAG, "put value == null >> return null;");
return null;
}
if (StringUtil.isEmpty(key, true)) {
Class<?> clazz = value.getClass(); //should not return null
if (clazz.getAnnotation(MethodAccess.class) == null) {
throw new IllegalArgumentException("puts StringUtil.isEmpty(key, true)" +
" clazz.getAnnotation(MethodAccess.class) == null" +
" \n key为空时仅支持 类型被@MethodAccess注解 的value !!!" +
" \n 如果一定要这么用,请对 " + clazz.getName() + " 注解!" +
" \n 如果是类似 key[]:{} 结构的请求,建议用 putsAll(...) !");
}
key = value.getClass().getSimpleName();
}
return super.put(key, value);
}
}