Skip to content

Commit 8a4074f

Browse files
committed
Client:同步adt版至studio版
1 parent 48d9e01 commit 8a4074f

27 files changed

Lines changed: 971 additions & 801 deletions

APIJSON(Android)/APIJSON(AndroidStudio)/APIJSONLibrary/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ android {
2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
2424
testCompile 'junit:junit:4.12'
25-
compile files('libs/fastjson-1.2.21.jar')
25+
compile files('libs/fastjson-1.2.24.jar')
2626
}
Binary file not shown.
Binary file not shown.

APIJSON(Android)/APIJSON(AndroidStudio)/APIJSONLibrary/src/main/java/zuo/biao/apijson/JSON.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ public static String getCorrectJson(String s, boolean isArray) {
7272
return s;//isJsonCorrect(s) ? s : null;
7373
}
7474

75+
/**json转JSONObject
76+
* @param json
77+
* @return
78+
*/
79+
public static JSONObject parseObject(Object obj) {
80+
return parseObject(toJSONString(obj));
81+
}
7582
/**json转JSONObject
7683
* @param json
7784
* @return
@@ -201,5 +208,48 @@ public static String format(JSONObject object) {
201208
return toJSONString(object, SerializerFeature.PrettyFormat);
202209
}
203210

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+
204254

205255
}

APIJSON(Android)/APIJSON(AndroidStudio)/APIJSONLibrary/src/main/java/zuo/biao/apijson/JSONObject.java

Lines changed: 142 additions & 18 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,63 +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-
72-
73-
public static final String KEY_COLUMNS = "@columns";//@key关键字都放这个类
74114

75-
/**set columns need to be returned
76-
* @param columns "column0,column1,column2..."
77-
* @return
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
78120
*/
79-
public JSONObject setColumns(String columns) {
80-
put(KEY_COLUMNS, columns);
81-
return this;
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);
82144
}
83-
public String getColumns() {
84-
return getString(KEY_COLUMNS);
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);
85192
}
86193

87194

88195

89196
//judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
90197
public static final String KEY_ARRAY = "[]";
91-
198+
92199
/**判断是否为Array的key
93200
* @param key
94201
* @return
@@ -112,4 +219,21 @@ public static boolean isWord(String key) {
112219
}
113220
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
114221

222+
223+
224+
public static final String KEY_COLUMNS = "@columns";//@key关键字都放这个类
225+
226+
/**set columns need to be returned
227+
* @param columns "column0,column1,column2..."
228+
* @return
229+
*/
230+
public JSONObject setColumns(String columns) {
231+
put(KEY_COLUMNS, columns);
232+
return this;
233+
}
234+
public String getColumns() {
235+
return getString(KEY_COLUMNS);
236+
}
237+
238+
115239
}

0 commit comments

Comments
 (0)