Skip to content

Commit b90ac05

Browse files
committed
Server:与客户端同步JSON, JSONObject,JSONRequest,JSONResponse;Client:优化JSONObject,解决get返回null和多层encode问题;fastjson版本升级到1.2.24;
1 parent 86097f3 commit b90ac05

File tree

9 files changed

+82
-55
lines changed

9 files changed

+82
-55
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public static JSONObject newPostRequest(boolean encode) {
5555
public static JSONObject newPutRequest(long id, boolean encode) {
5656
Moment data = new Moment(id <= 0 ? DEFAULT_MOMENT_ID : id);
5757
// data.setContent(context.getString(R.string.apijson_info));
58-
// List<Long> list = new ArrayList<>();
59-
// list.add((long) 10000);
60-
// list.add((long) 10001);
58+
List<Long> list = new ArrayList<>();
59+
list.add((long) 10000);
60+
list.add((long) 10001);
6161
JSONObject momentObject = new JSONObject(data, encode);
62-
// momentObject.put("praiseUserIdList+", list);//测试 +和- 通过
62+
momentObject.put("praiseUserIdList+", list, encode);//测试 +和- 通过
6363
momentObject.put("content", context.getString(R.string.apijson_info), encode);//测试 +和- 通过
6464
return new JSONRequest(Moment.class.getSimpleName(), momentObject, encode).setTag(Moment.class.getSimpleName());
6565
}
@@ -105,7 +105,8 @@ public static JSONObject newComplexRequest(boolean encode) {
105105

106106
request.put(User.class.getSimpleName(), new JSONRequest("id@", "/Moment/userId", encode), encode);
107107

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

111112
return request.toArray(3, 0, encode);
Binary file not shown.
Binary file not shown.

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

Lines changed: 8 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
@@ -244,4 +251,5 @@ public static boolean isJSONArray(Object obj) {
244251
return false;
245252
}
246253

254+
247255
}

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public JSONObject add(com.alibaba.fastjson.JSONObject object, boolean encode) {
105105
Set<String> set = object == null ? null : object.keySet();
106106
if (set != null) {
107107
for (String key : set) {
108-
put(key, object.get(key));
108+
put(key, object.get(key), encode);
109109
}
110110
}
111111
return this;
@@ -121,22 +121,24 @@ public JSONObject add(com.alibaba.fastjson.JSONObject object, boolean encode) {
121121
public Object get(Object key, boolean decode) {
122122
if (decode) {
123123
if (key instanceof String) {
124-
try {
125-
key = URLDecoder.decode((String) key, UTF_8);
126-
} catch (UnsupportedEncodingException e) {
127-
e.printStackTrace();
128-
return null;
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+
}
129131
}
130132
}
131133
Object value = super.get(key);
132134
if (value instanceof String) {
133135
try {
134-
return URLDecoder.decode((String) value, UTF_8);
136+
value = URLDecoder.decode((String) value, UTF_8);
135137
} catch (UnsupportedEncodingException e) {
136138
e.printStackTrace();
137139
}
138140
}
139-
return null;
141+
return value;
140142
}
141143
return super.get(key);
142144
}
@@ -150,6 +152,7 @@ public Object put(Object value) {
150152
return put(value, false);
151153
}
152154
/**
155+
* key = value.getClass().getSimpleName()
153156
* @param value
154157
* @param encode
155158
* @return {@link #put(String, Object, boolean)}
@@ -158,21 +161,26 @@ public Object put(Object value, boolean encode) {
158161
return put(null, value, encode);
159162
}
160163
/**
161-
* @param key StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName()
164+
* @param key if StringUtil.isNotEmpty(key, true) == false,
165+
* <br> key = value == null ? null : value.getClass().getSimpleName();
162166
* <br> >> if decode && key instanceof String, key = URLDecoder.decode((String) key, UTF_8)
163167
* @param value URLEncoder.encode((String) value, UTF_8);
164168
* @param encode if value instanceof String, value = URLEncoder.encode((String) value, UTF_8);
165169
* @return
166170
*/
167171
public Object put(String key, Object value, boolean encode) {
168-
key = StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName();
172+
if (StringUtil.isNotEmpty(key, true) == false) {
173+
key = value == null ? null : value.getClass().getSimpleName();
174+
}
169175
if (encode) {
170-
try {
171-
key = URLEncoder.encode(key, UTF_8);
172-
} catch (UnsupportedEncodingException e) {
173-
e.printStackTrace();
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+
}
174182
}
175-
if (value instanceof String) {
183+
if (value instanceof String) {//只在value instanceof String时encode key?{@link #get(Object, boolean)}内做不到
176184
try {
177185
value = URLEncoder.encode((String) value, UTF_8);
178186
} catch (UnsupportedEncodingException e) {

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,16 @@ public Object putPath(String key, String... parts) {
120120
return put(key, path);
121121
}
122122

123-
/**
124-
* decode = true
125-
* @param key
126-
* return {@link #get(Object, boolean)}
127-
*/
128-
@Override
129-
public Object get(Object key) {
130-
return get(key, true);
131-
}
123+
// 导致JSONObject add >> get = null
124+
// /**
125+
// * decode = true
126+
// * @param key
127+
// * return {@link #get(Object, boolean)}
128+
// */
129+
// @Override
130+
// public Object get(Object key) {
131+
// return get(key, true);
132+
// }
132133

133134
/**
134135
* encode = true

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public JSONObject add(com.alibaba.fastjson.JSONObject object, boolean encode) {
105105
Set<String> set = object == null ? null : object.keySet();
106106
if (set != null) {
107107
for (String key : set) {
108-
put(key, object.get(key));
108+
put(key, object.get(key), encode);
109109
}
110110
}
111111
return this;
@@ -121,22 +121,24 @@ public JSONObject add(com.alibaba.fastjson.JSONObject object, boolean encode) {
121121
public Object get(Object key, boolean decode) {
122122
if (decode) {
123123
if (key instanceof String) {
124-
try {
125-
key = URLDecoder.decode((String) key, UTF_8);
126-
} catch (UnsupportedEncodingException e) {
127-
e.printStackTrace();
128-
return null;
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+
}
129131
}
130132
}
131133
Object value = super.get(key);
132134
if (value instanceof String) {
133135
try {
134-
return URLDecoder.decode((String) value, UTF_8);
136+
value = URLDecoder.decode((String) value, UTF_8);
135137
} catch (UnsupportedEncodingException e) {
136138
e.printStackTrace();
137139
}
138140
}
139-
return null;
141+
return value;
140142
}
141143
return super.get(key);
142144
}
@@ -150,6 +152,7 @@ public Object put(Object value) {
150152
return put(value, false);
151153
}
152154
/**
155+
* key = value.getClass().getSimpleName()
153156
* @param value
154157
* @param encode
155158
* @return {@link #put(String, Object, boolean)}
@@ -158,21 +161,26 @@ public Object put(Object value, boolean encode) {
158161
return put(null, value, encode);
159162
}
160163
/**
161-
* @param key StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName()
164+
* @param key if StringUtil.isNotEmpty(key, true) == false,
165+
* <br> key = value == null ? null : value.getClass().getSimpleName();
162166
* <br> >> if decode && key instanceof String, key = URLDecoder.decode((String) key, UTF_8)
163167
* @param value URLEncoder.encode((String) value, UTF_8);
164168
* @param encode if value instanceof String, value = URLEncoder.encode((String) value, UTF_8);
165169
* @return
166170
*/
167171
public Object put(String key, Object value, boolean encode) {
168-
key = StringUtil.isNotEmpty(key, true) ? key : value.getClass().getSimpleName();
172+
if (StringUtil.isNotEmpty(key, true) == false) {
173+
key = value == null ? null : value.getClass().getSimpleName();
174+
}
169175
if (encode) {
170-
try {
171-
key = URLEncoder.encode(key, UTF_8);
172-
} catch (UnsupportedEncodingException e) {
173-
e.printStackTrace();
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+
}
174182
}
175-
if (value instanceof String) {
183+
if (value instanceof String) {//只在value instanceof String时encode key?{@link #get(Object, boolean)}内做不到
176184
try {
177185
value = URLEncoder.encode((String) value, UTF_8);
178186
} catch (UnsupportedEncodingException e) {

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/JSONRequest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,16 @@ public Object putPath(String key, String... parts) {
120120
return put(key, path);
121121
}
122122

123-
/**
124-
* decode = true
125-
* @param key
126-
* return {@link #get(Object, boolean)}
127-
*/
128-
@Override
129-
public Object get(Object key) {
130-
return get(key, true);
131-
}
123+
// 导致JSONObject add >> get = null
124+
// /**
125+
// * decode = true
126+
// * @param key
127+
// * return {@link #get(Object, boolean)}
128+
// */
129+
// @Override
130+
// public Object get(Object key) {
131+
// return get(key, true);
132+
// }
132133

133134
/**
134135
* encode = true

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/package-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
limitations under the License.*/
1414

1515
/**
16-
* common files package between Server and Client projects
16+
* the same files for server and client projects
1717
*/
1818
/**
1919
* @author Lemon
20-
*
20+
*
2121
*/
2222
package zuo.biao.apijson;

0 commit comments

Comments
 (0)