Skip to content

Commit 31b58e6

Browse files
committed
PUT 新增支持对 JSONObject {} 格式的字段值传 "key+": [{"key":value}} 新增,传 "key-": ["key"] 移除
1 parent 3a45780 commit 31b58e6

File tree

1 file changed

+69
-20
lines changed

1 file changed

+69
-20
lines changed

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

+69-20
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@
1818
import com.alibaba.fastjson.JSON;
1919
import com.alibaba.fastjson.JSONArray;
2020
import com.alibaba.fastjson.JSONObject;
21+
import com.alibaba.fastjson.serializer.SerializerFeature;
2122

2223
import java.rmi.ServerException;
23-
import java.util.ArrayList;
24-
import java.util.Arrays;
25-
import java.util.LinkedHashMap;
26-
import java.util.LinkedHashSet;
27-
import java.util.List;
28-
import java.util.Map;
24+
import java.util.*;
2925
import java.util.Map.Entry;
30-
import java.util.Set;
3126

3227
import static apijson.JSONObject.KEY_COMBINE;
3328
import static apijson.JSONObject.KEY_DROP;
@@ -559,8 +554,8 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
559554
}
560555

561556

557+
//TODO 改用 MySQL json_add,json_remove,json_contains 等函数!不过就没有具体报错了,或许可以新增功能符,或者直接调 SQL 函数
562558

563-
//TODO 改用 MySQL json_add,json_remove,json_contains 等函数!
564559
/**PUT key:[]
565560
* @param key
566561
* @param array
@@ -596,31 +591,85 @@ public void onPUTArrayParse(@NotNull String key, @NotNull JSONArray array) throw
596591

597592

598593
//add all 或 remove all <<<<<<<<<<<<<<<<<<<<<<<<<
599-
JSONArray targetArray = rp == null ? null : rp.getJSONArray(realKey);
600-
if (targetArray == null) {
594+
Object target = rp == null ? null : rp.get(realKey);
595+
if (target instanceof String) {
596+
try {
597+
target = JSON.parse((String) target);
598+
} catch (Throwable e) {
599+
if (Log.DEBUG) {
600+
Log.e(TAG, "try {\n" +
601+
"\t\t\t\ttarget = JSON.parse((String) target);\n" +
602+
"\t\t\t}\n" +
603+
"\t\t\tcatch (Throwable e) = " + e.getMessage());
604+
}
605+
}
606+
}
607+
608+
if (apijson.JSON.isBooleanOrNumberOrString(target)) {
609+
throw new NullPointerException("PUT " + path + ", " + realKey + " 类型为 " + target.getClass().getSimpleName() + ","
610+
+ "不支持 Boolean, String, Number 等类型字段使用 'key+': [] 或 'key-': [] !"
611+
+ "对应字段在数据库的值必须为 JSONArray, JSONObject 中的一种!"
612+
+ "值为 JSONObject 类型时传参必须是 'key+': [{'key': value, 'key2': value2}] 或 'key-': ['key', 'key2'] !"
613+
);
614+
}
615+
616+
boolean isAdd = putType == 1;
617+
618+
Collection<Object> targetArray = target instanceof Collection ? (Collection<Object>) target : null;
619+
Map<String, ?> targetObj = target instanceof Map ? (Map<String, Object>) target : null;
620+
621+
if (targetArray == null && targetObj == null) {
622+
if (isAdd == false) {
623+
throw new NullPointerException("PUT " + path + ", " + realKey + (target == null ? " 值为 null,不支持移除!"
624+
: " 类型为 " + target.getClass().getSimpleName() + ",不支持这样移除!")
625+
+ "对应字段在数据库的值必须为 JSONArray, JSONObject 中的一种,且 key- 移除时,本身的值不能为 null!"
626+
+ "值为 JSONObject 类型时传参必须是 'key+': [{'key': value, 'key2': value2}] 或 'key-': ['key', 'key2'] !"
627+
);
628+
}
629+
601630
targetArray = new JSONArray();
602631
}
603-
for (Object obj : array) {
632+
633+
for (int i = 0; i < array.size(); i++) {
634+
Object obj = array.get(i);
604635
if (obj == null) {
605636
continue;
606637
}
607-
if (putType == 1) {
608-
if (targetArray.contains(obj)) {
609-
throw new ConflictException("PUT " + path + ", " + realKey + ":" + obj + " 已存在!");
638+
639+
if (isAdd) {
640+
if (targetArray != null) {
641+
if (targetArray.contains(obj)) {
642+
throw new ConflictException("PUT " + path + ", " + key + "/" + i + " 已存在!");
643+
}
644+
targetArray.add(obj);
645+
} else {
646+
if (obj != null && obj instanceof Map == false) {
647+
throw new ConflictException("PUT " + path + ", " + key + "/" + i + " 必须为 JSONObject {} !");
648+
}
649+
targetObj.putAll((Map) obj);
610650
}
611-
targetArray.add(obj);
612-
} else if (putType == 2) {
613-
if (targetArray.contains(obj) == false) {
614-
throw new NullPointerException("PUT " + path + ", " + realKey + ":" + obj + " 不存在!");
651+
} else {
652+
if (targetArray != null) {
653+
if (targetArray.contains(obj) == false) {
654+
throw new NullPointerException("PUT " + path + ", " + key + "/" + i + " 不存在!");
655+
}
656+
targetArray.remove(obj);
657+
} else {
658+
if (obj instanceof String == false) {
659+
throw new ConflictException("PUT " + path + ", " + key + "/" + i + " 必须为 String 类型 !");
660+
}
661+
if (targetObj.containsKey(obj) == false) {
662+
throw new NullPointerException("PUT " + path + ", " + key + "/" + i + " 不存在!");
663+
}
664+
targetObj.remove(obj);
615665
}
616-
targetArray.remove(obj);
617666
}
618667
}
619668

620669
//add all 或 remove all >>>>>>>>>>>>>>>>>>>>>>>>>
621670

622671
//PUT <<<<<<<<<<<<<<<<<<<<<<<<<
623-
sqlRequest.put(realKey, targetArray);
672+
sqlRequest.put(realKey, targetArray != null ? targetArray : JSON.toJSONString(targetObj, SerializerFeature.WriteMapNullValue));
624673
//PUT >>>>>>>>>>>>>>>>>>>>>>>>>
625674

626675
}

0 commit comments

Comments
 (0)