@@ -1359,7 +1359,29 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
13591359 * @return the item matched by the JSONPointer, otherwise null
13601360 */
13611361 public Object query (String jsonPointer ) {
1362- return new JSONPointer (jsonPointer ).queryFrom (this );
1362+ return query (new JSONPointer (jsonPointer ));
1363+ }
1364+ /**
1365+ * Uses a uaer initialized JSONPointer and tries to
1366+ * match it to an item within this JSONObject. For example, given a
1367+ * JSONObject initialized with this document:
1368+ * <pre>
1369+ * {
1370+ * "a":{"b":"c"}
1371+ * }
1372+ * </pre>
1373+ * and this JSONPointer:
1374+ * <pre>
1375+ * "/a/b"
1376+ * </pre>
1377+ * Then this method will return the String "c".
1378+ * A JSONPointerException may be thrown from code called by this method.
1379+ *
1380+ * @param jsonPointer string that can be used to create a JSONPointer
1381+ * @return the item matched by the JSONPointer, otherwise null
1382+ */
1383+ public Object query (JSONPointer jsonPointer ) {
1384+ return jsonPointer .queryFrom (this );
13631385 }
13641386
13651387 /**
@@ -1371,9 +1393,20 @@ public Object query(String jsonPointer) {
13711393 * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
13721394 */
13731395 public Object optQuery (String jsonPointer ) {
1374- JSONPointer pointer = new JSONPointer (jsonPointer );
1396+ return optQuery (new JSONPointer (jsonPointer ));
1397+ }
1398+
1399+ /**
1400+ * Queries and returns a value from this object using {@code jsonPointer}, or
1401+ * returns null if the query fails due to a missing key.
1402+ *
1403+ * @param The JSON pointer
1404+ * @return the queried value or {@code null}
1405+ * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
1406+ */
1407+ public Object optQuery (JSONPointer jsonPointer ) {
13751408 try {
1376- return pointer .queryFrom (this );
1409+ return jsonPointer .queryFrom (this );
13771410 } catch (JSONPointerException e ) {
13781411 return null ;
13791412 }
0 commit comments