Skip to content

Commit 9f3b527

Browse files
committed
fix: restore tokener configuration after parsing
1 parent 0ae8d5a commit 9f3b527

4 files changed

Lines changed: 94 additions & 44 deletions

File tree

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

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

8989
/**
9090
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
91-
* The supplied configuration replaces the tokener's configuration and applies
92-
* to all values, including nested objects and arrays.
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.
9394
*
9495
* @param x A JSONTokener instance from which the JSONArray is constructed.
9596
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
@@ -110,35 +111,39 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
110111
*/
111112
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
112113
this();
113-
x.setJsonParserConfiguration(jsonParserConfiguration);
114-
115-
if (x.nextClean() != '[') {
116-
throw x.syntaxError("A JSONArray text must start with '['");
117-
}
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+
}
118120

119-
char nextChar = x.nextClean();
120-
if (nextChar == 0) {
121-
// array is unclosed. No ']' found, instead EOF
122-
throw x.syntaxError("Expected a ',' or ']'");
123-
} else if (nextChar==',' && jsonParserConfiguration.isStrictMode()) {
124-
throw x.syntaxError("Array content starts with a ','");
125-
}
126-
if (nextChar != ']') {
127-
x.back();
128-
for (;;) {
129-
if (x.nextClean() == ',') {
130-
x.back();
131-
this.myArrayList.add(JSONObject.NULL);
132-
} else {
133-
x.back();
134-
this.myArrayList.add(x.nextValue());
135-
}
136-
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 ','");
137127
}
138-
} else {
139-
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
140-
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+
}
141144
}
145+
} finally {
146+
x.setJsonParserConfigurationInternal(originalConfiguration);
142147
}
143148
}
144149

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +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 replaces the tokener's configuration and applies
208-
* to all values, including nested objects and arrays.
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.
209210
*
210211
* @param x
211212
* A JSONTokener object containing the source string.
@@ -235,15 +236,19 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
235236
*/
236237
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
237238
this();
238-
x.setJsonParserConfiguration(jsonParserConfiguration);
239-
240-
if (x.nextClean() != '{') {
241-
throw x.syntaxError("A JSONObject text must begin with '{'");
242-
}
243-
for (;;) {
244-
if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
245-
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+
}
246249
}
250+
} finally {
251+
x.setJsonParserConfigurationInternal(originalConfiguration);
247252
}
248253
}
249254

‎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: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ public void objectConstructorAppliesExplicitStrictModeToValues() {
4242
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
4343
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
4444
for (String source : Arrays.asList(
45+
"[]",
46+
"{",
47+
"{} trailing",
4548
"{\"value\":'text'}",
4649
"{\"value\":[1,]}",
4750
"{\"value\":{\"nested\":1,}}")) {
48-
assertThrows(JSONException.class, () -> new JSONObject(new JSONTokener(source, lenient), configuration));
51+
JSONTokener tokener = new JSONTokener(source, lenient);
52+
assertThrows(JSONException.class, () -> new JSONObject(tokener, configuration));
53+
assertSame(lenient, tokener.getJsonParserConfiguration());
4954
}
5055
}
5156

@@ -54,27 +59,58 @@ public void arrayConstructorAppliesExplicitStrictModeToValues() {
5459
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
5560
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
5661
for (String source : Arrays.asList(
62+
"{}",
63+
"[",
64+
"[] trailing",
5765
"['text']",
5866
"[[1,]]",
5967
"[{\"nested\":1,}]")) {
60-
assertThrows(JSONException.class, () -> new JSONArray(new JSONTokener(source, lenient), configuration));
68+
JSONTokener tokener = new JSONTokener(source, lenient);
69+
assertThrows(JSONException.class, () -> new JSONArray(tokener, configuration));
70+
assertSame(lenient, tokener.getJsonParserConfiguration());
6171
}
6272
}
6373

6474
@Test
6575
public void explicitConfigurationOverridesTokenerStrictMode() {
6676
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
6777
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
68-
JSONTokener objectTokener = new JSONTokener("{\"value\":[{'nested':'text',}]}", strict);
69-
JSONTokener arrayTokener = new JSONTokener("[{\"value\":['text',]}]", strict);
78+
JSONTokener objectTokener = new JSONTokener("{\"value\":[{'nested':'text',}], 'other':'text'} 'next'", strict);
79+
JSONTokener arrayTokener = new JSONTokener("[{\"value\":['text',]},'text'] 'next'", strict);
7080

7181
JSONObject object = new JSONObject(objectTokener, lenient);
7282
JSONArray array = new JSONArray(arrayTokener, lenient);
7383

7484
assertEquals("text", object.getJSONArray("value").getJSONObject(0).getString("nested"));
7585
assertEquals("text", array.getJSONObject(0).getJSONArray("value").getString(0));
76-
assertSame(lenient, objectTokener.getJsonParserConfiguration());
77-
assertSame(lenient, arrayTokener.getJsonParserConfiguration());
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+
}
78114
}
79115

80116
@Test

0 commit comments

Comments
 (0)