mirror of
https://github.com/stleary/JSON-java.git
synced 2026-01-24 00:03:17 -05:00
Revert "Merge pull request #877 from rikkarth/feat/871-strictMode"
This reverts commitd02ac0f2a3, reversing changes made tocfd47615d0.
This commit is contained in:
@@ -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> <small>(double quote)</small> or
|
||||
* <code>'</code> <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.
|
||||
|
||||
Reference in New Issue
Block a user