Fixes corner case where strict mode isn't working when JSONTokener's next() and back() are called first

This commit is contained in:
XIAYM-gh
2026-09-02 20:31:40 +08:00
parent 4f859fdf3b
commit bcbbc89568
5 changed files with 81 additions and 7 deletions
@@ -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"));
}
}