Skip to content

Commit

Permalink
1.2.6.3.2
Browse files Browse the repository at this point in the history
- 支持通过 getParentJsonMap() 和 getParentJsonList() 获取 JsonMap 和 JsonList 的父对象。
- 修复一些已知问题
  • Loading branch information
kongzue committed Jul 12, 2024
1 parent a87acf0 commit 78b201d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 16 deletions.
2 changes: 1 addition & 1 deletion basejson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 30
versionCode 19
versionName "1.2.6.3.1"
versionName "1.2.6.3.2"
}

buildTypes {
Expand Down
108 changes: 101 additions & 7 deletions basejson/src/main/java/com/kongzue/baseokhttp/util/JsonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 文本
*
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
35 changes: 27 additions & 8 deletions basejson/src/main/java/com/kongzue/baseokhttp/util/JsonMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 78b201d

Please sign in to comment.