diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index bdad42753..e2725b7a9 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -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()); } /** diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 7ec1b2cc6..d50fff73b 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -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()); } /** diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index e6d71479e..584e47cd0 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -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; @@ -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 buildNestedArray(int maxDepth) { if (maxDepth <= 0) { return new ArrayList<>(); diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 7f121f3c5..889113d26 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -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 *