Revert "Merge pull request #877 from rikkarth/feat/871-strictMode"

This reverts commit d02ac0f2a3, reversing
changes made to cfd47615d0.
This commit is contained in:
Sean Leary
2024-11-03 09:50:08 -06:00
parent 61dc2644d8
commit 215ec9bb9c
6 changed files with 109 additions and 764 deletions

View File

@@ -75,31 +75,19 @@ public class JSONArray implements Iterable<Object> {
}
/**
* Constructs a JSONArray from a JSONTokener.
* <p>
* This constructor reads the JSONTokener to parse a JSON array. It uses the default JSONParserConfiguration.
* Construct a JSONArray from a JSONTokener.
*
* @param x A JSONTokener
* @throws JSONException If there is a syntax error.
* @param x
* A JSONTokener
* @throws JSONException
* If there is a syntax error.
*/
public JSONArray(JSONTokener x) throws JSONException {
this(x, new JSONParserConfiguration());
}
/**
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
* JSONParserConfiguration contains strictMode turned off (false) by default.
*
* @param x A JSONTokener instance from which the JSONArray is constructed.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
*/
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this();
if (x.nextClean() != '[') {
throw x.syntaxError("A JSONArray text must start with '['");
}
char nextChar = x.nextClean();
if (nextChar == 0) {
// array is unclosed. No ']' found, instead EOF
@@ -113,34 +101,27 @@ public class JSONArray implements Iterable<Object> {
this.myArrayList.add(JSONObject.NULL);
} else {
x.back();
this.myArrayList.add(x.nextValue(jsonParserConfiguration));
this.myArrayList.add(x.nextValue());
}
switch (x.nextClean()) {
case 0:
case 0:
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
case ',':
nextChar = x.nextClean();
if (nextChar == 0) {
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
case ',':
nextChar = x.nextClean();
if (nextChar == 0) {
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
}
if (nextChar == ']') {
return;
}
x.back();
break;
case ']':
if (jsonParserConfiguration.isStrictMode()) {
nextChar = x.nextClean();
if (nextChar != 0) {
throw x.syntaxError("invalid character found after end of array: " + nextChar);
}
}
}
if (nextChar == ']') {
return;
default:
throw x.syntaxError("Expected a ',' or ']'");
}
x.back();
break;
case ']':
return;
default:
throw x.syntaxError("Expected a ',' or ']'");
}
}
}
@@ -157,19 +138,7 @@ public class JSONArray implements Iterable<Object> {
* If there is a syntax error.
*/
public JSONArray(String source) throws JSONException {
this(new JSONTokener(source), new JSONParserConfiguration());
}
/**
* Constructs a JSONArray from a source JSON text and a JSONParserConfiguration.
*
* @param source A string that begins with <code>[</code>&nbsp;<small>(left bracket)</small> and
* ends with <code>]</code> &nbsp;<small>(right bracket)</small>.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
* @throws JSONException If there is a syntax error.
*/
public JSONArray(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this(new JSONTokener(source), jsonParserConfiguration);
this(new JSONTokener(source));
}
/**
@@ -398,7 +367,7 @@ public class JSONArray implements Iterable<Object> {
/**
* Get the enum value associated with an index.
*
*
* @param <E>
* Enum Type
* @param clazz
@@ -586,7 +555,7 @@ public class JSONArray implements Iterable<Object> {
if (len == 0) {
return "";
}
StringBuilder sb = new StringBuilder(
JSONObject.valueToString(this.myArrayList.get(0)));
@@ -900,7 +869,7 @@ public class JSONArray implements Iterable<Object> {
/**
* Get the enum value associated with a key.
*
*
* @param <E>
* Enum Type
* @param clazz
@@ -915,7 +884,7 @@ public class JSONArray implements Iterable<Object> {
/**
* Get the enum value associated with a key.
*
*
* @param <E>
* Enum Type
* @param clazz
@@ -948,8 +917,8 @@ public class JSONArray implements Iterable<Object> {
}
/**
* Get the optional BigInteger value associated with an index. The
* defaultValue is returned if there is no value for the index, or if the
* Get the optional BigInteger value associated with an index. The
* defaultValue is returned if there is no value for the index, or if the
* value is not a number and cannot be converted to a number.
*
* @param index
@@ -964,8 +933,8 @@ public class JSONArray implements Iterable<Object> {
}
/**
* Get the optional BigDecimal value associated with an index. The
* defaultValue is returned if there is no value for the index, or if the
* Get the optional BigDecimal value associated with an index. The
* defaultValue is returned if there is no value for the index, or if the
* value is not a number and cannot be converted to a number. If the value
* is float or double, the {@link BigDecimal#BigDecimal(double)}
* constructor will be used. See notes on the constructor for conversion
@@ -1134,7 +1103,7 @@ public class JSONArray implements Iterable<Object> {
if (val instanceof Number){
return (Number) val;
}
if (val instanceof String) {
try {
return JSONObject.stringToNumber((String) val);
@@ -1211,7 +1180,7 @@ public class JSONArray implements Iterable<Object> {
public JSONArray put(double value) throws JSONException {
return this.put(Double.valueOf(value));
}
/**
* Append a float value. This increases the array's length by one.
*
@@ -1466,19 +1435,19 @@ public class JSONArray implements Iterable<Object> {
*
* @param collection
* A Collection.
* @return this.
* @return this.
*/
public JSONArray putAll(Collection<?> collection) {
this.addAll(collection, false);
return this;
}
/**
* Put an Iterable's elements in to the JSONArray.
*
* @param iter
* An Iterable.
* @return this.
* @return this.
*/
public JSONArray putAll(Iterable<?> iter) {
this.addAll(iter, false);
@@ -1490,7 +1459,7 @@ public class JSONArray implements Iterable<Object> {
*
* @param array
* A JSONArray.
* @return this.
* @return this.
*/
public JSONArray putAll(JSONArray array) {
// directly copy the elements from the source array to this one
@@ -1505,7 +1474,7 @@ public class JSONArray implements Iterable<Object> {
* @param array
* Array. If the parameter passed is null, or not an array or Iterable, an
* exception will be thrown.
* @return this.
* @return this.
*
* @throws JSONException
* If not an array, JSONArray, Iterable or if an value is non-finite number.
@@ -1516,9 +1485,9 @@ public class JSONArray implements Iterable<Object> {
this.addAll(array, false);
return this;
}
/**
* Creates a JSONPointer using an initialization string and tries to
* Creates a JSONPointer using an initialization string and tries to
* match it to an item within this JSONArray. For example, given a
* JSONArray initialized with this document:
* <pre>
@@ -1526,7 +1495,7 @@ public class JSONArray implements Iterable<Object> {
* {"b":"c"}
* ]
* </pre>
* and this JSONPointer string:
* and this JSONPointer string:
* <pre>
* "/0/b"
* </pre>
@@ -1539,9 +1508,9 @@ public class JSONArray implements Iterable<Object> {
public Object query(String jsonPointer) {
return query(new JSONPointer(jsonPointer));
}
/**
* Uses a user initialized JSONPointer and tries to
* Uses a user initialized JSONPointer and tries to
* match it to an item within this JSONArray. For example, given a
* JSONArray initialized with this document:
* <pre>
@@ -1549,7 +1518,7 @@ public class JSONArray implements Iterable<Object> {
* {"b":"c"}
* ]
* </pre>
* and this JSONPointer:
* and this JSONPointer:
* <pre>
* "/0/b"
* </pre>
@@ -1562,11 +1531,11 @@ public class JSONArray implements Iterable<Object> {
public Object query(JSONPointer jsonPointer) {
return jsonPointer.queryFrom(this);
}
/**
* Queries and returns a value from this object using {@code jsonPointer}, or
* returns null if the query fails due to a missing key.
*
*
* @param jsonPointer the string representation of the JSON pointer
* @return the queried value or {@code null}
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
@@ -1574,11 +1543,11 @@ public class JSONArray implements Iterable<Object> {
public Object optQuery(String jsonPointer) {
return optQuery(new JSONPointer(jsonPointer));
}
/**
* Queries and returns a value from this object using {@code jsonPointer}, or
* returns null if the query fails due to a missing key.
*
*
* @param jsonPointer The JSON pointer
* @return the queried value or {@code null}
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
@@ -1698,11 +1667,11 @@ public class JSONArray implements Iterable<Object> {
/**
* Make a pretty-printed JSON text of this JSONArray.
*
*
* <p>If <pre> {@code indentFactor > 0}</pre> and the {@link JSONArray} has only
* one element, then the array will be output on a single line:
* <pre>{@code [1]}</pre>
*
*
* <p>If an array has 2 or more elements, then it will be output across
* multiple lines: <pre>{@code
* [
@@ -1714,7 +1683,7 @@ public class JSONArray implements Iterable<Object> {
* <p><b>
* Warning: This method assumes that the data structure is acyclical.
* </b>
*
*
* @param indentFactor
* The number of spaces to add to each level of indentation.
* @return a printable, displayable, transmittable representation of the
@@ -1748,11 +1717,11 @@ public class JSONArray implements Iterable<Object> {
/**
* Write the contents of the JSONArray as JSON text to a writer.
*
*
* <p>If <pre>{@code indentFactor > 0}</pre> and the {@link JSONArray} has only
* one element, then the array will be output on a single line:
* <pre>{@code [1]}</pre>
*
*
* <p>If an array has 2 or more elements, then it will be output across
* multiple lines: <pre>{@code
* [
@@ -1978,7 +1947,7 @@ public class JSONArray implements Iterable<Object> {
"JSONArray initial value should be a string or collection or array.");
}
}
/**
* Create a new JSONException in a common format for incorrect conversions.
* @param idx index of the item

View File

@@ -220,12 +220,12 @@ public class JSONObject {
for (;;) {
c = x.nextClean();
switch (c) {
case 0:
throw x.syntaxError("A JSONObject text must end with '}'");
case '}':
return;
default:
key = x.nextSimpleValue(c, jsonParserConfiguration).toString();
case 0:
throw x.syntaxError("A JSONObject text must end with '}'");
case '}':
return;
default:
key = x.nextSimpleValue(c).toString();
}
// The key is followed by ':'.
@@ -244,7 +244,7 @@ public class JSONObject {
throw x.syntaxError("Duplicate key \"" + key + "\"");
}
Object value = x.nextValue(jsonParserConfiguration);
Object value = x.nextValue();
// Only add value if non-null
if (value != null) {
this.put(key, value);
@@ -1247,7 +1247,7 @@ public class JSONObject {
static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
return objectToBigDecimal(val, defaultValue, true);
}
/**
* @param val value to convert
* @param defaultValue default value to return is the conversion doesn't work or is null.

View File

@@ -4,25 +4,11 @@ package org.json;
* Configuration object for the JSON parser. The configuration is immutable.
*/
public class JSONParserConfiguration extends ParserConfiguration {
/** Original Configuration of the JSON Parser. */
public static final JSONParserConfiguration ORIGINAL = new JSONParserConfiguration();
/** Original configuration of the JSON Parser except that values are kept as strings. */
public static final JSONParserConfiguration KEEP_STRINGS = new JSONParserConfiguration().withKeepStrings(true);
/**
* Used to indicate whether to overwrite duplicate key or not.
*/
private boolean overwriteDuplicateKey;
/**
* This flag, when set to true, instructs the parser to throw a JSONException if it encounters an invalid character
* immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
* JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
*/
private boolean strictMode;
/**
* Configuration with the default values.
*/
@@ -72,24 +58,6 @@ public class JSONParserConfiguration extends ParserConfiguration {
return clone;
}
/**
* Sets the strict mode configuration for the JSON parser.
* <p>
* When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character
* immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
* JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
*
* @param mode a boolean value indicating whether strict mode should be enabled or not
* @return a new JSONParserConfiguration instance with the updated strict mode setting
*/
public JSONParserConfiguration withStrictMode(final boolean mode) {
JSONParserConfiguration clone = this.clone();
clone.strictMode = mode;
return clone;
}
/**
* The parser's behavior when meeting duplicate keys, controls whether the parser should
* overwrite duplicate keys or not.
@@ -99,18 +67,4 @@ public class JSONParserConfiguration extends ParserConfiguration {
public boolean isOverwriteDuplicateKey() {
return this.overwriteDuplicateKey;
}
/**
* Retrieves the current strict mode setting of the JSON parser.
* <p>
* Strict mode, when enabled, instructs the parser to throw a JSONException if it encounters an invalid character
* immediately following the final ']' character in the input. This ensures strict adherence to the JSON syntax, as
* any characters after the final closing bracket of a JSON array are considered invalid.
*
* @return the current strict mode setting. True if strict mode is enabled, false otherwise.
*/
public boolean isStrictMode() {
return this.strictMode;
}
}

View File

@@ -284,14 +284,13 @@ public class JSONTokener {
* Backslash processing is done. The formal JSON format does not
* allow strings in single quotes, but an implementation is allowed to
* accept them.
* If strictMode is true, this implementation will not accept unbalanced quotes (e.g will not accept <code>"test'</code>)
* @param quote The quoting character, either
* <code>"</code>&nbsp;<small>(double quote)</small> or
* <code>'</code>&nbsp;<small>(single quote)</small>.
* @return A String.
* @throws JSONException Unterminated string or unbalanced quotes if strictMode == true.
* @return A String.
* @throws JSONException Unterminated string.
*/
public String nextString(char quote, boolean strictMode) throws JSONException {
public String nextString(char quote) throws JSONException {
char c;
StringBuilder sb = new StringBuilder();
for (;;) {
@@ -339,21 +338,11 @@ public class JSONTokener {
throw this.syntaxError("Illegal escape. Escape sequence \\" + c + " is not valid.");
}
break;
default:
if (strictMode && c == '\"' && quote != c) {
throw this.syntaxError(String.format(
"Field contains unbalanced quotes. Starts with %s but ends with double quote.", quote));
}
if (strictMode && c == '\'' && quote != c) {
throw this.syntaxError(String.format(
"Field contains unbalanced quotes. Starts with %s but ends with single quote.", quote));
}
if (c == quote) {
return sb.toString();
}
sb.append(c);
default:
if (c == quote) {
return sb.toString();
}
sb.append(c);
}
}
}
@@ -408,103 +397,51 @@ public class JSONTokener {
/**
* Get the next value. The value can be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
* JSONObject.NULL object.
* Get the next value. The value can be a Boolean, Double, Integer,
* JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
* @throws JSONException If syntax error.
*
* @return An object.
* @throws JSONException If syntax error.
*/
public Object nextValue() throws JSONException {
return nextValue(new JSONParserConfiguration());
}
/**
* Get the next value. The value can be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
* JSONObject.NULL object. The strictMode parameter controls the behavior of the method when parsing the value.
*
* @param jsonParserConfiguration which carries options such as strictMode, these methods will
* strictly adhere to the JSON syntax, throwing a JSONException for any deviations.
* @return An object.
* @throws JSONException If syntax error.
*/
public Object nextValue(JSONParserConfiguration jsonParserConfiguration) throws JSONException {
char c = this.nextClean();
switch (c) {
case '{':
this.back();
try {
return new JSONObject(this, jsonParserConfiguration);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
case '[':
this.back();
try {
return new JSONArray(this);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
default:
return nextSimpleValue(c, jsonParserConfiguration);
case '{':
this.back();
try {
return new JSONObject(this);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
case '[':
this.back();
try {
return new JSONArray(this);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
}
return nextSimpleValue(c);
}
/**
* This method is used to get a JSONObject from the JSONTokener. The strictMode parameter controls the behavior of
* the method when parsing the JSONObject.
*
* @param jsonParserConfiguration which carries options such as strictMode, these methods will
* strictly adhere to the JSON syntax, throwing a JSONException for any deviations.
* deviations.
* @return A JSONObject which is the next value in the JSONTokener.
* @throws JSONException If the JSONObject or JSONArray depth is too large to process.
*/
private JSONObject getJsonObject(JSONParserConfiguration jsonParserConfiguration) {
try {
return new JSONObject(this, jsonParserConfiguration);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
}
Object nextSimpleValue(char c) {
String string;
/**
* This method is used to get a JSONArray from the JSONTokener.
*
* @return A JSONArray which is the next value in the JSONTokener.
* @throws JSONException If the JSONArray depth is too large to process.
*/
private JSONArray getJsonArray() {
try {
return new JSONArray(this);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
}
Object nextSimpleValue(char c, JSONParserConfiguration jsonParserConfiguration) {
boolean strictMode = jsonParserConfiguration.isStrictMode();
if(strictMode && c == '\''){
throw this.syntaxError("Single quote wrap not allowed in strict mode");
switch (c) {
case '"':
case '\'':
return this.nextString(c);
}
if (c == '"' || c == '\'') {
return this.nextString(c, strictMode);
}
/*
* Handle unquoted text. This could be the values true, false, or
* null, or it can be a number. An implementation (such as this one)
* is allowed to also accept non-standard forms.
*
* Accumulate characters until we reach the end of the text or a
* formatting character.
*/
return parsedUnquotedText(c, strictMode);
}
/**
* Parses unquoted text from the JSON input. This could be the values true, false, or null, or it can be a number.
* Non-standard forms are also accepted. Characters are accumulated until the end of the text or a formatting
* character is reached.
*
* @param c The starting character.
* @return The parsed object.
* @throws JSONException If the parsed string is empty.
*/
private Object parsedUnquotedText(char c, boolean strictMode) {
StringBuilder sb = new StringBuilder();
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
sb.append(c);
@@ -514,37 +451,13 @@ public class JSONTokener {
this.back();
}
String string = sb.toString().trim();
if (strictMode) {
boolean isBooleanOrNumeric = checkIfValueIsBooleanOrNumeric(string);
if (isBooleanOrNumeric) {
return string;
}
throw new JSONException(String.format("Value is not surrounded by quotes: %s", string));
}
if (string.isEmpty()) {
string = sb.toString().trim();
if ("".equals(string)) {
throw this.syntaxError("Missing value");
}
return JSONObject.stringToValue(string);
}
private boolean checkIfValueIsBooleanOrNumeric(Object valueToValidate) {
String stringToValidate = valueToValidate.toString();
if (stringToValidate.equals("true") || stringToValidate.equals("false")) {
return true;
}
try {
Double.parseDouble(stringToValidate);
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* Skip characters until the next character is the requested character.