Skip to content

Fix for JSONPointer, for stleary/JSON-Java#233 #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,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 @@ -1341,6 +1341,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