Skip to content

Commit 5ea9139

Browse files
committed
引用赋值:新增支持 User-id[]/2 从数组 User-id[] 中 get(2)
1 parent 5843dcf commit 5ea9139

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,4 +674,18 @@ public static String getString(Map<String, Object> map, String key) {
674674
return value.toString();
675675
}
676676

677+
678+
public static Object getFromObjOrArr(Object parent, String k) {
679+
if (parent instanceof Map<?, ?>) {
680+
return ((Map<String, Object>) parent).get(k);
681+
}
682+
683+
if (parent instanceof List<?>) {
684+
int j = Integer.valueOf(k);
685+
return ((List<?>) parent).get(j);
686+
}
687+
688+
return null;
689+
}
690+
677691
}

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

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
public abstract class AbstractParser<T, M extends Map<String, Object>, L extends List<Object>>
4040
implements Parser<T, M, L> {
4141
protected static final String TAG = "AbstractParser";
42-
42+
4343
/**
4444
* JSON 对象、数组对应的数据源、版本、角色、method等
4545
*/
@@ -706,7 +706,7 @@ public M parseCorrectRequest(RequestMethod method, String tag, int version, Stri
706706

707707
return batchVerify(method, tag, version, name, request, maxUpdateCount, creator);
708708
}
709-
709+
710710
/**自动根据 tag 是否为 TableKey 及是否被包含在 object 内来决定是否包装一层,改为 { tag: object, "tag": tag }
711711
* @param object
712712
* @param tag
@@ -1902,24 +1902,32 @@ else if (join != null){
19021902
* @param pathKeys
19031903
* @return
19041904
*/
1905-
public static <V extends Object> V getValue(Map<String, Object> parent, String[] pathKeys) {
1906-
if (parent == null || pathKeys == null || pathKeys.length <= 0) {
1905+
public static <V extends Object> V getValue(Object parent, String[] pathKeys) {
1906+
int len = parent == null || pathKeys == null ? 0 : pathKeys.length;
1907+
if (len <= 0) {
19071908
Log.w(TAG, "getChild parent == null || pathKeys == null || pathKeys.length <= 0 >> return parent;");
19081909
return (V) parent;
19091910
}
19101911

1911-
//逐层到达child的直接容器JSONObject parent
1912-
int last = pathKeys.length - 1;
1913-
for (int i = 0; i < last; i++) {//一步一步到达指定位置
1914-
if (parent == null) {//不存在或路径错误(中间的key对应value不是JSONObject)
1912+
// 逐层到达child的直接容器JSONObject parent
1913+
Object v = parent;
1914+
for (int i = 0; i < len; i++) { // 一步一步到达指定位置
1915+
if (v == null) { // 不存在或路径错误(中间的key对应value不是JSONObject)
19151916
break;
19161917
}
19171918

19181919
String k = getDecodedKey(pathKeys[i]);
1919-
parent = JSON.get(parent, k);
1920+
try {
1921+
v = getFromObjOrArr(v, k);
1922+
} catch (Throwable e) {
1923+
if (IS_PRINT_BIG_LOG) {
1924+
e.printStackTrace();
1925+
}
1926+
v = null;
1927+
}
19201928
}
19211929

1922-
return parent == null ? null : (V) parent.get(getDecodedKey(pathKeys[last]));
1930+
return (V) v;
19231931
}
19241932

19251933

@@ -2025,13 +2033,13 @@ public Object getValueByPath(String valuePath) {
20252033
}
20262034

20272035
//取出key被valuePath包含的result,再从里面获取key对应的value
2028-
Map<String, Object> parent = null;
2036+
Object parent = null;
20292037
String[] keys = null;
20302038
for (Entry<String, Object> entry : queryResultMap.entrySet()){
20312039
String path = entry.getKey();
20322040
if (valuePath.startsWith(path + "/")) {
20332041
try {
2034-
parent = (M) entry.getValue();
2042+
parent = entry.getValue();
20352043
} catch (Exception e) {
20362044
Log.e(TAG, "getValueByPath try { parent = (Map<String>) queryResultMap.get(path); } catch { "
20372045
+ "\n parent not instanceof Map<String>!");
@@ -2044,39 +2052,13 @@ public Object getValueByPath(String valuePath) {
20442052
}
20452053
}
20462054

2047-
//逐层到达targetKey的直接容器JSONObject parent
2048-
int last = keys == null ? -1 : keys.length - 1;
2049-
if (last >= 1) {
2050-
for (int i = 0; i < last; i++) {//一步一步到达指定位置parentPath
2051-
if (parent == null) {//不存在或路径错误(中间的key对应value不是JSONObject)
2052-
break;
2053-
}
2054-
2055-
String k = getDecodedKey(keys[i]);
2056-
Object p = parent.get(k);
2057-
parent = p instanceof Map<?, ?> ? (Map<String, Object>) p : null;
2058-
}
2059-
}
2060-
2061-
if (parent != null) {
2062-
Log.i(TAG, "getValueByPath >> get from queryResultMap >> return parent.get(keys[keys.length - 1]);");
2063-
target = last < 0 ? parent : parent.get(getDecodedKey(keys[last])); //值为null应该报错NotExistExeption,一般都是id关联,不可为null,否则可能绕过安全机制
2064-
if (target != null) {
2065-
Log.i(TAG, "getValueByPath >> getValue >> return target = " + target);
2066-
return target;
2067-
}
2055+
target = getValue(parent, keys); // 逐层到达targetKey的直接容器JSONObject parent
2056+
if (target == null) { //从requestObject中取值
2057+
target = getValue(requestObject, StringUtil.splitPath(valuePath));
20682058
}
20692059

2070-
2071-
//从requestObject中取值
2072-
target = getValue(requestObject, StringUtil.splitPath(valuePath));
2073-
if (target != null) {
2074-
Log.i(TAG, "getValueByPath >> getValue >> return target = " + target);
2075-
return target;
2076-
}
2077-
2078-
Log.i(TAG, "getValueByPath return null;");
2079-
return null;
2060+
Log.i(TAG, "getValueByPath >> getValue >> return target = " + target);
2061+
return target;
20802062
}
20812063

20822064
/**解码 引用赋值 路径中的 key,支持把 URL encode 后的值,转为 decode 后的原始值,例如 %2Fuser%2Flist -> /user/list ; %7B%7D -> []

0 commit comments

Comments
 (0)