From b050ee247a2e49c3d86f630fecc36d073962f059 Mon Sep 17 00:00:00 2001 From: serngawy Date: Tue, 15 Oct 2019 10:46:36 -0400 Subject: [PATCH 1/3] Add remove by query feature to JSONObject Signed-off-by: serngawy --- JSONObject.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/JSONObject.java b/JSONObject.java index 678a5d67b..cce159418 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -47,6 +47,8 @@ of this software and associated documentation files (the "Software"), to deal import java.util.Set; import java.util.regex.Pattern; +import com.hpe.officeconnect.yang.JsonYang; + /** * A JSONObject is an unordered collection of name/value pairs. Its external * form is a string wrapped in curly braces with colons between the names and @@ -2039,6 +2041,33 @@ public Object remove(String key) { return this.map.remove(key); } + /** + * Remove a name and its value, based on the given key Path. + * + * @param keyPath + * The keyPath to be removed ex: group/person/address/city. + * @return The value that was associated with the keyPath, or null if there was + * no value or keyPath does not exist. + */ + public Object removeByQuary(String keyPath) { + if (keyPath == null || keyPath.isEmpty()) { + throw new IllegalArgumentException("a keyPath cannot be null or empty"); + } + if (keyPath.startsWith("/")) { + throw new IllegalArgumentException("a keyPath should not start with '/'"); + } + if (!keyPath.contains("/")) { + return this.remove(keyPath); + } + + int idx = keyPath.indexOf("/"); + String firstKey = keyPath.substring(0, idx); + if (this.optJSONObject(firstKey) != null) { + return this.getJSONObject(firstKey).removeByQuary(keyPath.substring(idx+1)); + } + return null; + } + /** * Determine if two JSONObjects are similar. * They must contain the same set of names which must be associated with From 08c7975967196bb1444df0c234ea71a3503f4852 Mon Sep 17 00:00:00 2001 From: serngawy Date: Tue, 15 Oct 2019 10:48:31 -0400 Subject: [PATCH 2/3] Add remove by query to JSONObject Signed-off-by: serngawy --- JSONObject.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index cce159418..2eb145c9e 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -2049,7 +2049,7 @@ public Object remove(String key) { * @return The value that was associated with the keyPath, or null if there was * no value or keyPath does not exist. */ - public Object removeByQuary(String keyPath) { + public Object removeByQuery(String keyPath) { if (keyPath == null || keyPath.isEmpty()) { throw new IllegalArgumentException("a keyPath cannot be null or empty"); } @@ -2063,7 +2063,7 @@ public Object removeByQuary(String keyPath) { int idx = keyPath.indexOf("/"); String firstKey = keyPath.substring(0, idx); if (this.optJSONObject(firstKey) != null) { - return this.getJSONObject(firstKey).removeByQuary(keyPath.substring(idx+1)); + return this.getJSONObject(firstKey).removeByQuery(keyPath.substring(idx+1)); } return null; } From 46821b48d49cd9ffb51b962af1f081ae7c16dd94 Mon Sep 17 00:00:00 2001 From: serngawy Date: Tue, 15 Oct 2019 10:52:26 -0400 Subject: [PATCH 3/3] Add remove by quey to JSONObject Signed-off-by: serngawy --- JSONObject.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 2eb145c9e..0a2fa4387 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -47,8 +47,6 @@ of this software and associated documentation files (the "Software"), to deal import java.util.Set; import java.util.regex.Pattern; -import com.hpe.officeconnect.yang.JsonYang; - /** * A JSONObject is an unordered collection of name/value pairs. Its external * form is a string wrapped in curly braces with colons between the names and