Fixes corner case where strict mode isn't working when JSONTokener's next() and back() are called first
This commit is contained in:
@@ -94,9 +94,21 @@ public class JSONArray implements Iterable<Object> {
|
||||
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
|
||||
*/
|
||||
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||
this(x, jsonParserConfiguration, x.isAtStart());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration, for internal use. <br>
|
||||
* Never call this instead of using withStrictMode(boolean).
|
||||
*
|
||||
* @param x A JSONTokener instance from which the JSONArray is constructed.
|
||||
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||
* @param eofRequired A boolean that determines whether this array is the root.
|
||||
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
|
||||
*/
|
||||
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean eofRequired) throws JSONException {
|
||||
this();
|
||||
|
||||
boolean isInitial = x.getPrevious() == 0;
|
||||
if (x.nextClean() != '[') {
|
||||
throw x.syntaxError("A JSONArray text must start with '['");
|
||||
}
|
||||
@@ -118,10 +130,10 @@ public class JSONArray implements Iterable<Object> {
|
||||
x.back();
|
||||
this.myArrayList.add(x.nextValue());
|
||||
}
|
||||
if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
|
||||
if (checkForSyntaxError(x, jsonParserConfiguration, eofRequired)) return;
|
||||
}
|
||||
} else {
|
||||
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
|
||||
if (eofRequired && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
|
||||
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,14 +214,31 @@ public class JSONObject {
|
||||
* duplicated key.
|
||||
*/
|
||||
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||
this(x, jsonParserConfiguration, x.isAtStart());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a JSONObject from a JSONTokener with custom json parse configurations, for internal use. <br>
|
||||
* Never call this instead of using withStrictMode(boolean).
|
||||
*
|
||||
* @param x
|
||||
* A JSONTokener object containing the source string.
|
||||
* @param jsonParserConfiguration
|
||||
* Variable to pass parser custom configuration for json parsing.
|
||||
* @param eofRequired
|
||||
* A boolean that determines whether this object is the root.
|
||||
* @throws JSONException
|
||||
* If there is a syntax error in the source string or a
|
||||
* duplicated key.
|
||||
*/
|
||||
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean eofRequired) throws JSONException {
|
||||
this();
|
||||
boolean isInitial = x.getPrevious() == 0;
|
||||
|
||||
if (x.nextClean() != '{') {
|
||||
throw x.syntaxError("A JSONObject text must begin with '{'");
|
||||
}
|
||||
for (;;) {
|
||||
if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
|
||||
if (parseJSONObject(x, jsonParserConfiguration, eofRequired)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +120,15 @@ public class JSONTokener {
|
||||
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the tokener is currently positioned at the beginning,
|
||||
*
|
||||
* @return true if the current input position is the beginning
|
||||
*/
|
||||
public boolean isAtStart() {
|
||||
return this.index == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Back up one character. This provides a sort of lookahead capability,
|
||||
* so that you can test for a digit or letter before attempting to parse
|
||||
@@ -458,14 +467,14 @@ public class JSONTokener {
|
||||
case '{':
|
||||
this.back();
|
||||
try {
|
||||
return new JSONObject(this, jsonParserConfiguration);
|
||||
return new JSONObject(this, jsonParserConfiguration, false);
|
||||
} catch (StackOverflowError e) {
|
||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||
}
|
||||
case '[':
|
||||
this.back();
|
||||
try {
|
||||
return new JSONArray(this, jsonParserConfiguration);
|
||||
return new JSONArray(this, jsonParserConfiguration, false);
|
||||
} catch (StackOverflowError e) {
|
||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||
}
|
||||
|
||||
@@ -1566,4 +1566,22 @@ public class JSONArrayTest {
|
||||
"[1,null,3]", jsonArray.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void strictModeShouldCheckTrailingCharactersAfterNextAndBack() {
|
||||
JSONParserConfiguration strict =
|
||||
new JSONParserConfiguration().withStrictMode();
|
||||
|
||||
JSONTokener tok = new JSONTokener("[]xxx");
|
||||
|
||||
tok.next();
|
||||
tok.back();
|
||||
|
||||
JSONException exception = assertThrows(
|
||||
JSONException.class,
|
||||
() -> new JSONArray(tok, strict));
|
||||
|
||||
assertTrue(exception.getMessage().contains(
|
||||
"Unparsed characters found at end of input text"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4337,4 +4337,22 @@ public class JSONObjectTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void strictModeShouldCheckTrailingCharactersAfterNextAndBack() {
|
||||
JSONParserConfiguration strict =
|
||||
new JSONParserConfiguration().withStrictMode();
|
||||
|
||||
JSONTokener tok = new JSONTokener("{}xxx");
|
||||
|
||||
tok.next();
|
||||
tok.back();
|
||||
|
||||
JSONException exception = assertThrows(
|
||||
JSONException.class,
|
||||
() -> new JSONObject(tok, strict));
|
||||
|
||||
assertTrue(exception.getMessage().contains(
|
||||
"Unparsed characters found at end of input text"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user