Visitar URL original
去除 fastjson 并支持各种 JSON 库 by TommyLemon · Pull Request #810 · APIJSON/APIJSON · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
解决未返回列表、对象内返回 @RAW@LIST、JSON 键值对未保持写入顺序等
  • Loading branch information
TommyLemon committed Mar 30, 2025
commit f11c3a18ea86cc9ccd620a9f0d40d6285b76b45d
12 changes: 8 additions & 4 deletions APIJSONORM/src/main/java/apijson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import apijson.orm.exception.UnsupportedDataTypeException;
import apijson.Log;
import apijson.StringUtil;

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

public static Object createJSONObject(Map<? extends String, ?> map) {
return DEFAULT_JSON_PARSER.createJSONObject(map);
}

public static Object createJSONArray() {
return DEFAULT_JSON_PARSER.createJSONArray();
}

public static Object createJSONArray(List<?> list) {
return DEFAULT_JSON_PARSER.createJSONArray(list);
}

public static Object parseJSON(Object json) {
if (json instanceof Boolean || json instanceof Number || json instanceof Enum<?>) {
return json;
Expand Down
17 changes: 8 additions & 9 deletions APIJSONORM/src/main/java/apijson/JSONCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@ public interface JSONCreator<M extends Map<String, Object>, L extends List<Objec
@NotNull
M createJSONObject();

// @NotNull
// M createJSONObject(String json);

@NotNull
default M createJSONObject(Map<String, Object> m) {
default M createJSONObject(Map<? extends String, ?> m) {
M obj = createJSONObject();
obj.putAll(m);
if (m != null && ! m.isEmpty()) {
obj.putAll(m);
}
return obj;
}

@NotNull
L createJSONArray();
// @NotNull
// L createJSONArray(String json);

@NotNull
default L createJSONArray(List<Object> l){
default L createJSONArray(List<?> l){
L arr = createJSONArray();
arr.addAll(l);
if (l != null && ! l.isEmpty()) {
arr.addAll(l);
}
return arr;
}
}
9 changes: 5 additions & 4 deletions APIJSONORM/src/main/java/apijson/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,6 @@ public JSONObject putsAll(Map<? extends String, ? extends Object> map) {
return this;
}

@Override
public Object remove(Object key) {
return null;
}

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

@Override
public Object remove(Object key) {
return map.remove(key);
}

@Override
public void clear() {
map.clear();
Expand Down
20 changes: 10 additions & 10 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public M getCacheItem(List<M> list, int position, SQLConfig<T, M, L> config) {
}

M result = position >= list.size() ? null : list.get(position);
return result != null ? result : createJSONObject();
return result != null ? result : (M) JSON.createJSONObject();
}


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

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

M item = createJSONObject();
M item = (M) JSON.createJSONObject();
M viceItem = null;
M curItem = item;
boolean isMain = true;
Expand Down Expand Up @@ -629,7 +629,7 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) {
else {
String viceName = viceConfig.getTableKey();
if (viceItem == null) {
viceItem = createJSONObject();
viceItem = (M) JSON.createJSONObject();
}
curItem = JSON.get(viceItem, viceName);

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

if (curItem == null || curItem.isEmpty()) {
// 导致前面判断重复 key 出错 curItem = curCache != null ? curCache : new M(true);
curItem = createJSONObject();
curItem = (M) JSON.createJSONObject();
viceItem.put(viceName, curItem);
if (hasPK && curCache == null) {
childMap.put(viceSql, curItem);
Expand Down Expand Up @@ -688,7 +688,7 @@ else if (hasPK) {
if (unknownType || isExplain) {
if (isExplain) {
if (result == null) {
result = createJSONObject();
result = (M) JSON.createJSONObject();
}
config.setExplain(false);
result.put("sql", config.getSQL(false));
Expand Down Expand Up @@ -723,13 +723,13 @@ else if (hasPK) {

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

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

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

M result = createJSONObject();
M result = (M) JSON.createJSONObject();

for (int i = 1; i <= length; i++) {
result = onPutColumn(jc, rs, rsmd, index, result, i, null, null, keyMap);
Expand Down Expand Up @@ -1135,7 +1135,7 @@ else if (value instanceof Clob) { //SQL Server TEXT 类型 居然走这个
}
if (castToJson) {
try {
value = parseJSON((String) value);
value = JSON.parseJSON(value);
} catch (Exception e) {
Log.e(TAG, "getValue try { value = parseJSON((String) value); } catch (Exception e) { \n" + e.getMessage());
}
Expand Down
61 changes: 31 additions & 30 deletions APIJSONORM/src/main/java/apijson/orm/SQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,36 @@ default Statement getStatement(@NotNull SQLConfig<T, M, L> config) throws Except

long getSqlResultDuration();

default M createJSONObject() {
return (M) new JSONObject();
}

default L createJSONArray() {
return (L) new JSONArray();
}

default String toJSONString(Object obj) {
return JSON.toJSONString(obj);
}

default Object parseJSON(Object json) {
return JSON.parseJSON(json);
}
// default M createJSONObject() {
// return (M) new JSONObject();
// }
//
// default L createJSONArray() {
// return (L) new JSONArray();
// }
//
// default String toJSONString(Object obj) {
// return JSON.toJSONString(obj);
// }
//
// default Object parseJSON(Object json) {
// return JSON.parseJSON(json);
// }
//
// default M parseObject(Object json) {
// return (M) parseObject(json, JSONObject.class);
// }
//
// default <T> T parseObject(Object json, Class<T> clazz) {
// return JSON.parseObject(json, clazz);
// }
//
// default L parseArray(Object json) {
// return (L) parseObject(json, JSONArray.class);
// }
//
// default <T> List<T> parseArray(Object json, Class<T> clazz) {
// return JSON.parseArray(json, clazz);
// }

default M parseObject(Object json) {
return (M) parseObject(json, JSONObject.class);
}

default <T> T parseObject(Object json, Class<T> clazz) {
return JSON.parseObject(json, clazz);
}

default L parseArray(Object json) {
return (L) parseObject(json, JSONArray.class);
}

default <T> List<T> parseArray(Object json, Class<T> clazz) {
return JSON.parseArray(json, clazz);
}
}