forked from Tencent/APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONObject.java
More file actions
117 lines (93 loc) · 2.81 KB
/
JSONObject.java
File metadata and controls
117 lines (93 loc) · 2.81 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
/*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 static zuo.biao.apijson.StringUtil.namePattern;
import java.util.Set;
/**use this class instead of com.alibaba.fastjson.JSONObject
* @author Lemon
*/
public class JSONObject extends com.alibaba.fastjson.JSONObject {
private static final long serialVersionUID = 8907029699680768212L;
/**ordered
*/
public JSONObject() {
super(true);
}
/**transfer Object to JSONObject
* @param object
*/
public JSONObject(Object object) {
this(toJSONString(object));
}
/**parse JSONObject with JSON String
* @param json
*/
public JSONObject(String json) {
this(parseObject(json));
}
/**transfer com.alibaba.fastjson.JSONObject to JSONObject
* @param object
*/
public JSONObject(com.alibaba.fastjson.JSONObject object) {
this();
add(object);
}
/**put key-value in object into this
* @param object
* @return this
*/
public JSONObject add(com.alibaba.fastjson.JSONObject object) {
Set<String> set = object == null ? null : object.keySet();
if (set != null) {
for (String key : set) {
put(key, object.get(key));
}
}
return this;
}
//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 isWord(key) && bigAlphaPattern.matcher(key.substring(0, 1)).matches();
}
/**判断是否为词,只能包含字母,数字或下划线
* @param key
* @return
*/
public static boolean isWord(String key) {
return StringUtil.isNotEmpty(key, false) && namePattern.matcher(key).matches();
}
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public static final String KEY_COLUMNS = "@columns";//@key关键字都放这个类
/**set columns need to be returned
* @param columns "column0,column1,column2..."
* @return
*/
public JSONObject setColumns(String columns) {
put(KEY_COLUMNS, columns);
return this;
}
public String getColumns() {
return getString(KEY_COLUMNS);
}
}