json remove support deep scan

This commit is contained in:
高铁
2019-06-27 00:30:35 +08:00
parent 4f7e0dadf5
commit a34675c7c2
2 changed files with 72 additions and 7 deletions
@@ -2136,7 +2136,7 @@ public class JSONPath implements JSONAware {
}
public boolean remove(JSONPath path, Object parent) {
return path.removePropertyValue(parent, propertyName);
return path.removePropertyValue(parent, propertyName, deep);
}
}
@@ -3718,10 +3718,18 @@ public class JSONPath implements JSONAware {
}
@SuppressWarnings({"rawtypes" })
protected boolean removePropertyValue(Object parent, String name) {
protected boolean removePropertyValue(Object parent, String name, boolean deep) {
if (parent instanceof Map) {
Object origin = ((Map) parent).remove(name);
return origin != null;
boolean found = origin != null;
if (deep) {
for (Object item : ((Map) parent).values()) {
removePropertyValue(item, name, deep);
}
}
return found;
}
ObjectDeserializer derializer = parserConfig.getDeserializer(parent.getClass());
@@ -3733,12 +3741,28 @@ public class JSONPath implements JSONAware {
if (beanDerializer != null) {
FieldDeserializer fieldDeserializer = beanDerializer.getFieldDeserializer(name);
if (fieldDeserializer == null) {
return false;
boolean found = false;
if (fieldDeserializer != null) {
fieldDeserializer.setValue(parent, null);
found = true;
}
fieldDeserializer.setValue(parent, null);
return true;
if (deep) {
Collection<Object> propertyValues = this.getPropertyValues(parent);
for (Object item : propertyValues) {
if (item == null) {
continue;
}
removePropertyValue(item, name, deep);
}
}
return found;
}
if (deep) {
return false;
}
throw new UnsupportedOperationException();
@@ -0,0 +1,41 @@
package com.alibaba.json.bvt.path;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import junit.framework.TestCase;
public class JSONPath_13
extends TestCase {
public void test_0() {
JSONObject root = new JSONObject();
root.put("company", new JSONObject());
root.getJSONObject("company").put("id", 123);
root.getJSONObject("company").put("name", "jobs");
JSONPath.remove(root, "$..id");
assertEquals("{\"company\":{\"name\":\"jobs\"}}", root.toJSONString());
}
public void test_1() {
Root root = new Root();
root.company = new Company();
root.company.id = 123;
root.company.name = "jobs";
JSONPath.remove(root, "$..id");
assertEquals("{\"company\":{\"name\":\"jobs\"}}", JSON.toJSONString(root));
}
public static class Root {
public Company company;
}
public static class Company {
public Integer id;
public String name;
}
}