From a34675c7c2edefff45d36cdfbf7bee37e04112a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Thu, 27 Jun 2019 00:30:35 +0800 Subject: [PATCH] json remove support deep scan --- .../java/com/alibaba/fastjson/JSONPath.java | 38 +++++++++++++---- .../alibaba/json/bvt/path/JSONPath_13.java | 41 +++++++++++++++++++ 2 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/alibaba/json/bvt/path/JSONPath_13.java diff --git a/src/main/java/com/alibaba/fastjson/JSONPath.java b/src/main/java/com/alibaba/fastjson/JSONPath.java index d73593eb9..c6b42a3cd 100644 --- a/src/main/java/com/alibaba/fastjson/JSONPath.java +++ b/src/main/java/com/alibaba/fastjson/JSONPath.java @@ -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 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(); diff --git a/src/test/java/com/alibaba/json/bvt/path/JSONPath_13.java b/src/test/java/com/alibaba/json/bvt/path/JSONPath_13.java new file mode 100644 index 000000000..87268bfc3 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/path/JSONPath_13.java @@ -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; + } +}