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:
Sean Leary
2024-12-14 14:40:40 -06:00
parent 80b2672f77
commit 1f0729cadb
5 changed files with 679 additions and 6 deletions

View File

@@ -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));
}
}
}
/**