mirror of
https://github.com/stleary/JSON-java.git
synced 2026-01-25 00:00:38 -05:00
restore-jsonparserconfiguration: strict mode initial attempt. Still missing all JSONObject test cases and strict mode sanity check. Might be able to simplify implementation a bit more
This commit is contained in:
@@ -67,6 +67,10 @@ public class JSONArray implements Iterable<Object> {
|
||||
*/
|
||||
private final ArrayList<Object> myArrayList;
|
||||
|
||||
private JSONTokener jsonTokener;
|
||||
|
||||
private JSONParserConfiguration jsonParserConfiguration;
|
||||
|
||||
/**
|
||||
* Construct an empty JSONArray.
|
||||
*/
|
||||
@@ -95,6 +99,15 @@ public class JSONArray implements Iterable<Object> {
|
||||
*/
|
||||
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||
this();
|
||||
|
||||
if (this.jsonParserConfiguration == null) {
|
||||
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||
}
|
||||
if (this.jsonTokener == null) {
|
||||
this.jsonTokener = x;
|
||||
this.jsonTokener.setJsonParserConfiguration(this.jsonParserConfiguration);
|
||||
}
|
||||
|
||||
if (x.nextClean() != '[') {
|
||||
throw x.syntaxError("A JSONArray text must start with '['");
|
||||
}
|
||||
@@ -125,6 +138,9 @@ public class JSONArray implements Iterable<Object> {
|
||||
throw x.syntaxError("Expected a ',' or ']'");
|
||||
}
|
||||
if (nextChar == ']') {
|
||||
if (jsonParserConfiguration.isStrictMode()) {
|
||||
throw x.syntaxError("Expected another array element");
|
||||
}
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
@@ -136,7 +152,6 @@ public class JSONArray implements Iterable<Object> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,6 +166,13 @@ public class JSONArray implements Iterable<Object> {
|
||||
*/
|
||||
public JSONArray(String source) throws JSONException {
|
||||
this(source, new JSONParserConfiguration());
|
||||
if (this.jsonParserConfiguration.isStrictMode()) {
|
||||
char c = jsonTokener.nextClean();
|
||||
if (c != 0) {
|
||||
throw jsonTokener.syntaxError(String.format("invalid character '%s' found after end of array", c));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,6 +188,13 @@ public class JSONArray implements Iterable<Object> {
|
||||
*/
|
||||
public JSONArray(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||
this(new JSONTokener(source), jsonParserConfiguration);
|
||||
if (this.jsonParserConfiguration.isStrictMode()) {
|
||||
char c = jsonTokener.nextClean();
|
||||
if (c != 0) {
|
||||
throw jsonTokener.syntaxError(String.format("invalid character '%s' found after end of array", c));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -152,6 +152,10 @@ public class JSONObject {
|
||||
*/
|
||||
public static final Object NULL = new Null();
|
||||
|
||||
private JSONTokener jsonTokener;
|
||||
|
||||
private JSONParserConfiguration jsonParserConfiguration;
|
||||
|
||||
/**
|
||||
* Construct an empty JSONObject.
|
||||
*/
|
||||
@@ -211,6 +215,15 @@ public class JSONObject {
|
||||
*/
|
||||
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||
this();
|
||||
|
||||
if (this.jsonParserConfiguration == null) {
|
||||
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||
}
|
||||
if (this.jsonTokener == null) {
|
||||
this.jsonTokener = x;
|
||||
this.jsonTokener.setJsonParserConfiguration(this.jsonParserConfiguration);
|
||||
}
|
||||
|
||||
char c;
|
||||
String key;
|
||||
|
||||
@@ -433,6 +446,10 @@ public class JSONObject {
|
||||
*/
|
||||
public JSONObject(String source) throws JSONException {
|
||||
this(source, new JSONParserConfiguration());
|
||||
if (this.jsonParserConfiguration.isStrictMode() &&
|
||||
this.jsonTokener.nextClean() != 0) {
|
||||
throw new JSONException("Unparsed characters found at end of input text");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,6 +32,7 @@ public class JSONTokener {
|
||||
/** the number of characters read in the previous line. */
|
||||
private long characterPreviousLine;
|
||||
|
||||
private JSONParserConfiguration jsonParserConfiguration;
|
||||
|
||||
/**
|
||||
* Construct a JSONTokener from a Reader. The caller must close the Reader.
|
||||
@@ -70,6 +71,21 @@ public class JSONTokener {
|
||||
this(new StringReader(s));
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter
|
||||
* @return jsonParserConfiguration
|
||||
*/
|
||||
public JSONParserConfiguration getJsonParserConfiguration() {
|
||||
return jsonParserConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter
|
||||
* @param jsonParserConfiguration new value for jsonParserConfiguration
|
||||
*/
|
||||
public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfiguration) {
|
||||
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Back up one character. This provides a sort of lookahead capability,
|
||||
@@ -409,14 +425,14 @@ public class JSONTokener {
|
||||
case '{':
|
||||
this.back();
|
||||
try {
|
||||
return new JSONObject(this);
|
||||
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);
|
||||
return new JSONArray(this, jsonParserConfiguration);
|
||||
} catch (StackOverflowError e) {
|
||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||
}
|
||||
@@ -427,6 +443,11 @@ public class JSONTokener {
|
||||
Object nextSimpleValue(char c) {
|
||||
String string;
|
||||
|
||||
if (jsonParserConfiguration != null &&
|
||||
jsonParserConfiguration.isStrictMode() &&
|
||||
c == '\'') {
|
||||
throw this.syntaxError("Single quote wrap not allowed in strict mode");
|
||||
}
|
||||
switch (c) {
|
||||
case '"':
|
||||
case '\'':
|
||||
@@ -455,7 +476,13 @@ public class JSONTokener {
|
||||
if ("".equals(string)) {
|
||||
throw this.syntaxError("Missing value");
|
||||
}
|
||||
return JSONObject.stringToValue(string);
|
||||
Object obj = JSONObject.stringToValue(string);
|
||||
if (jsonParserConfiguration != null &&
|
||||
jsonParserConfiguration.isStrictMode() &&
|
||||
obj instanceof String) {
|
||||
throw this.syntaxError(String.format("Value '%s' is not surrounded by quotes", obj));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user