File tree 1 file changed +13
-4
lines changed
1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -53,18 +53,27 @@ public Object queryFrom(JSONObject document) {
53
53
if (current instanceof JSONObject ) {
54
54
current = ((JSONObject ) current ).opt (unescape (token ));
55
55
} else if (current instanceof JSONArray ) {
56
- current = readByIndexToken (current , unescape (token ));
56
+ current = readByIndexToken (current , token );
57
+ } else {
58
+ throw new JSONPointerException (format (
59
+ "value [%s] is not an array or object therefore its key %s cannot be resolved" , current ,
60
+ token ));
57
61
}
58
62
}
59
63
return current ;
60
64
}
61
65
62
66
private Object readByIndexToken (Object current , String indexToken ) {
63
67
try {
64
- return ((JSONArray ) current ).opt (Integer .parseInt (unescape (indexToken )));
68
+ int index = Integer .parseInt (indexToken );
69
+ JSONArray currentArr = (JSONArray ) current ;
70
+ if (index >= currentArr .length ()) {
71
+ throw new JSONPointerException (format ("index %d is out of bounds - the array has %d elements" , index ,
72
+ currentArr .length ()));
73
+ }
74
+ return currentArr .get (index );
65
75
} catch (NumberFormatException e ) {
66
- throw new JSONPointerException (format ("%s is not an array index" , unescape ( indexToken ) ), e );
76
+ throw new JSONPointerException (format ("%s is not an array index" , indexToken ), e );
67
77
}
68
78
}
69
-
70
79
}
You can’t perform that action at this time.
0 commit comments