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; }