Skip to content

Commit 4f822e0

Browse files
authored
Merge pull request #1077 from Mahmoodifar/fix/parser-configuration-precedence
Apply explicit parser configuration to nested values
2 parents 8746735 + 9f3b527 commit 4f822e0

4 files changed

Lines changed: 139 additions & 32 deletions

File tree

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

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public JSONArray(JSONTokener x) throws JSONException {
8888

8989
/**
9090
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
91+
* The supplied configuration applies to all values during construction,
92+
* including nested objects and arrays. The tokener's original configuration
93+
* is restored afterward, even if parsing fails.
9194
*
9295
* @param x A JSONTokener instance from which the JSONArray is constructed.
9396
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
@@ -108,34 +111,39 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
108111
*/
109112
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
110113
this();
114+
JSONParserConfiguration originalConfiguration = x.getJsonParserConfiguration();
115+
x.setJsonParserConfigurationInternal(jsonParserConfiguration);
116+
try {
117+
if (x.nextClean() != '[') {
118+
throw x.syntaxError("A JSONArray text must start with '['");
119+
}
111120

112-
if (x.nextClean() != '[') {
113-
throw x.syntaxError("A JSONArray text must start with '['");
114-
}
115-
116-
char nextChar = x.nextClean();
117-
if (nextChar == 0) {
118-
// array is unclosed. No ']' found, instead EOF
119-
throw x.syntaxError("Expected a ',' or ']'");
120-
} else if (nextChar==',' && jsonParserConfiguration.isStrictMode()) {
121-
throw x.syntaxError("Array content starts with a ','");
122-
}
123-
if (nextChar != ']') {
124-
x.back();
125-
for (;;) {
126-
if (x.nextClean() == ',') {
127-
x.back();
128-
this.myArrayList.add(JSONObject.NULL);
129-
} else {
130-
x.back();
131-
this.myArrayList.add(x.nextValue());
132-
}
133-
if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
121+
char nextChar = x.nextClean();
122+
if (nextChar == 0) {
123+
// array is unclosed. No ']' found, instead EOF
124+
throw x.syntaxError("Expected a ',' or ']'");
125+
} else if (nextChar==',' && jsonParserConfiguration.isStrictMode()) {
126+
throw x.syntaxError("Array content starts with a ','");
134127
}
135-
} else {
136-
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
137-
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
128+
if (nextChar != ']') {
129+
x.back();
130+
for (;;) {
131+
if (x.nextClean() == ',') {
132+
x.back();
133+
this.myArrayList.add(JSONObject.NULL);
134+
} else {
135+
x.back();
136+
this.myArrayList.add(x.nextValue());
137+
}
138+
if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
139+
}
140+
} else {
141+
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
142+
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
143+
}
138144
}
145+
} finally {
146+
x.setJsonParserConfigurationInternal(originalConfiguration);
139147
}
140148
}
141149

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ public JSONObject(JSONTokener x) throws JSONException {
204204

205205
/**
206206
* Construct a JSONObject from a JSONTokener with custom json parse configurations.
207+
* The supplied configuration applies to all values during construction,
208+
* including nested objects and arrays. The tokener's original configuration
209+
* is restored afterward, even if parsing fails.
207210
*
208211
* @param x
209212
* A JSONTokener object containing the source string.
@@ -233,14 +236,19 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
233236
*/
234237
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
235238
this();
236-
237-
if (x.nextClean() != '{') {
238-
throw x.syntaxError("A JSONObject text must begin with '{'");
239-
}
240-
for (;;) {
241-
if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
242-
return;
239+
JSONParserConfiguration originalConfiguration = x.getJsonParserConfiguration();
240+
x.setJsonParserConfigurationInternal(jsonParserConfiguration);
241+
try {
242+
if (x.nextClean() != '{') {
243+
throw x.syntaxError("A JSONObject text must begin with '{'");
244+
}
245+
for (;;) {
246+
if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
247+
return;
248+
}
243249
}
250+
} finally {
251+
x.setJsonParserConfigurationInternal(originalConfiguration);
244252
}
245253
}
246254

‎src/main/java/org/json/JSONTokener.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public JSONParserConfiguration getJsonParserConfiguration() {
117117
*/
118118
@Deprecated
119119
public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfiguration) {
120+
setJsonParserConfigurationInternal(jsonParserConfiguration);
121+
}
122+
123+
void setJsonParserConfigurationInternal(JSONParserConfiguration jsonParserConfiguration) {
120124
this.jsonParserConfiguration = jsonParserConfiguration;
121125
}
122126

‎src/test/java/org/json/junit/JSONParserConfigurationTest.java‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.stream.Stream;
1717

1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertSame;
1920
import static org.junit.Assert.assertThrows;
2021
import static org.junit.Assert.assertTrue;
2122
import static org.junit.Assert.fail;
@@ -36,6 +37,92 @@ public void testOverwrite() {
3637
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
3738
}
3839

40+
@Test
41+
public void objectConstructorAppliesExplicitStrictModeToValues() {
42+
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
43+
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
44+
for (String source : Arrays.asList(
45+
"[]",
46+
"{",
47+
"{} trailing",
48+
"{\"value\":'text'}",
49+
"{\"value\":[1,]}",
50+
"{\"value\":{\"nested\":1,}}")) {
51+
JSONTokener tokener = new JSONTokener(source, lenient);
52+
assertThrows(JSONException.class, () -> new JSONObject(tokener, configuration));
53+
assertSame(lenient, tokener.getJsonParserConfiguration());
54+
}
55+
}
56+
57+
@Test
58+
public void arrayConstructorAppliesExplicitStrictModeToValues() {
59+
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
60+
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
61+
for (String source : Arrays.asList(
62+
"{}",
63+
"[",
64+
"[] trailing",
65+
"['text']",
66+
"[[1,]]",
67+
"[{\"nested\":1,}]")) {
68+
JSONTokener tokener = new JSONTokener(source, lenient);
69+
assertThrows(JSONException.class, () -> new JSONArray(tokener, configuration));
70+
assertSame(lenient, tokener.getJsonParserConfiguration());
71+
}
72+
}
73+
74+
@Test
75+
public void explicitConfigurationOverridesTokenerStrictMode() {
76+
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
77+
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
78+
JSONTokener objectTokener = new JSONTokener("{\"value\":[{'nested':'text',}], 'other':'text'} 'next'", strict);
79+
JSONTokener arrayTokener = new JSONTokener("[{\"value\":['text',]},'text'] 'next'", strict);
80+
81+
JSONObject object = new JSONObject(objectTokener, lenient);
82+
JSONArray array = new JSONArray(arrayTokener, lenient);
83+
84+
assertEquals("text", object.getJSONArray("value").getJSONObject(0).getString("nested"));
85+
assertEquals("text", array.getJSONObject(0).getJSONArray("value").getString(0));
86+
assertEquals("text", object.getString("other"));
87+
assertEquals("text", array.getString(1));
88+
assertSame(strict, objectTokener.getJsonParserConfiguration());
89+
assertSame(strict, arrayTokener.getJsonParserConfiguration());
90+
assertThrows(JSONException.class, () -> objectTokener.nextValue());
91+
assertThrows(JSONException.class, () -> arrayTokener.nextValue());
92+
}
93+
94+
@Test
95+
public void objectConstructorRestoresTokenerConfiguration() {
96+
JSONParserConfiguration original = new JSONParserConfiguration().withStrictMode(false);
97+
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
98+
for (String source : Arrays.asList("{}", "{\"value\":[{\"nested\":\"text\"}],\"other\":true}")) {
99+
JSONTokener tokener = new JSONTokener(source, original);
100+
new JSONObject(tokener, strict);
101+
assertSame(original, tokener.getJsonParserConfiguration());
102+
}
103+
}
104+
105+
@Test
106+
public void arrayConstructorRestoresTokenerConfiguration() {
107+
JSONParserConfiguration original = new JSONParserConfiguration().withStrictMode(false);
108+
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
109+
for (String source : Arrays.asList("[]", "[{\"value\":[\"text\"]},true]")) {
110+
JSONTokener tokener = new JSONTokener(source, original);
111+
new JSONArray(tokener, strict);
112+
assertSame(original, tokener.getJsonParserConfiguration());
113+
}
114+
}
115+
116+
@Test
117+
public void explicitConfigurationAppliesToNestedDuplicateKeys() {
118+
JSONParserConfiguration configuration = new JSONParserConfiguration().withOverwriteDuplicateKey(true);
119+
JSONObject object = new JSONObject(new JSONTokener("{\"value\":[{\"key\":1,\"key\":2}]}"), configuration);
120+
JSONArray array = new JSONArray(new JSONTokener("[{\"value\":{\"key\":1,\"key\":2}}]"), configuration);
121+
122+
assertEquals(2, object.getJSONArray("value").getJSONObject(0).getInt("key"));
123+
assertEquals(2, array.getJSONObject(0).getJSONObject("value").getInt("key"));
124+
}
125+
39126
@Test
40127
public void strictModeIsCloned(){
41128
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()

0 commit comments

Comments
 (0)