Skip to content

Commit 86097f3

Browse files
committed
Server:与客户端JSON, JSONObject,JSONRequest,JSONResponse同步;Client:与服务端JSON同步;优化JSONObject,JSONRequest,JSONResponse
1 parent 65a262b commit 86097f3

File tree

9 files changed

+415
-175
lines changed

9 files changed

+415
-175
lines changed

APIJSON(Android)/APIJSON(ADT)/APIJSONDemoApp/src/apijson/demo/RequestUtil.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class RequestUtil {
3838

3939
private static final long DEFAULT_MOMENT_ID = 15;
4040
private static final long DEFAULT_USER_ID = 38710;
41-
42-
41+
42+
4343

4444
public static JSONObject newPostRequest(boolean encode) {
4545
Moment data = new Moment();
@@ -54,8 +54,14 @@ public static JSONObject newPostRequest(boolean encode) {
5454

5555
public static JSONObject newPutRequest(long id, boolean encode) {
5656
Moment data = new Moment(id <= 0 ? DEFAULT_MOMENT_ID : id);
57-
data.setContent(context.getString(R.string.apijson_info));
58-
return new JSONRequest(data, encode).setTag(Moment.class.getSimpleName());
57+
// data.setContent(context.getString(R.string.apijson_info));
58+
// List<Long> list = new ArrayList<>();
59+
// list.add((long) 10000);
60+
// list.add((long) 10001);
61+
JSONObject momentObject = new JSONObject(data, encode);
62+
// momentObject.put("praiseUserIdList+", list);//测试 +和- 通过
63+
momentObject.put("content", context.getString(R.string.apijson_info), encode);//测试 +和- 通过
64+
return new JSONRequest(Moment.class.getSimpleName(), momentObject, encode).setTag(Moment.class.getSimpleName());
5965
}
6066

6167
public static JSONObject newDeleteRequest(long id, boolean encode) {
@@ -70,7 +76,7 @@ public static JSONObject newSingleRequest(long id, boolean encode) {
7076
}
7177

7278
public static JSONObject newColumnsRequest(long id, boolean encode) {
73-
JSONObject object = new JSONObject(new Moment(id <= 0 ? DEFAULT_MOMENT_ID : id));
79+
JSONObject object = new JSONObject(new Moment(id <= 0 ? DEFAULT_MOMENT_ID : id), encode);
7480
object.setColumns("id,userId,content");
7581
return new JSONRequest(Moment.class.getSimpleName(), object, encode);
7682
}
@@ -86,7 +92,7 @@ public static JSONObject newArrayRequest(boolean encode) {
8692
JSONRequest dataObject = new JSONRequest();
8793
dataObject.put("name$", "%o%", encode);
8894
JSONRequest request = new JSONRequest(User.class.getSimpleName(), dataObject, encode);
89-
return request.toArray(5, 1, User.class.getSimpleName());
95+
return request.toArray(5, 1, User.class.getSimpleName(), encode);
9096
}
9197

9298
public static JSONObject newComplexRequest(boolean encode) {
@@ -100,9 +106,9 @@ public static JSONObject newComplexRequest(boolean encode) {
100106
request.put(User.class.getSimpleName(), new JSONRequest("id@", "/Moment/userId", encode), encode);
101107

102108
request.add(new JSONRequest(Comment.class.getSimpleName(), new JSONRequest("workId@", "[]/Moment/id", encode)).
103-
toArray(3, 0, Comment.class.getSimpleName()));
109+
toArray(3, 0, Comment.class.getSimpleName()), encode);
104110

105-
return request.toArray(3, 0);
111+
return request.toArray(3, 0, encode);
106112
}
107113

108114
public static JSONObject newAccessErrorRequest(boolean encode) {

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,47 @@ public static String format(JSONObject object) {
201201
return toJSONString(object, SerializerFeature.PrettyFormat);
202202
}
203203

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

205247
}

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

Lines changed: 126 additions & 12 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,33 +36,72 @@ 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) {
@@ -68,12 +111,83 @@ public JSONObject add(com.alibaba.fastjson.JSONObject object) {
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+
try {
125+
key = URLDecoder.decode((String) key, UTF_8);
126+
} catch (UnsupportedEncodingException e) {
127+
e.printStackTrace();
128+
return null;
129+
}
130+
}
131+
Object value = super.get(key);
132+
if (value instanceof String) {
133+
try {
134+
return URLDecoder.decode((String) value, UTF_8);
135+
} catch (UnsupportedEncodingException e) {
136+
e.printStackTrace();
137+
}
138+
}
139+
return null;
140+
}
141+
return super.get(key);
142+
}
143+
144+
/**
145+
* encode = false
146+
* @param value
147+
* @return {@link #put(String, boolean)}
148+
*/
149+
public Object put(Object value) {
150+
return put(value, false);
151+
}
152+
/**
153+
* @param value
154+
* @param encode
155+
* @return {@link #put(String, Object, boolean)}
156+
*/
157+
public Object put(Object value, boolean encode) {
158+
return put(null, value, encode);
159+
}
160+
/**
161+
* @param key StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName()
162+
* <br> >> if decode && key instanceof String, key = URLDecoder.decode((String) key, UTF_8)
163+
* @param value URLEncoder.encode((String) value, UTF_8);
164+
* @param encode if value instanceof String, value = URLEncoder.encode((String) value, UTF_8);
165+
* @return
166+
*/
167+
public Object put(String key, Object value, boolean encode) {
168+
key = StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName();
169+
if (encode) {
170+
try {
171+
key = URLEncoder.encode(key, UTF_8);
172+
} catch (UnsupportedEncodingException e) {
173+
e.printStackTrace();
174+
}
175+
if (value instanceof String) {
176+
try {
177+
value = URLEncoder.encode((String) value, UTF_8);
178+
} catch (UnsupportedEncodingException e) {
179+
e.printStackTrace();
180+
}
181+
}
182+
}
183+
return super.put(key, value);
184+
}
185+
72186

73187

74188
//judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
75189
public static final String KEY_ARRAY = "[]";
76-
190+
77191
/**判断是否为Array的key
78192
* @param key
79193
* @return
@@ -96,9 +210,9 @@ public static boolean isWord(String key) {
96210
return StringUtil.isNotEmpty(key, false) && namePattern.matcher(key).matches();
97211
}
98212
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
99-
100-
101-
213+
214+
215+
102216
public static final String KEY_COLUMNS = "@columns";//@key关键字都放这个类
103217

104218
/**set columns need to be returned
@@ -112,6 +226,6 @@ public JSONObject setColumns(String columns) {
112226
public String getColumns() {
113227
return getString(KEY_COLUMNS);
114228
}
115-
116-
229+
230+
117231
}

0 commit comments

Comments
 (0)