Skip to content

Commit f11c3a1

Browse files
committed
解决未返回列表、对象内返回 @RAW@LIST、JSON 键值对未保持写入顺序等
1 parent ae023cb commit f11c3a1

File tree

5 files changed

+62
-57
lines changed

5 files changed

+62
-57
lines changed

APIJSONORM/src/main/java/apijson/JSON.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66

77
import java.util.List;
88
import java.util.Map;
9-
import java.util.ArrayList;
10-
import java.util.LinkedHashMap;
119

1210
import apijson.orm.exception.UnsupportedDataTypeException;
13-
import apijson.Log;
14-
import apijson.StringUtil;
1511

1612
/**JSON工具类 防止解析时异常
1713
* @author Lemon
@@ -69,10 +65,18 @@ public static Object createJSONObject() {
6965
return DEFAULT_JSON_PARSER.createJSONObject();
7066
}
7167

68+
public static Object createJSONObject(Map<? extends String, ?> map) {
69+
return DEFAULT_JSON_PARSER.createJSONObject(map);
70+
}
71+
7272
public static Object createJSONArray() {
7373
return DEFAULT_JSON_PARSER.createJSONArray();
7474
}
7575

76+
public static Object createJSONArray(List<?> list) {
77+
return DEFAULT_JSON_PARSER.createJSONArray(list);
78+
}
79+
7680
public static Object parseJSON(Object json) {
7781
if (json instanceof Boolean || json instanceof Number || json instanceof Enum<?>) {
7882
return json;

APIJSONORM/src/main/java/apijson/JSONCreator.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,24 @@ public interface JSONCreator<M extends Map<String, Object>, L extends List<Objec
1919
@NotNull
2020
M createJSONObject();
2121

22-
// @NotNull
23-
// M createJSONObject(String json);
24-
2522
@NotNull
26-
default M createJSONObject(Map<String, Object> m) {
23+
default M createJSONObject(Map<? extends String, ?> m) {
2724
M obj = createJSONObject();
28-
obj.putAll(m);
25+
if (m != null && ! m.isEmpty()) {
26+
obj.putAll(m);
27+
}
2928
return obj;
3029
}
3130

3231
@NotNull
3332
L createJSONArray();
34-
// @NotNull
35-
// L createJSONArray(String json);
3633

3734
@NotNull
38-
default L createJSONArray(List<Object> l){
35+
default L createJSONArray(List<?> l){
3936
L arr = createJSONArray();
40-
arr.addAll(l);
37+
if (l != null && ! l.isEmpty()) {
38+
arr.addAll(l);
39+
}
4140
return arr;
4241
}
4342
}

APIJSONORM/src/main/java/apijson/JSONObject.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,6 @@ public JSONObject putsAll(Map<? extends String, ? extends Object> map) {
690690
return this;
691691
}
692692

693-
@Override
694-
public Object remove(Object key) {
695-
return null;
696-
}
697693

698694
/**
699695
* Get a boolean value from the JSONObject
@@ -825,6 +821,11 @@ public void putAll(Map<? extends String, ? extends Object> map) {
825821
}
826822
}
827823

824+
@Override
825+
public Object remove(Object key) {
826+
return map.remove(key);
827+
}
828+
828829
@Override
829830
public void clear() {
830831
map.clear();

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public M getCacheItem(List<M> list, int position, SQLConfig<T, M, L> config) {
122122
}
123123

124124
M result = position >= list.size() ? null : list.get(position);
125-
return result != null ? result : createJSONObject();
125+
return result != null ? result : (M) JSON.createJSONObject();
126126
}
127127

128128

@@ -212,7 +212,7 @@ public M execute(@NotNull SQLConfig<T, M, L> config, boolean unknownType) throws
212212
executedSQLDuration += System.currentTimeMillis() - executedSQLStartTime;
213213
}
214214

215-
result = createJSONObject();
215+
result = (M) JSON.createJSONObject();
216216
result.put(JSONResponse.KEY_COUNT, updateCount);
217217
result.put("update", updateCount >= 0);
218218
//导致后面 rs.getMetaData() 报错 Operation not allowed after ResultSet closed result.put("moreResults", statement.getMoreResults());
@@ -408,7 +408,7 @@ public M execute(@NotNull SQLConfig<T, M, L> config, boolean unknownType) throws
408408
index ++;
409409
Log.d(TAG, "\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n execute while (rs.next()){ index = " + index + "\n\n");
410410

411-
M item = createJSONObject();
411+
M item = (M) JSON.createJSONObject();
412412
M viceItem = null;
413413
M curItem = item;
414414
boolean isMain = true;
@@ -629,7 +629,7 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {
629629
else {
630630
String viceName = viceConfig.getTableKey();
631631
if (viceItem == null) {
632-
viceItem = createJSONObject();
632+
viceItem = (M) JSON.createJSONObject();
633633
}
634634
curItem = JSON.get(viceItem, viceName);
635635

@@ -638,7 +638,7 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {
638638

639639
if (curItem == null || curItem.isEmpty()) {
640640
// 导致前面判断重复 key 出错 curItem = curCache != null ? curCache : new M(true);
641-
curItem = createJSONObject();
641+
curItem = (M) JSON.createJSONObject();
642642
viceItem.put(viceName, curItem);
643643
if (hasPK && curCache == null) {
644644
childMap.put(viceSql, curItem);
@@ -688,7 +688,7 @@ else if (hasPK) {
688688
if (unknownType || isExplain) {
689689
if (isExplain) {
690690
if (result == null) {
691-
result = createJSONObject();
691+
result = (M) JSON.createJSONObject();
692692
}
693693
config.setExplain(false);
694694
result.put("sql", config.getSQL(false));
@@ -723,13 +723,13 @@ else if (hasPK) {
723723

724724
// 数组主表对象额外一次返回全部,方便 Parser<T, M, L> 缓存来提高性能
725725

726-
result = position >= resultList.size() ? createJSONObject() : resultList.get(position);
726+
result = position >= resultList.size() ? (M) JSON.createJSONObject() : resultList.get(position);
727727
if (position == 0 && resultList.size() > 1 && result != null && result.isEmpty() == false) {
728728
// 不是 main 不会直接执行,count=1 返回的不会超过 1 && config.isMain() && config.getCount() != 1
729729
Log.i(TAG, ">>> execute position == 0 && resultList.size() > 1 && result != null && result.isEmpty() == false"
730730
+ " >> result = new M(result); result.put(KEY_RAW_LIST, resultList);");
731731

732-
result = createJSONObject();
732+
result = (M) JSON.createJSONObject(result);
733733
result.put(KEY_RAW_LIST, resultList);
734734
}
735735
}
@@ -909,7 +909,7 @@ protected void executeAppJoin(SQLConfig<T, M, L> config, List<M> resultList, Map
909909
index ++;
910910
Log.d(TAG, "\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n executeAppJoin while (rs.next()){ index = " + index + "\n\n");
911911

912-
M result = createJSONObject();
912+
M result = (M) JSON.createJSONObject();
913913

914914
for (int i = 1; i <= length; i++) {
915915
result = onPutColumn(jc, rs, rsmd, index, result, i, null, null, keyMap);
@@ -1135,7 +1135,7 @@ else if (value instanceof Clob) { //SQL Server TEXT 类型 居然走这个
11351135
}
11361136
if (castToJson) {
11371137
try {
1138-
value = parseJSON((String) value);
1138+
value = JSON.parseJSON(value);
11391139
} catch (Exception e) {
11401140
Log.e(TAG, "getValue try { value = parseJSON((String) value); } catch (Exception e) { \n" + e.getMessage());
11411141
}

APIJSONORM/src/main/java/apijson/orm/SQLExecutor.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,35 +135,36 @@ default Statement getStatement(@NotNull SQLConfig<T, M, L> config) throws Except
135135

136136
long getSqlResultDuration();
137137

138-
default M createJSONObject() {
139-
return (M) new JSONObject();
140-
}
141-
142-
default L createJSONArray() {
143-
return (L) new JSONArray();
144-
}
145-
146-
default String toJSONString(Object obj) {
147-
return JSON.toJSONString(obj);
148-
}
149-
150-
default Object parseJSON(Object json) {
151-
return JSON.parseJSON(json);
152-
}
138+
// default M createJSONObject() {
139+
// return (M) new JSONObject();
140+
// }
141+
//
142+
// default L createJSONArray() {
143+
// return (L) new JSONArray();
144+
// }
145+
//
146+
// default String toJSONString(Object obj) {
147+
// return JSON.toJSONString(obj);
148+
// }
149+
//
150+
// default Object parseJSON(Object json) {
151+
// return JSON.parseJSON(json);
152+
// }
153+
//
154+
// default M parseObject(Object json) {
155+
// return (M) parseObject(json, JSONObject.class);
156+
// }
157+
//
158+
// default <T> T parseObject(Object json, Class<T> clazz) {
159+
// return JSON.parseObject(json, clazz);
160+
// }
161+
//
162+
// default L parseArray(Object json) {
163+
// return (L) parseObject(json, JSONArray.class);
164+
// }
165+
//
166+
// default <T> List<T> parseArray(Object json, Class<T> clazz) {
167+
// return JSON.parseArray(json, clazz);
168+
// }
153169

154-
default M parseObject(Object json) {
155-
return (M) parseObject(json, JSONObject.class);
156-
}
157-
158-
default <T> T parseObject(Object json, Class<T> clazz) {
159-
return JSON.parseObject(json, clazz);
160-
}
161-
162-
default L parseArray(Object json) {
163-
return (L) parseObject(json, JSONArray.class);
164-
}
165-
166-
default <T> List<T> parseArray(Object json, Class<T> clazz) {
167-
return JSON.parseArray(json, clazz);
168-
}
169170
}

0 commit comments

Comments
 (0)