diff --git a/basejson/build.gradle b/basejson/build.gradle index 18a42f6..2a000fd 100644 --- a/basejson/build.gradle +++ b/basejson/build.gradle @@ -8,7 +8,7 @@ android { //noinspection ExpiredTargetSdkVersion targetSdkVersion 30 versionCode 19 - versionName "1.2.6.3.1" + versionName "1.2.6.3.2" } buildTypes { diff --git a/basejson/src/main/java/com/kongzue/baseokhttp/util/JsonList.java b/basejson/src/main/java/com/kongzue/baseokhttp/util/JsonList.java index d6f39a7..df7a76c 100644 --- a/basejson/src/main/java/com/kongzue/baseokhttp/util/JsonList.java +++ b/basejson/src/main/java/com/kongzue/baseokhttp/util/JsonList.java @@ -2,6 +2,7 @@ import org.json.JSONArray; +import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -248,21 +249,86 @@ public JsonMap getJsonMap(int index) { public JsonList set(Object value) { callParentRelease(); + if (value instanceof JsonMap) { + ((JsonMap) value).setParentJsonList(this); + } + if (value instanceof JsonList) { + ((JsonList) value).setParentJsonList(this); + } super.add(value); return this; } public JsonList set(int index, Object value) { callParentRelease(); + if (value instanceof JsonMap) { + ((JsonMap) value).setParentJsonList(this); + } + if (value instanceof JsonList) { + ((JsonList) value).setParentJsonList(this); + } super.set(index, value); return this; } public void add(int index, Object value) { callParentRelease(); + if (value instanceof JsonMap) { + ((JsonMap) value).setParentJsonList(this); + } + if (value instanceof JsonList) { + ((JsonList) value).setParentJsonList(this); + } super.add(index, value); } + @Override + public boolean add(Object value) { + if (value instanceof JsonMap) { + ((JsonMap) value).setParentJsonList(this); + } + if (value instanceof JsonList) { + ((JsonList) value).setParentJsonList(this); + } + return super.add(value); + } + + @Override + public boolean addAll(Collection c) { + if (c != null) { + Object[] a = c.toArray(); + for (int i = 0; i < a.length; i++) { + Object value = a[i]; + if (value instanceof JsonMap) { + ((JsonMap) value).setParentJsonList(this); + } + if (value instanceof JsonList) { + ((JsonList) value).setParentJsonList(this); + } + } + return super.addAll(c); + } + return false; + } + + @Override + public boolean addAll(int index, Collection c) { + if (c != null) { + Object[] a = c.toArray(); + for (int i = 0; i < a.length; i++) { + Object value = a[i]; + if (value instanceof JsonMap) { + ((JsonMap) value).setParentJsonList(this); + } + if (value instanceof JsonList) { + ((JsonList) value).setParentJsonList(this); + } + } + return super.addAll(index, c); + } + return false; + } + /** * 输出 Json 文本 * @@ -353,6 +419,7 @@ public JsonList findRemove(String key, Object value) { Object child = get(i); if (child instanceof JsonMap) { if (((JsonMap) child).getString(key).equals(String.valueOf(value))) { + ((JsonMap) child).cleanParent(); remove(i); return this; } @@ -368,6 +435,22 @@ public JsonList remove(JsonMap data) { for (int i = 0; i < size(); i++) { Object child = get(i); if (data.toString().equals(child.toString())) { + data.cleanParent(); + remove(i); + return this; + } + } + return this; + } + + public JsonList remove(JsonList data) { + if (data == null || data.isEmpty()) { + return this; + } + for (int i = 0; i < size(); i++) { + Object child = get(i); + if (data.toString().equals(child.toString())) { + data.cleanParent(); remove(i); return this; } @@ -385,6 +468,7 @@ public JsonList preprocessedJsonMapData(JsonMapPreprocessingEvents events) { Object data = iterator.next(); if (data instanceof JsonMap) { JsonMap jsonMap = (JsonMap) data; + jsonMap.cleanParent(); JsonMap result = events.processingData(jsonMap); if (events.isDeleteWhenDataIsNull() && result == null) { iterator.remove(); @@ -404,15 +488,19 @@ private void callParentRelease() { if (preCreated) { return; } - if (parentJsonMap != null && parentJsonMap.get(preBuildKey) != this) { - parentJsonMap.set(preBuildKey, this); + if (parentJsonMap != null && preBuildKey != null) { + if (parentJsonMap.get(preBuildKey) != this) { + parentJsonMap.set(preBuildKey, this); + } preCreated = true; } - if (parentJsonList != null && !parentJsonList.contains(this)) { - if (preBuildIndex >= 0) { - parentJsonList.set(preBuildIndex, this); - } else { - parentJsonList.set(this); + if (parentJsonList != null) { + if (!parentJsonList.contains(this)) { + if (preBuildIndex >= 0) { + parentJsonList.set(preBuildIndex, this); + } else { + parentJsonList.set(this); + } } preCreated = true; } @@ -473,4 +561,10 @@ public JsonMap getParentJsonMap() { public JsonList getParentJsonList() { return parentJsonList == null ? new JsonList() : parentJsonList; } + + public void cleanParent() { + parentJsonMap = null; + parentJsonList = null; + preCreated = false; + } } diff --git a/basejson/src/main/java/com/kongzue/baseokhttp/util/JsonMap.java b/basejson/src/main/java/com/kongzue/baseokhttp/util/JsonMap.java index 784c8da..68dac2c 100644 --- a/basejson/src/main/java/com/kongzue/baseokhttp/util/JsonMap.java +++ b/basejson/src/main/java/com/kongzue/baseokhttp/util/JsonMap.java @@ -274,7 +274,16 @@ public JsonMap set(String key, Object value) { @Override public Object put(String key, Object value) { callParentRelease(); - if (value == null) value = ""; + if (value == null) { + value = ""; + } else { + if (value instanceof JsonMap) { + ((JsonMap) value).setParentJsonMap(this); + } + if (value instanceof JsonList) { + ((JsonList) value).setParentJsonMap(this); + } + } return super.put(key, value); } @@ -353,15 +362,19 @@ private void callParentRelease() { if (preCreated) { return; } - if (parentJsonMap != null && parentJsonMap.get(preBuildKey) != this) { - parentJsonMap.set(preBuildKey, this); + if (parentJsonMap != null && preBuildKey != null) { + if (parentJsonMap.get(preBuildKey) != this) { + parentJsonMap.set(preBuildKey, this); + } preCreated = true; } - if (parentJsonList != null && !parentJsonList.contains(this)) { - if (preBuildIndex >= 0) { - parentJsonList.set(preBuildIndex, this); - } else { - parentJsonList.set(this); + if (parentJsonList != null) { + if (!parentJsonList.contains(this)) { + if (preBuildIndex >= 0) { + parentJsonList.set(preBuildIndex, this); + } else { + parentJsonList.set(this); + } } preCreated = true; } @@ -422,4 +435,10 @@ public JsonMap getParentJsonMap() { public JsonList getParentJsonList() { return parentJsonList == null ? new JsonList() : parentJsonList; } + + public void cleanParent() { + parentJsonMap = null; + parentJsonList = null; + preCreated = false; + } }