Skip to content

Commit 80e2ea2

Browse files
authored
Merge pull request stleary#324 from dtalex/JSONPointerOnBeans
Allow user to invoke query and optQuery ,with a JSONPointer
2 parents 724fb88 + 2917104 commit 80e2ea2

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

JSONArray.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,30 @@ public JSONArray put(int index, Object value) throws JSONException {
980980
* @return the item matched by the JSONPointer, otherwise null
981981
*/
982982
public Object query(String jsonPointer) {
983-
return new JSONPointer(jsonPointer).queryFrom(this);
983+
return query(new JSONPointer(jsonPointer));
984+
}
985+
986+
/**
987+
* Uses a uaer initialized JSONPointer and tries to
988+
* match it to an item whithin this JSONArray. For example, given a
989+
* JSONArray initialized with this document:
990+
* <pre>
991+
* [
992+
* {"b":"c"}
993+
* ]
994+
* </pre>
995+
* and this JSONPointer:
996+
* <pre>
997+
* "/0/b"
998+
* </pre>
999+
* Then this method will return the String "c"
1000+
* A JSONPointerException may be thrown from code called by this method.
1001+
*
1002+
* @param jsonPointer string that can be used to create a JSONPointer
1003+
* @return the item matched by the JSONPointer, otherwise null
1004+
*/
1005+
public Object query(JSONPointer jsonPointer) {
1006+
return jsonPointer.queryFrom(this);
9841007
}
9851008

9861009
/**
@@ -992,9 +1015,20 @@ public Object query(String jsonPointer) {
9921015
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
9931016
*/
9941017
public Object optQuery(String jsonPointer) {
995-
JSONPointer pointer = new JSONPointer(jsonPointer);
1018+
return optQuery(new JSONPointer(jsonPointer));
1019+
}
1020+
1021+
/**
1022+
* Queries and returns a value from this object using {@code jsonPointer}, or
1023+
* returns null if the query fails due to a missing key.
1024+
*
1025+
* @param The JSON pointer
1026+
* @return the queried value or {@code null}
1027+
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
1028+
*/
1029+
public Object optQuery(JSONPointer jsonPointer) {
9961030
try {
997-
return pointer.queryFrom(this);
1031+
return jsonPointer.queryFrom(this);
9981032
} catch (JSONPointerException e) {
9991033
return null;
10001034
}

JSONObject.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)