Merge pull request #1077 from Mahmoodifar/fix/parser-configuration-precedence
Apply explicit parser configuration to nested values
This commit is contained in:
@@ -88,6 +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 applies to all values during construction,
|
||||||
|
* 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.
|
||||||
@@ -108,34 +111,39 @@ 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();
|
||||||
|
JSONParserConfiguration originalConfiguration = x.getJsonParserConfiguration();
|
||||||
|
x.setJsonParserConfigurationInternal(jsonParserConfiguration);
|
||||||
|
try {
|
||||||
|
if (x.nextClean() != '[') {
|
||||||
|
throw x.syntaxError("A JSONArray text must start with '['");
|
||||||
|
}
|
||||||
|
|
||||||
if (x.nextClean() != '[') {
|
char nextChar = x.nextClean();
|
||||||
throw x.syntaxError("A JSONArray text must start with '['");
|
if (nextChar == 0) {
|
||||||
}
|
// array is unclosed. No ']' found, instead EOF
|
||||||
|
throw x.syntaxError("Expected a ',' or ']'");
|
||||||
char nextChar = x.nextClean();
|
} else if (nextChar==',' && jsonParserConfiguration.isStrictMode()) {
|
||||||
if (nextChar == 0) {
|
throw x.syntaxError("Array content starts with a ','");
|
||||||
// array is unclosed. No ']' found, instead EOF
|
}
|
||||||
throw x.syntaxError("Expected a ',' or ']'");
|
if (nextChar != ']') {
|
||||||
} else if (nextChar==',' && jsonParserConfiguration.isStrictMode()) {
|
x.back();
|
||||||
throw x.syntaxError("Array content starts with a ','");
|
for (;;) {
|
||||||
}
|
if (x.nextClean() == ',') {
|
||||||
if (nextChar != ']') {
|
x.back();
|
||||||
x.back();
|
this.myArrayList.add(JSONObject.NULL);
|
||||||
for (;;) {
|
} else {
|
||||||
if (x.nextClean() == ',') {
|
x.back();
|
||||||
x.back();
|
this.myArrayList.add(x.nextValue());
|
||||||
this.myArrayList.add(JSONObject.NULL);
|
}
|
||||||
} else {
|
if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
|
||||||
x.back();
|
}
|
||||||
this.myArrayList.add(x.nextValue());
|
} else {
|
||||||
|
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
|
||||||
|
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
||||||
}
|
}
|
||||||
if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
|
|
||||||
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
x.setJsonParserConfigurationInternal(originalConfiguration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -204,6 +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 applies to all values during construction,
|
||||||
|
* 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.
|
||||||
@@ -233,14 +236,19 @@ public class JSONObject {
|
|||||||
*/
|
*/
|
||||||
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
|
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
|
||||||
this();
|
this();
|
||||||
|
JSONParserConfiguration originalConfiguration = x.getJsonParserConfiguration();
|
||||||
if (x.nextClean() != '{') {
|
x.setJsonParserConfigurationInternal(jsonParserConfiguration);
|
||||||
throw x.syntaxError("A JSONObject text must begin with '{'");
|
try {
|
||||||
}
|
if (x.nextClean() != '{') {
|
||||||
for (;;) {
|
throw x.syntaxError("A JSONObject text must begin with '{'");
|
||||||
if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
for (;;) {
|
||||||
|
if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import java.util.stream.Collectors;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
import static org.junit.Assert.assertThrows;
|
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;
|
||||||
@@ -36,6 +37,92 @@ public class JSONParserConfigurationTest {
|
|||||||
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
|
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(
|
||||||
|
"[]",
|
||||||
|
"{",
|
||||||
|
"{} trailing",
|
||||||
|
"{\"value\":'text'}",
|
||||||
|
"{\"value\":[1,]}",
|
||||||
|
"{\"value\":{\"nested\":1,}}")) {
|
||||||
|
JSONTokener tokener = new JSONTokener(source, lenient);
|
||||||
|
assertThrows(JSONException.class, () -> new JSONObject(tokener, configuration));
|
||||||
|
assertSame(lenient, tokener.getJsonParserConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void arrayConstructorAppliesExplicitStrictModeToValues() {
|
||||||
|
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
|
||||||
|
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
||||||
|
for (String source : Arrays.asList(
|
||||||
|
"{}",
|
||||||
|
"[",
|
||||||
|
"[] trailing",
|
||||||
|
"['text']",
|
||||||
|
"[[1,]]",
|
||||||
|
"[{\"nested\":1,}]")) {
|
||||||
|
JSONTokener tokener = new JSONTokener(source, lenient);
|
||||||
|
assertThrows(JSONException.class, () -> new JSONArray(tokener, configuration));
|
||||||
|
assertSame(lenient, tokener.getJsonParserConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void explicitConfigurationOverridesTokenerStrictMode() {
|
||||||
|
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
|
||||||
|
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
||||||
|
JSONTokener objectTokener = new JSONTokener("{\"value\":[{'nested':'text',}], 'other':'text'} 'next'", strict);
|
||||||
|
JSONTokener arrayTokener = new JSONTokener("[{\"value\":['text',]},'text'] 'next'", 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));
|
||||||
|
assertEquals("text", object.getString("other"));
|
||||||
|
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
|
||||||
|
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
|
@Test
|
||||||
public void strictModeIsCloned(){
|
public void strictModeIsCloned(){
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
|||||||
Reference in New Issue
Block a user