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
This commit is contained in:
Sean Leary 2025-01-19 09:09:42 -06:00 committed by GitHub
commit 22f8290840
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 2 deletions

View File

@ -83,7 +83,7 @@ public class JSONArray implements Iterable<Object> {
* If there is a syntax error. * If there is a syntax error.
*/ */
public JSONArray(JSONTokener x) throws JSONException { public JSONArray(JSONTokener x) throws JSONException {
this(x, new JSONParserConfiguration()); this(x, x.getJsonParserConfiguration());
} }
/** /**

View File

@ -195,7 +195,7 @@ public class JSONObject {
* duplicated key. * duplicated key.
*/ */
public JSONObject(JSONTokener x) throws JSONException { public JSONObject(JSONTokener x) throws JSONException {
this(x, new JSONParserConfiguration()); this(x, x.getJsonParserConfiguration());
} }
/** /**

View File

@ -8,6 +8,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -1509,6 +1510,14 @@ public class JSONArrayTest {
new JSONArray(array); 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) { public static ArrayList<Object> buildNestedArray(int maxDepth) {
if (maxDepth <= 0) { if (maxDepth <= 0) {
return new ArrayList<>(); return new ArrayList<>();

View File

@ -3853,6 +3853,15 @@ public class JSONObjectTest {
assertEquals(j3.getString("hex6"), "0011"); 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 * Method to build nested map of max maxDepth
* *