diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index b6aee11..c17907c 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -38,7 +38,18 @@ public class JSONTokener { /** * Construct a JSONTokener from a Reader. The caller must close the Reader. * - * @param reader A reader. + * @param reader the source. + */ + public JSONTokener(Reader reader) { + this(reader, new JSONParserConfiguration()); + } + + /** + * Construct a JSONTokener from a Reader with a given JSONParserConfiguration. The caller must close the Reader. + * + * @param reader the source. + * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser. + * */ public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguration) { this.jsonParserConfiguration = jsonParserConfiguration; @@ -54,10 +65,6 @@ public class JSONTokener { this.line = 1; } - public JSONTokener(Reader reader) { - this(reader, new JSONParserConfiguration()); - } - /** * Construct a JSONTokener from an InputStream. The caller must close the input stream. * @param inputStream The source. @@ -69,23 +76,29 @@ public class JSONTokener { /** * Construct a JSONTokener from an InputStream. The caller must close the input stream. * @param inputStream The source. + * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser. */ public JSONTokener(InputStream inputStream, JSONParserConfiguration jsonParserConfiguration) { - this(new InputStreamReader(inputStream, Charset.forName("UTF-8")),jsonParserConfiguration); + this(new InputStreamReader(inputStream, Charset.forName("UTF-8")), jsonParserConfiguration); } /** * Construct a JSONTokener from a string. * - * @param s A source string. + * @param source A source string. */ - public JSONTokener(String s) { - this(new StringReader(s)); + public JSONTokener(String source) { + this(new StringReader(source)); } - public JSONTokener(String s, JSONParserConfiguration jsonParserConfiguration) { - this(new StringReader(s), jsonParserConfiguration); + /** + * Construct a JSONTokener from an InputStream. The caller must close the input stream. + * @param source The source. + * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser. + */ + public JSONTokener(String source, JSONParserConfiguration jsonParserConfiguration) { + this(new StringReader(source), jsonParserConfiguration); } /** diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java index 422c90c..006f2a4 100644 --- a/src/test/java/org/json/junit/JSONParserConfigurationTest.java +++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java @@ -4,6 +4,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONParserConfiguration; +import org.json.JSONTokener; import org.junit.Test; import java.io.IOException; @@ -490,6 +491,40 @@ public class JSONParserConfigurationTest { je.getMessage()); } + @Test + public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingJSONTokener_shouldThrowJSONException() { + JSONException exception = assertThrows(JSONException.class, () -> { + new JSONObject(new JSONTokener("{\"key\":\"value\"} invalid trailing text"), new JSONParserConfiguration().withStrictMode(true)); + }); + + assertEquals("Strict mode error: Unparsed characters found at end of input text at 17 [character 18 line 1]", exception.getMessage()); + } + + @Test + public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingString_shouldThrowJSONException() { + JSONException exception = assertThrows(JSONException.class, () -> { + new JSONObject("{\"key\":\"value\"} invalid trailing text", new JSONParserConfiguration().withStrictMode(true)); + }); + assertEquals("Strict mode error: Unparsed characters found at end of input text at 17 [character 18 line 1]", exception.getMessage()); + } + + @Test + public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingJSONTokener_shouldThrowJSONException() { + JSONException exception = assertThrows(JSONException.class, () -> { + new JSONArray(new JSONTokener("[\"value\"] invalid trailing text"), new JSONParserConfiguration().withStrictMode(true)); + }); + + assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage()); + } + + @Test + public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingString_shouldThrowJSONException() { + JSONException exception = assertThrows(JSONException.class, () -> { + new JSONArray("[\"value\"] invalid trailing text", new JSONParserConfiguration().withStrictMode(true)); + }); + assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage()); + } + /** * This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in * this class. diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java index 59ca6d8..c436d27 100644 --- a/src/test/java/org/json/junit/JSONTokenerTest.java +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -325,4 +325,21 @@ public class JSONTokenerTest { assertEquals("Stream closed", exception.getMessage()); } } + + @Test + public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() { + String input = "{\"invalidInput\": [],}"; + JSONTokener tokener = new JSONTokener(input); + Object value = tokener.nextValue(); + assertEquals(new JSONObject(input).toString(), value.toString()); + } + + @Test + public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() { + String input = "[\"invalidInput\",]"; + JSONTokener tokener = new JSONTokener(input); + Object value = tokener.nextValue(); + assertEquals(new JSONArray(input).toString(), value.toString()); + } + }