Skip to content

Commit

Permalink
Merge pull request #234 from erosb/master
Browse files Browse the repository at this point in the history
fixing #233
  • Loading branch information
stleary committed May 21, 2016
2 parents 9a81b40 + 56be31e commit b2bde1f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,23 @@ public JSONArray put(int index, Object value) throws JSONException {
public Object query(String jsonPointer) {
return new JSONPointer(jsonPointer).queryFrom(this);
}

/**
* Queries and returns a value from this object using {@code jsonPointer}, or
* returns null if the query fails due to a missing key.
*
* @param jsonPointer the string representation of the JSON pointer
* @return the queried value or {@code null}
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
*/
public Object optQuery(String jsonPointer) {
JSONPointer pointer = new JSONPointer(jsonPointer);
try {
return pointer.queryFrom(this);
} catch (JSONPointerException e) {
return null;
}
}

/**
* Remove an index and close the hole.
Expand Down
17 changes: 17 additions & 0 deletions JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,23 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
public Object query(String jsonPointer) {
return new JSONPointer(jsonPointer).queryFrom(this);
}

/**
* Queries and returns a value from this object using {@code jsonPointer}, or
* returns null if the query fails due to a missing key.
*
* @param jsonPointer the string representation of the JSON pointer
* @return the queried value or {@code null}
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
*/
public Object optQuery(String jsonPointer) {
JSONPointer pointer = new JSONPointer(jsonPointer);
try {
return pointer.queryFrom(this);
} catch (JSONPointerException e) {
return null;
}
}

/**
* Produce a string in double quotes with backslash sequences in all the
Expand Down

0 comments on commit b2bde1f

Please sign in to comment.