Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use JSONParserConfiguration of JSONTokener in JSONObject and JSONArray constructor instead of creating a new one #948

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public JSONArray() {
* If there is a syntax error.
*/
public JSONArray(JSONTokener x) throws JSONException {
this(x, new JSONParserConfiguration());
this(x, x.getJsonParserConfiguration());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public JSONObject(JSONObject jo, String ... names) {
* duplicated key.
*/
public JSONObject(JSONTokener x) throws JSONException {
this(x, new JSONParserConfiguration());
this(x, x.getJsonParserConfiguration());
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -1509,6 +1510,14 @@ public void testRecursiveDepthArrayFor1001Levels() {
new JSONArray(array);
}

@Test
public void testStrictModeJSONTokener_expectException(){
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
JSONTokener tokener = new JSONTokener("[\"value\"]invalidCharacters", jsonParserConfiguration);

assertThrows(JSONException.class, () -> { new JSONArray(tokener); });
}

public static ArrayList<Object> buildNestedArray(int maxDepth) {
if (maxDepth <= 0) {
return new ArrayList<>();
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3853,6 +3853,15 @@ public void clarifyCurrentBehavior() {
assertEquals(j3.getString("hex6"), "0011");
}


@Test
public void testStrictModeJSONTokener_expectException(){
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
JSONTokener tokener = new JSONTokener("{\"key\":\"value\"}invalidCharacters", jsonParserConfiguration);

assertThrows(JSONException.class, () -> { new JSONObject(tokener); });
}

/**
* Method to build nested map of max maxDepth
*
Expand Down
Loading