|
18 | 18 | import com.alibaba.fastjson.JSON; |
19 | 19 | import com.alibaba.fastjson.JSONArray; |
20 | 20 | import com.alibaba.fastjson.JSONObject; |
| 21 | +import com.alibaba.fastjson.serializer.SerializerFeature; |
21 | 22 |
|
22 | 23 | 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.*; |
29 | 25 | import java.util.Map.Entry; |
30 | | -import java.util.Set; |
31 | 26 |
|
32 | 27 | import static apijson.JSONObject.KEY_COMBINE; |
33 | 28 | import static apijson.JSONObject.KEY_DROP; |
@@ -559,8 +554,8 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti |
559 | 554 | } |
560 | 555 |
|
561 | 556 |
|
| 557 | + //TODO 改用 MySQL json_add,json_remove,json_contains 等函数!不过就没有具体报错了,或许可以新增功能符,或者直接调 SQL 函数 |
562 | 558 |
|
563 | | - //TODO 改用 MySQL json_add,json_remove,json_contains 等函数! |
564 | 559 | /**PUT key:[] |
565 | 560 | * @param key |
566 | 561 | * @param array |
@@ -596,31 +591,85 @@ public void onPUTArrayParse(@NotNull String key, @NotNull JSONArray array) throw |
596 | 591 |
|
597 | 592 |
|
598 | 593 | //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 | + |
601 | 630 | targetArray = new JSONArray(); |
602 | 631 | } |
603 | | - for (Object obj : array) { |
| 632 | + |
| 633 | + for (int i = 0; i < array.size(); i++) { |
| 634 | + Object obj = array.get(i); |
604 | 635 | if (obj == null) { |
605 | 636 | continue; |
606 | 637 | } |
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); |
610 | 650 | } |
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); |
615 | 665 | } |
616 | | - targetArray.remove(obj); |
617 | 666 | } |
618 | 667 | } |
619 | 668 |
|
620 | 669 | //add all 或 remove all >>>>>>>>>>>>>>>>>>>>>>>>> |
621 | 670 |
|
622 | 671 | //PUT <<<<<<<<<<<<<<<<<<<<<<<<< |
623 | | - sqlRequest.put(realKey, targetArray); |
| 672 | + sqlRequest.put(realKey, targetArray != null ? targetArray : JSON.toJSONString(targetObj, SerializerFeature.WriteMapNullValue)); |
624 | 673 | //PUT >>>>>>>>>>>>>>>>>>>>>>>>> |
625 | 674 |
|
626 | 675 | } |
|
0 commit comments