From 215f4268bfe10e560c2129f5442a0bb04dd32f9a Mon Sep 17 00:00:00 2001 From: Simulant Date: Sat, 11 Jan 2025 21:35:36 +0100 Subject: [PATCH 1/5] add Javadoc and rename parameters to speaking variable names --- src/main/java/org/json/JSONTokener.java | 35 +++++++++++++++++-------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index b6aee1110..c17907cf9 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 JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguratio 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 JSONTokener(InputStream inputStream) { /** * 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); } /** From 3b7ba07531f4b6486667767b291a78bfa2616dcb Mon Sep 17 00:00:00 2001 From: Simulant Date: Sat, 11 Jan 2025 21:40:41 +0100 Subject: [PATCH 2/5] add test for invalid input on JSONTokener --- .../java/org/json/junit/JSONTokenerTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java index 59ca6d8f6..c436d27e2 100644 --- a/src/test/java/org/json/junit/JSONTokenerTest.java +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -325,4 +325,21 @@ public void testAutoClose(){ 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()); + } + } From ad44a9274c487cfc580a183226ed4d5aea0c99ab Mon Sep 17 00:00:00 2001 From: Simulant Date: Sat, 11 Jan 2025 21:43:04 +0100 Subject: [PATCH 3/5] add new test cases for JSONObject and JSONArray Constructors with JSONTokener and strict mode --- .../junit/JSONParserConfigurationTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java index 422c90c34..006f2a41d 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.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 void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurround 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. From afd9a6fbb70cd66d440a53458c186d11377bd2ff Mon Sep 17 00:00:00 2001 From: Simulant Date: Wed, 15 Jan 2025 20:55:13 +0100 Subject: [PATCH 4/5] #928 add missing java dock for JSONParserConfiguration --- src/main/java/org/json/JSONParserConfiguration.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index f5d0660e2..7fcf97ece 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -104,6 +104,9 @@ public boolean isOverwriteDuplicateKey() { } /** + * The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters. + * Otherwise, the parser is more relaxed and might tolerate some invalid characters. + * * @return the current strict mode setting. */ public boolean isStrictMode() { From 94341cd6634c09bba1a8976d1c2999e1fdb15c12 Mon Sep 17 00:00:00 2001 From: Simulant Date: Wed, 15 Jan 2025 20:58:45 +0100 Subject: [PATCH 5/5] Revert "#928 add missing java dock for JSONParserConfiguration" This reverts commit afd9a6fbb70cd66d440a53458c186d11377bd2ff. --- src/main/java/org/json/JSONParserConfiguration.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 7fcf97ece..f5d0660e2 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -104,9 +104,6 @@ public boolean isOverwriteDuplicateKey() { } /** - * The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters. - * Otherwise, the parser is more relaxed and might tolerate some invalid characters. - * * @return the current strict mode setting. */ public boolean isStrictMode() {