From 9e0fc5e6804b205ba907e819028ff6c31d6907e8 Mon Sep 17 00:00:00 2001 From: alessandro rao Date: Sat, 25 Feb 2017 13:27:50 +0100 Subject: [PATCH 1/2] Allow user to invoke query and optQuery ,with a JSONPointer,directly from JSONArray or JSONObject --- JSONArray.java | 40 +++++++++++++++++++++++++++++++++++++--- JSONObject.java | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index 2446fd6..8647f0a 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -980,7 +980,30 @@ public class JSONArray implements Iterable { * @return the item matched by the JSONPointer, otherwise null */ public Object query(String jsonPointer) { - return new JSONPointer(jsonPointer).queryFrom(this); + return query(new JSONPointer(jsonPointer)); + } + + /** + * Uses a uaer initialized JSONPointer and tries to + * match it to an item whithin this JSONArray. For example, given a + * JSONArray initialized with this document: + *
+     * [
+     *     {"b":"c"}
+     * ]
+     * 
+ * and this JSONPointer: + *
+     * "/0/b"
+     * 
+ * Then this method will return the String "c" + * A JSONPointerException may be thrown from code called by this method. + * + * @param jsonPointer string that can be used to create a JSONPointer + * @return the item matched by the JSONPointer, otherwise null + */ + public Object query(JSONPointer jsonPointer) { + return jsonPointer.queryFrom(this); } /** @@ -992,9 +1015,20 @@ public class JSONArray implements Iterable { * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax */ public Object optQuery(String jsonPointer) { - JSONPointer pointer = new JSONPointer(jsonPointer); + return query(new JSONPointer(jsonPointer)); + } + + /** + * Queries and returns a value from this object using {@code jsonPointer}, or + * returns null if the query fails due to a missing key. + * + * @param The JSON pointer + * @return the queried value or {@code null} + * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax + */ + public Object optQuery(JSONPointer jsonPointer) { try { - return pointer.queryFrom(this); + return jsonPointer.queryFrom(this); } catch (JSONPointerException e) { return null; } diff --git a/JSONObject.java b/JSONObject.java index 1eab694..b71b3b6 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -1359,7 +1359,29 @@ public class JSONObject { * @return the item matched by the JSONPointer, otherwise null */ public Object query(String jsonPointer) { - return new JSONPointer(jsonPointer).queryFrom(this); + return query(new JSONPointer(jsonPointer)); + } + /** + * Uses a uaer initialized JSONPointer and tries to + * match it to an item within this JSONObject. For example, given a + * JSONObject initialized with this document: + *
+     * {
+     *     "a":{"b":"c"}
+     * }
+     * 
+ * and this JSONPointer: + *
+     * "/a/b"
+     * 
+ * Then this method will return the String "c". + * A JSONPointerException may be thrown from code called by this method. + * + * @param jsonPointer string that can be used to create a JSONPointer + * @return the item matched by the JSONPointer, otherwise null + */ + public Object query(JSONPointer jsonPointer) { + return jsonPointer.queryFrom(this); } /** @@ -1371,9 +1393,20 @@ public class JSONObject { * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax */ public Object optQuery(String jsonPointer) { - JSONPointer pointer = new JSONPointer(jsonPointer); + return optQuery(new JSONPointer(jsonPointer)); + } + + /** + * Queries and returns a value from this object using {@code jsonPointer}, or + * returns null if the query fails due to a missing key. + * + * @param The JSON pointer + * @return the queried value or {@code null} + * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax + */ + public Object optQuery(JSONPointer jsonPointer) { try { - return pointer.queryFrom(this); + return jsonPointer.queryFrom(this); } catch (JSONPointerException e) { return null; } From 2917104b5377a1a7ee4a5a2eb2487457d78dd71c Mon Sep 17 00:00:00 2001 From: alessandro rao Date: Sat, 25 Feb 2017 14:35:02 +0100 Subject: [PATCH 2/2] Allow user to invoke query and optQuery ,with a JSONPointer,directly from JSONArray or JSONObject fix JSONArray --- JSONArray.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONArray.java b/JSONArray.java index 8647f0a..132d46d 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -1015,7 +1015,7 @@ public class JSONArray implements Iterable { * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax */ public Object optQuery(String jsonPointer) { - return query(new JSONPointer(jsonPointer)); + return optQuery(new JSONPointer(jsonPointer)); } /**