Skip to content

Commit 22f8290

Browse files
authored
Merge pull request #948 from Simulant87/947-JSONTokener-configuration-ignored
use JSONParserConfiguration of JSONTokener in JSONObject and JSONArray constructor instead of creating a new one
2 parents 8b857da + 4c873a1 commit 22f8290

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/main/java/org/json/JSONArray.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public JSONArray() {
8383
* If there is a syntax error.
8484
*/
8585
public JSONArray(JSONTokener x) throws JSONException {
86-
this(x, new JSONParserConfiguration());
86+
this(x, x.getJsonParserConfiguration());
8787
}
8888

8989
/**

src/main/java/org/json/JSONObject.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public JSONObject(JSONObject jo, String ... names) {
195195
* duplicated key.
196196
*/
197197
public JSONObject(JSONTokener x) throws JSONException {
198-
this(x, new JSONParserConfiguration());
198+
this(x, x.getJsonParserConfiguration());
199199
}
200200

201201
/**

src/test/java/org/json/junit/JSONArrayTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.junit.Assert.assertFalse;
99
import static org.junit.Assert.assertNotNull;
1010
import static org.junit.Assert.assertNull;
11+
import static org.junit.Assert.assertThrows;
1112
import static org.junit.Assert.assertTrue;
1213
import static org.junit.Assert.fail;
1314

@@ -1509,6 +1510,14 @@ public void testRecursiveDepthArrayFor1001Levels() {
15091510
new JSONArray(array);
15101511
}
15111512

1513+
@Test
1514+
public void testStrictModeJSONTokener_expectException(){
1515+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
1516+
JSONTokener tokener = new JSONTokener("[\"value\"]invalidCharacters", jsonParserConfiguration);
1517+
1518+
assertThrows(JSONException.class, () -> { new JSONArray(tokener); });
1519+
}
1520+
15121521
public static ArrayList<Object> buildNestedArray(int maxDepth) {
15131522
if (maxDepth <= 0) {
15141523
return new ArrayList<>();

src/test/java/org/json/junit/JSONObjectTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -3853,6 +3853,15 @@ public void clarifyCurrentBehavior() {
38533853
assertEquals(j3.getString("hex6"), "0011");
38543854
}
38553855

3856+
3857+
@Test
3858+
public void testStrictModeJSONTokener_expectException(){
3859+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
3860+
JSONTokener tokener = new JSONTokener("{\"key\":\"value\"}invalidCharacters", jsonParserConfiguration);
3861+
3862+
assertThrows(JSONException.class, () -> { new JSONObject(tokener); });
3863+
}
3864+
38563865
/**
38573866
* Method to build nested map of max maxDepth
38583867
*

0 commit comments

Comments
 (0)