Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "\"");
}

Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/json/junit/JSONTokenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Loading