fix: restore tokener configuration after parsing
This commit is contained in:
@@ -88,8 +88,9 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
|
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
|
||||||
* The supplied configuration replaces the tokener's configuration and applies
|
* The supplied configuration applies to all values during construction,
|
||||||
* to all values, including nested objects and arrays.
|
* including nested objects and arrays. The tokener's original configuration
|
||||||
|
* is restored afterward, even if parsing fails.
|
||||||
*
|
*
|
||||||
* @param x A JSONTokener instance from which the JSONArray is constructed.
|
* @param x A JSONTokener instance from which the JSONArray is constructed.
|
||||||
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||||
@@ -110,8 +111,9 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
*/
|
*/
|
||||||
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
|
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
|
||||||
this();
|
this();
|
||||||
x.setJsonParserConfiguration(jsonParserConfiguration);
|
JSONParserConfiguration originalConfiguration = x.getJsonParserConfiguration();
|
||||||
|
x.setJsonParserConfigurationInternal(jsonParserConfiguration);
|
||||||
|
try {
|
||||||
if (x.nextClean() != '[') {
|
if (x.nextClean() != '[') {
|
||||||
throw x.syntaxError("A JSONArray text must start with '['");
|
throw x.syntaxError("A JSONArray text must start with '['");
|
||||||
}
|
}
|
||||||
@@ -140,6 +142,9 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
x.setJsonParserConfigurationInternal(originalConfiguration);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Convenience function. Checks for JSON syntax error.
|
/** Convenience function. Checks for JSON syntax error.
|
||||||
|
|||||||
@@ -204,8 +204,9 @@ public class JSONObject {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a JSONObject from a JSONTokener with custom json parse configurations.
|
* Construct a JSONObject from a JSONTokener with custom json parse configurations.
|
||||||
* The supplied configuration replaces the tokener's configuration and applies
|
* The supplied configuration applies to all values during construction,
|
||||||
* to all values, including nested objects and arrays.
|
* including nested objects and arrays. The tokener's original configuration
|
||||||
|
* is restored afterward, even if parsing fails.
|
||||||
*
|
*
|
||||||
* @param x
|
* @param x
|
||||||
* A JSONTokener object containing the source string.
|
* A JSONTokener object containing the source string.
|
||||||
@@ -235,8 +236,9 @@ public class JSONObject {
|
|||||||
*/
|
*/
|
||||||
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
|
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
|
||||||
this();
|
this();
|
||||||
x.setJsonParserConfiguration(jsonParserConfiguration);
|
JSONParserConfiguration originalConfiguration = x.getJsonParserConfiguration();
|
||||||
|
x.setJsonParserConfigurationInternal(jsonParserConfiguration);
|
||||||
|
try {
|
||||||
if (x.nextClean() != '{') {
|
if (x.nextClean() != '{') {
|
||||||
throw x.syntaxError("A JSONObject text must begin with '{'");
|
throw x.syntaxError("A JSONObject text must begin with '{'");
|
||||||
}
|
}
|
||||||
@@ -245,6 +247,9 @@ public class JSONObject {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
x.setJsonParserConfigurationInternal(originalConfiguration);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -117,6 +117,10 @@ public class JSONTokener {
|
|||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfiguration) {
|
public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfiguration) {
|
||||||
|
setJsonParserConfigurationInternal(jsonParserConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setJsonParserConfigurationInternal(JSONParserConfiguration jsonParserConfiguration) {
|
||||||
this.jsonParserConfiguration = jsonParserConfiguration;
|
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,10 +42,15 @@ public class JSONParserConfigurationTest {
|
|||||||
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
|
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
|
||||||
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
||||||
for (String source : Arrays.asList(
|
for (String source : Arrays.asList(
|
||||||
|
"[]",
|
||||||
|
"{",
|
||||||
|
"{} trailing",
|
||||||
"{\"value\":'text'}",
|
"{\"value\":'text'}",
|
||||||
"{\"value\":[1,]}",
|
"{\"value\":[1,]}",
|
||||||
"{\"value\":{\"nested\":1,}}")) {
|
"{\"value\":{\"nested\":1,}}")) {
|
||||||
assertThrows(JSONException.class, () -> new JSONObject(new JSONTokener(source, lenient), configuration));
|
JSONTokener tokener = new JSONTokener(source, lenient);
|
||||||
|
assertThrows(JSONException.class, () -> new JSONObject(tokener, configuration));
|
||||||
|
assertSame(lenient, tokener.getJsonParserConfiguration());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,10 +59,15 @@ public class JSONParserConfigurationTest {
|
|||||||
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
|
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
|
||||||
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
||||||
for (String source : Arrays.asList(
|
for (String source : Arrays.asList(
|
||||||
|
"{}",
|
||||||
|
"[",
|
||||||
|
"[] trailing",
|
||||||
"['text']",
|
"['text']",
|
||||||
"[[1,]]",
|
"[[1,]]",
|
||||||
"[{\"nested\":1,}]")) {
|
"[{\"nested\":1,}]")) {
|
||||||
assertThrows(JSONException.class, () -> new JSONArray(new JSONTokener(source, lenient), configuration));
|
JSONTokener tokener = new JSONTokener(source, lenient);
|
||||||
|
assertThrows(JSONException.class, () -> new JSONArray(tokener, configuration));
|
||||||
|
assertSame(lenient, tokener.getJsonParserConfiguration());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,16 +75,42 @@ public class JSONParserConfigurationTest {
|
|||||||
public void explicitConfigurationOverridesTokenerStrictMode() {
|
public void explicitConfigurationOverridesTokenerStrictMode() {
|
||||||
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
|
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
|
||||||
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
||||||
JSONTokener objectTokener = new JSONTokener("{\"value\":[{'nested':'text',}]}", strict);
|
JSONTokener objectTokener = new JSONTokener("{\"value\":[{'nested':'text',}], 'other':'text'} 'next'", strict);
|
||||||
JSONTokener arrayTokener = new JSONTokener("[{\"value\":['text',]}]", strict);
|
JSONTokener arrayTokener = new JSONTokener("[{\"value\":['text',]},'text'] 'next'", strict);
|
||||||
|
|
||||||
JSONObject object = new JSONObject(objectTokener, lenient);
|
JSONObject object = new JSONObject(objectTokener, lenient);
|
||||||
JSONArray array = new JSONArray(arrayTokener, lenient);
|
JSONArray array = new JSONArray(arrayTokener, lenient);
|
||||||
|
|
||||||
assertEquals("text", object.getJSONArray("value").getJSONObject(0).getString("nested"));
|
assertEquals("text", object.getJSONArray("value").getJSONObject(0).getString("nested"));
|
||||||
assertEquals("text", array.getJSONObject(0).getJSONArray("value").getString(0));
|
assertEquals("text", array.getJSONObject(0).getJSONArray("value").getString(0));
|
||||||
assertSame(lenient, objectTokener.getJsonParserConfiguration());
|
assertEquals("text", object.getString("other"));
|
||||||
assertSame(lenient, arrayTokener.getJsonParserConfiguration());
|
assertEquals("text", array.getString(1));
|
||||||
|
assertSame(strict, objectTokener.getJsonParserConfiguration());
|
||||||
|
assertSame(strict, arrayTokener.getJsonParserConfiguration());
|
||||||
|
assertThrows(JSONException.class, () -> objectTokener.nextValue());
|
||||||
|
assertThrows(JSONException.class, () -> arrayTokener.nextValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void objectConstructorRestoresTokenerConfiguration() {
|
||||||
|
JSONParserConfiguration original = new JSONParserConfiguration().withStrictMode(false);
|
||||||
|
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
|
||||||
|
for (String source : Arrays.asList("{}", "{\"value\":[{\"nested\":\"text\"}],\"other\":true}")) {
|
||||||
|
JSONTokener tokener = new JSONTokener(source, original);
|
||||||
|
new JSONObject(tokener, strict);
|
||||||
|
assertSame(original, tokener.getJsonParserConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void arrayConstructorRestoresTokenerConfiguration() {
|
||||||
|
JSONParserConfiguration original = new JSONParserConfiguration().withStrictMode(false);
|
||||||
|
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
|
||||||
|
for (String source : Arrays.asList("[]", "[{\"value\":[\"text\"]},true]")) {
|
||||||
|
JSONTokener tokener = new JSONTokener(source, original);
|
||||||
|
new JSONArray(tokener, strict);
|
||||||
|
assertSame(original, tokener.getJsonParserConfiguration());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user