1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #324 from dtalex/JSONPointerOnBeans

Allow user to invoke query and optQuery ,with a JSONPointer
This commit is contained in:
Sean Leary 2017-03-27 20:04:18 -05:00 committed by GitHub
commit 80e2ea2a80
2 changed files with 73 additions and 6 deletions

View file

@ -980,7 +980,30 @@ public class JSONArray implements Iterable<Object> {
* @return the item matched by the JSONPointer, otherwise null * @return the item matched by the JSONPointer, otherwise null
*/ */
public Object query(String jsonPointer) { 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:
* <pre>
* [
* {"b":"c"}
* ]
* </pre>
* and this JSONPointer:
* <pre>
* "/0/b"
* </pre>
* 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<Object> {
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
*/ */
public Object optQuery(String jsonPointer) { 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 { try {
return pointer.queryFrom(this); return jsonPointer.queryFrom(this);
} catch (JSONPointerException e) { } catch (JSONPointerException e) {
return null; return null;
} }

View file

@ -1359,7 +1359,29 @@ public class JSONObject {
* @return the item matched by the JSONPointer, otherwise null * @return the item matched by the JSONPointer, otherwise null
*/ */
public Object query(String jsonPointer) { 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:
* <pre>
* {
* "a":{"b":"c"}
* }
* </pre>
* and this JSONPointer:
* <pre>
* "/a/b"
* </pre>
* 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 * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
*/ */
public Object optQuery(String jsonPointer) { 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 { try {
return pointer.queryFrom(this); return jsonPointer.queryFrom(this);
} catch (JSONPointerException e) { } catch (JSONPointerException e) {
return null; return null;
} }