Skip to content

Commit 48d9e01

Browse files
committed
Server:同步eclipse版至idea版
1 parent 2a283ce commit 48d9e01

8 files changed

Lines changed: 444 additions & 166 deletions

File tree

‎APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/JSON.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,48 @@ public static String format(JSONObject object) {
208208
return toJSONString(object, SerializerFeature.PrettyFormat);
209209
}
210210

211+
/**判断是否为JSONObject
212+
* @param obj instanceof String ? parseObject
213+
* @return
214+
*/
215+
public static boolean isJSONObject(Object obj) {
216+
if (obj instanceof JSONObject) {
217+
return true;
218+
}
219+
if (obj instanceof String) {
220+
try {
221+
JSONObject json = parseObject((String) obj);
222+
return json != null && json.isEmpty() == false;
223+
} catch (Exception e) {
224+
//太长 System.out.println(TAG + "select while (rs.next()){ >> i = "
225+
// + i + " try { json = JSON.parse((String) value);"
226+
// + ">> } catch (Exception e) {\n" + e.getMessage());
227+
}
228+
}
229+
230+
return false;
231+
}
232+
/**判断是否为JSONArray
233+
* @param obj instanceof String ? parseArray
234+
* @return
235+
*/
236+
public static boolean isJSONArray(Object obj) {
237+
if (obj instanceof JSONArray) {
238+
return true;
239+
}
240+
if (obj instanceof String) {
241+
try {
242+
JSONArray json = parseArray((String) obj);
243+
return json != null && json.isEmpty() == false;
244+
} catch (Exception e) {
245+
//太长 System.out.println(TAG + "select while (rs.next()){ >> i = "
246+
// + i + " try { json = JSON.parse((String) value);"
247+
// + ">> } catch (Exception e) {\n" + e.getMessage());
248+
}
249+
}
250+
251+
return false;
252+
}
253+
211254

212255
}

‎APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/JSONObject.java‎

Lines changed: 135 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
package zuo.biao.apijson;
1616

17+
import static zuo.biao.apijson.StringUtil.UTF_8;
1718
import static zuo.biao.apijson.StringUtil.bigAlphaPattern;
1819
import static zuo.biao.apijson.StringUtil.namePattern;
1920

21+
import java.io.UnsupportedEncodingException;
22+
import java.net.URLDecoder;
23+
import java.net.URLEncoder;
2024
import java.util.Set;
2125

2226

23-
/**use this class instead of com.alibaba.fastjson.JSONObject
27+
/**use this class instead of com.alibaba.fastjson.JSONObject, not encode in default cases
2428
* @author Lemon
2529
*/
2630
public class JSONObject extends com.alibaba.fastjson.JSONObject {
@@ -32,48 +36,166 @@ public JSONObject() {
3236
super(true);
3337
}
3438
/**transfer Object to JSONObject
39+
* encode = false;
3540
* @param object
41+
* @see {@link #JSONObject(Object, boolean)}
3642
*/
3743
public JSONObject(Object object) {
38-
this(toJSONString(object));
44+
this(object, false);
45+
}
46+
/**transfer Object to JSONObject
47+
* @param object
48+
* @param encode
49+
* @see {@link #JSONObject(String, boolean)}
50+
*/
51+
public JSONObject(Object object, boolean encode) {
52+
this(toJSONString(object), encode);
3953
}
4054
/**parse JSONObject with JSON String
55+
* encode = false;
4156
* @param json
57+
* @see {@link #JSONObject(String, boolean)}
4258
*/
4359
public JSONObject(String json) {
44-
this(parseObject(json));
60+
this(json, false);
61+
}
62+
/**parse JSONObject with JSON String
63+
* @param json
64+
* @param encode
65+
* @see {@link #JSONObject(com.alibaba.fastjson.JSONObject, boolean)}
66+
*/
67+
public JSONObject(String json, boolean encode) {
68+
this(parseObject(json), encode);
4569
}
4670
/**transfer com.alibaba.fastjson.JSONObject to JSONObject
71+
* encode = false;
4772
* @param object
73+
* @see {@link #JSONObject(com.alibaba.fastjson.JSONObject, boolean)}
4874
*/
4975
public JSONObject(com.alibaba.fastjson.JSONObject object) {
76+
this(object, false);
77+
}
78+
/**transfer com.alibaba.fastjson.JSONObject to JSONObject
79+
* @param object
80+
* @param encode
81+
* @see {@link #add(com.alibaba.fastjson.JSONObject, boolean)}
82+
*/
83+
public JSONObject(com.alibaba.fastjson.JSONObject object, boolean encode) {
5084
this();
51-
add(object);
85+
add(object, encode);
5286
}
5387

5488

5589

5690

5791
/**put key-value in object into this
92+
* encode = false;
5893
* @param object
59-
* @return this
94+
* @return {@link #add(com.alibaba.fastjson.JSONObject, boolean)}
6095
*/
6196
public JSONObject add(com.alibaba.fastjson.JSONObject object) {
97+
return add(object, false);
98+
}
99+
/**put key-value in object into this
100+
* @param object
101+
* @param encode
102+
* @return this
103+
*/
104+
public JSONObject add(com.alibaba.fastjson.JSONObject object, boolean encode) {
62105
Set<String> set = object == null ? null : object.keySet();
63106
if (set != null) {
64107
for (String key : set) {
65-
put(key, object.get(key));
108+
put(key, object.get(key), encode);
66109
}
67110
}
68111
return this;
69112
}
70113

71-
114+
115+
116+
/**
117+
* @param key if decode && key instanceof String, key = URLDecoder.decode((String) key, UTF_8)
118+
* @param decode if decode && value instanceof String, value = URLDecoder.decode((String) value, UTF_8)
119+
* @return
120+
*/
121+
public Object get(Object key, boolean decode) {
122+
if (decode) {
123+
if (key instanceof String) {
124+
if (((String) key).endsWith("+") || ((String) key).endsWith("-")) {
125+
try {//多层encode导致内部Comment[]传到服务端decode后最终变为Comment%5B%5D
126+
key = URLDecoder.decode((String) key, UTF_8);
127+
} catch (UnsupportedEncodingException e) {
128+
e.printStackTrace();
129+
return null;
130+
}
131+
}
132+
}
133+
Object value = super.get(key);
134+
if (value instanceof String) {
135+
try {
136+
value = URLDecoder.decode((String) value, UTF_8);
137+
} catch (UnsupportedEncodingException e) {
138+
e.printStackTrace();
139+
}
140+
}
141+
return value;
142+
}
143+
return super.get(key);
144+
}
145+
146+
/**
147+
* encode = false
148+
* @param value
149+
* @return {@link #put(String, boolean)}
150+
*/
151+
public Object put(Object value) {
152+
return put(value, false);
153+
}
154+
/**
155+
* key = value.getClass().getSimpleName()
156+
* @param value
157+
* @param encode
158+
* @return {@link #put(String, Object, boolean)}
159+
*/
160+
public Object put(Object value, boolean encode) {
161+
return put(null, value, encode);
162+
}
163+
/**
164+
* @param key if StringUtil.isNotEmpty(key, true) == false,
165+
* <br> key = value == null ? null : value.getClass().getSimpleName();
166+
* <br> >> if decode && key instanceof String, key = URLDecoder.decode((String) key, UTF_8)
167+
* @param value URLEncoder.encode((String) value, UTF_8);
168+
* @param encode if value instanceof String, value = URLEncoder.encode((String) value, UTF_8);
169+
* @return
170+
*/
171+
public Object put(String key, Object value, boolean encode) {
172+
if (StringUtil.isNotEmpty(key, true) == false) {
173+
key = value == null ? null : value.getClass().getSimpleName();
174+
}
175+
if (encode) {
176+
if (key.endsWith("+") || key.endsWith("-")) {
177+
try {//多层encode导致内部Comment[]传到服务端decode后最终变为Comment%5B%5D
178+
key = URLEncoder.encode(key, UTF_8);
179+
} catch (UnsupportedEncodingException e) {
180+
e.printStackTrace();
181+
}
182+
}
183+
if (value instanceof String) {//只在value instanceof String时encode key?{@link #get(Object, boolean)}内做不到
184+
try {
185+
value = URLEncoder.encode((String) value, UTF_8);
186+
} catch (UnsupportedEncodingException e) {
187+
e.printStackTrace();
188+
}
189+
}
190+
}
191+
return super.put(key, value);
192+
}
193+
72194

73195

74196
//judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
75197
public static final String KEY_ARRAY = "[]";
76-
198+
77199
/**判断是否为Array的key
78200
* @param key
79201
* @return
@@ -96,9 +218,9 @@ public static boolean isWord(String key) {
96218
return StringUtil.isNotEmpty(key, false) && namePattern.matcher(key).matches();
97219
}
98220
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
99-
100-
101-
221+
222+
223+
102224
public static final String KEY_COLUMNS = "@columns";//@key关键字都放这个类
103225

104226
/**set columns need to be returned
@@ -112,6 +234,6 @@ public JSONObject setColumns(String columns) {
112234
public String getColumns() {
113235
return getString(KEY_COLUMNS);
114236
}
115-
116-
237+
238+
117239
}

0 commit comments

Comments
 (0)