diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 2bd80c1..a0934e6 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -88,6 +88,8 @@ public class JSONArray implements Iterable { /** * Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration. + * The supplied configuration replaces the tokener's configuration and applies + * to all values, including nested objects and arrays. * * @param x A JSONTokener instance from which the JSONArray is constructed. * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser. @@ -108,6 +110,7 @@ public class JSONArray implements Iterable { */ JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException { this(); + x.setJsonParserConfiguration(jsonParserConfiguration); if (x.nextClean() != '[') { throw x.syntaxError("A JSONArray text must start with '['"); diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 0b2268c..77571f7 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -204,6 +204,8 @@ public class JSONObject { /** * Construct a JSONObject from a JSONTokener with custom json parse configurations. + * The supplied configuration replaces the tokener's configuration and applies + * to all values, including nested objects and arrays. * * @param x * A JSONTokener object containing the source string. @@ -233,6 +235,7 @@ public class JSONObject { */ JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException { this(); + x.setJsonParserConfiguration(jsonParserConfiguration); if (x.nextClean() != '{') { throw x.syntaxError("A JSONObject text must begin with '{'"); diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java index 926c49f..cbe4447 100644 --- a/src/test/java/org/json/junit/JSONParserConfigurationTest.java +++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java @@ -16,6 +16,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -36,6 +37,56 @@ public class JSONParserConfigurationTest { assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); } + @Test + public void objectConstructorAppliesExplicitStrictModeToValues() { + JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode(); + JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false); + for (String source : Arrays.asList( + "{\"value\":'text'}", + "{\"value\":[1,]}", + "{\"value\":{\"nested\":1,}}")) { + assertThrows(JSONException.class, () -> new JSONObject(new JSONTokener(source, lenient), configuration)); + } + } + + @Test + public void arrayConstructorAppliesExplicitStrictModeToValues() { + JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode(); + JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false); + for (String source : Arrays.asList( + "['text']", + "[[1,]]", + "[{\"nested\":1,}]")) { + assertThrows(JSONException.class, () -> new JSONArray(new JSONTokener(source, lenient), configuration)); + } + } + + @Test + public void explicitConfigurationOverridesTokenerStrictMode() { + JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode(); + JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false); + JSONTokener objectTokener = new JSONTokener("{\"value\":[{'nested':'text',}]}", strict); + JSONTokener arrayTokener = new JSONTokener("[{\"value\":['text',]}]", strict); + + JSONObject object = new JSONObject(objectTokener, lenient); + JSONArray array = new JSONArray(arrayTokener, lenient); + + assertEquals("text", object.getJSONArray("value").getJSONObject(0).getString("nested")); + assertEquals("text", array.getJSONObject(0).getJSONArray("value").getString(0)); + assertSame(lenient, objectTokener.getJsonParserConfiguration()); + assertSame(lenient, arrayTokener.getJsonParserConfiguration()); + } + + @Test + public void explicitConfigurationAppliesToNestedDuplicateKeys() { + JSONParserConfiguration configuration = new JSONParserConfiguration().withOverwriteDuplicateKey(true); + JSONObject object = new JSONObject(new JSONTokener("{\"value\":[{\"key\":1,\"key\":2}]}"), configuration); + JSONArray array = new JSONArray(new JSONTokener("[{\"value\":{\"key\":1,\"key\":2}}]"), configuration); + + assertEquals(2, object.getJSONArray("value").getJSONObject(0).getInt("key")); + assertEquals(2, array.getJSONObject(0).getJSONObject("value").getInt("key")); + } + @Test public void strictModeIsCloned(){ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()