diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 6458ab254..0715e22c8 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -141,14 +141,14 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) } if (nextChar == ']') { // trailing commas are not allowed in strict mode - if (jsonParserConfiguration.isStrictMode()) { + if (jsonParserConfiguration != null && jsonParserConfiguration.isStrictMode()) { throw x.syntaxError("Strict mode error: Expected another array element"); } return; } if (nextChar == ',') { // consecutive commas are not allowed in strict mode - if (jsonParserConfiguration.isStrictMode()) { + if (jsonParserConfiguration != null && jsonParserConfiguration.isStrictMode()) { throw x.syntaxError("Strict mode error: Expected a valid array element"); } return; diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 3bb6da7ed..f15a637a6 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -255,7 +255,7 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration if (key != null) { // Check if key exists boolean keyExists = this.opt(key) != null; - if (keyExists && !jsonParserConfiguration.isOverwriteDuplicateKey()) { + if (keyExists && jsonParserConfiguration != null && !jsonParserConfiguration.isOverwriteDuplicateKey()) { throw x.syntaxError("Duplicate key \"" + key + "\""); } @@ -271,13 +271,13 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration switch (x.nextClean()) { case ';': // In strict mode semicolon is not a valid separator - if (jsonParserConfiguration.isStrictMode()) { + if (jsonParserConfiguration != null && jsonParserConfiguration.isStrictMode()) { throw x.syntaxError("Strict mode error: Invalid character ';' found"); } case ',': if (x.nextClean() == '}') { // trailing commas are not allowed in strict mode - if (jsonParserConfiguration.isStrictMode()) { + if (jsonParserConfiguration != null && jsonParserConfiguration.isStrictMode()) { throw x.syntaxError("Strict mode error: Expected another object element"); } return; diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java index 59ca6d8f6..502e572f9 100644 --- a/src/test/java/org/json/junit/JSONTokenerTest.java +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -325,4 +325,20 @@ 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()); + } }