mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-02 11:05:28 -04:00
restore-jsonparserconfiguration: Restore methods to be used for strict mode
This commit is contained in:
parent
2ee5bf13f4
commit
1f308db7c4
@ -83,11 +83,22 @@ public class JSONArray implements Iterable<Object> {
|
||||
* 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.
|
||||
*
|
||||
* @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
|
||||
@ -104,27 +115,28 @@ public class JSONArray implements Iterable<Object> {
|
||||
this.myArrayList.add(x.nextValue());
|
||||
}
|
||||
switch (x.nextClean()) {
|
||||
case 0:
|
||||
// array is unclosed. No ']' found, instead EOF
|
||||
throw x.syntaxError("Expected a ',' or ']'");
|
||||
case ',':
|
||||
nextChar = x.nextClean();
|
||||
if (nextChar == 0) {
|
||||
case 0:
|
||||
// array is unclosed. No ']' found, instead EOF
|
||||
throw x.syntaxError("Expected a ',' or ']'");
|
||||
}
|
||||
if (nextChar == ']') {
|
||||
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 ']':
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
break;
|
||||
case ']':
|
||||
return;
|
||||
default:
|
||||
throw x.syntaxError("Expected a ',' or ']'");
|
||||
default:
|
||||
throw x.syntaxError("Expected a ',' or ']'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -138,7 +150,22 @@ public class JSONArray implements Iterable<Object> {
|
||||
* If there is a syntax error.
|
||||
*/
|
||||
public JSONArray(String source) throws JSONException {
|
||||
this(new JSONTokener(source));
|
||||
this(source, new JSONParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a JSONArray from a source JSON text.
|
||||
*
|
||||
* @param source
|
||||
* A string that begins with <code>[</code> <small>(left
|
||||
* bracket)</small> and ends with <code>]</code>
|
||||
* <small>(right bracket)</small>.
|
||||
* @param jsonParserConfiguration the parser config object
|
||||
* @throws JSONException
|
||||
* If there is a syntax error.
|
||||
*/
|
||||
public JSONArray(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||
this(new JSONTokener(source), jsonParserConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,6 +17,12 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
||||
this.overwriteDuplicateKey = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This flag, when set to true, instructs the parser to enforce strict mode when parsing JSON text.
|
||||
* Garbage chars at the end of the doc, unquoted string, and single-quoted strings are all disallowed.
|
||||
*/
|
||||
private boolean strictMode;
|
||||
|
||||
@Override
|
||||
protected JSONParserConfiguration clone() {
|
||||
JSONParserConfiguration clone = new JSONParserConfiguration();
|
||||
@ -58,6 +64,23 @@ 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.
|
||||
@ -67,4 +90,17 @@ 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user