mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
improved failure handling
This commit is contained in:
parent
5bee7e3b45
commit
792c6f6a9c
1 changed files with 13 additions and 4 deletions
|
@ -53,7 +53,11 @@ public class JSONPointer {
|
|||
if (current instanceof JSONObject) {
|
||||
current = ((JSONObject) current).opt(unescape(token));
|
||||
} else if (current instanceof JSONArray) {
|
||||
current = readByIndexToken(current, unescape(token));
|
||||
current = readByIndexToken(current, token);
|
||||
} else {
|
||||
throw new JSONPointerException(format(
|
||||
"value [%s] is not an array or object therefore its key %s cannot be resolved", current,
|
||||
token));
|
||||
}
|
||||
}
|
||||
return current;
|
||||
|
@ -61,10 +65,15 @@ public class JSONPointer {
|
|||
|
||||
private Object readByIndexToken(Object current, String indexToken) {
|
||||
try {
|
||||
return ((JSONArray) current).opt(Integer.parseInt(unescape(indexToken)));
|
||||
int index = Integer.parseInt(indexToken);
|
||||
JSONArray currentArr = (JSONArray) current;
|
||||
if (index >= currentArr.length()) {
|
||||
throw new JSONPointerException(format("index %d is out of bounds - the array has %d elements", index,
|
||||
currentArr.length()));
|
||||
}
|
||||
return currentArr.get(index);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new JSONPointerException(format("%s is not an array index", unescape(indexToken)), e);
|
||||
throw new JSONPointerException(format("%s is not an array index", indexToken), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue