mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 11:25:30 -04:00
restore-jsonparserconfiguration: Restore methods to be used for strict mode
This commit is contained in:
parent
2ee5bf13f4
commit
1f308db7c4
@ -83,6 +83,17 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* If there is a syntax error.
|
* If there is a syntax error.
|
||||||
*/
|
*/
|
||||||
public JSONArray(JSONTokener x) throws JSONException {
|
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();
|
this();
|
||||||
if (x.nextClean() != '[') {
|
if (x.nextClean() != '[') {
|
||||||
throw x.syntaxError("A JSONArray text must start with '['");
|
throw x.syntaxError("A JSONArray text must start with '['");
|
||||||
@ -125,6 +136,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -138,7 +150,22 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* If there is a syntax error.
|
* If there is a syntax error.
|
||||||
*/
|
*/
|
||||||
public JSONArray(String source) throws JSONException {
|
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.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
|
@Override
|
||||||
protected JSONParserConfiguration clone() {
|
protected JSONParserConfiguration clone() {
|
||||||
JSONParserConfiguration clone = new JSONParserConfiguration();
|
JSONParserConfiguration clone = new JSONParserConfiguration();
|
||||||
@ -58,6 +64,23 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
return clone;
|
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
|
* The parser's behavior when meeting duplicate keys, controls whether the parser should
|
||||||
* overwrite duplicate keys or not.
|
* overwrite duplicate keys or not.
|
||||||
@ -67,4 +90,17 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
public boolean isOverwriteDuplicateKey() {
|
public boolean isOverwriteDuplicateKey() {
|
||||||
return this.overwriteDuplicateKey;
|
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