Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#887): complete strictMode for JSONArray #888

Merged
merged 24 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fe597d2
fix(#887): complete strictMode for JSONArray
rikkarth Apr 14, 2024
ce074e9
fix(#887): corrected small typo
rikkarth Apr 14, 2024
3dcd5b2
fix(#887): double array breaking JSONTokener.nextValue
rikkarth Apr 21, 2024
03def9c
Merge branch 'master' of github.com:stleary/JSON-java into fix/887
rikkarth Apr 21, 2024
0bace72
fix(#887): small typo
rikkarth Apr 21, 2024
7cc1948
fix(#887): regression parsing array with non-string and boolean values
rikkarth Apr 23, 2024
ce13ebd
chore(#887): clean up parsedUnquotedText implementation
rikkarth Apr 23, 2024
898dd5a
fix(#887): allow null value strict mode
rikkarth Apr 23, 2024
879579d
chore(#887): signature minor edit
rikkarth Apr 23, 2024
9216a19
feat(#877): improved JSONArray and JSONTokener logic
rikkarth Apr 27, 2024
7a8c216
fix(#877): adaptation for java 6 compatibility
rikkarth Apr 27, 2024
1e3f37b
feat(#877): add additional validation, test case
rikkarth Apr 27, 2024
4319b71
force strict mode to expose failing tests
Apr 28, 2024
6529a7e
fixes the broken JSONArrayTest cases
Apr 28, 2024
d1fd901
fixes the JSONObjectNumberTest cases
Apr 28, 2024
2098373
fixes the broken JSONObjectTest cases
Apr 28, 2024
1881cbe
fixes the broken CDLTest cases
Apr 28, 2024
f4944fb
fixes the broken JSONMLTest cases
Apr 28, 2024
fa2f340
fixes the broken XMLConfigurationTest cases
Apr 28, 2024
0180bd9
fixes the broken XMLTest cases
Apr 28, 2024
cf00ef3
fixes the broken JSONTokenerTest cases
Apr 28, 2024
1ae43bd
fix(#887): regressions, unit tests
rikkarth Apr 28, 2024
48dfeb8
fix(#887): unit tests, uncommented tests after fix
rikkarth Apr 28, 2024
a8ab79e
chore(#887): JSONParserConfiguration strictMode true flag cleanup
rikkarth May 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 75 additions & 42 deletions src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,53 +96,83 @@ public JSONArray(JSONTokener x) throws JSONException {
*/
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this();
if (x.nextClean() != '[') {
char nextChar = x.nextClean();

// check first character, if not '[' throw JSONException
if (nextChar != '[') {
throw x.syntaxError("A JSONArray text must start with '['");
}

char nextChar = x.nextClean();
if (nextChar == 0) {
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
}
if (nextChar != ']') {
x.back();
for (;;) {
if (x.nextClean() == ',') {
x.back();
this.myArrayList.add(JSONObject.NULL);
} else {
x.back();
this.myArrayList.add(x.nextValue(jsonParserConfiguration));
parseTokener(x, jsonParserConfiguration); // runs recursively

}

private void parseTokener(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) {
boolean strictMode = jsonParserConfiguration.isStrictMode();

char cursor = x.nextClean();

switch (cursor) {
case 0:
throwErrorIfEoF(x);
break;
case ',':
cursor = x.nextClean();

throwErrorIfEoF(x);

if(strictMode && cursor == ']'){
throw x.syntaxError(getInvalidCharErrorMsg(cursor));
}
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) {
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
}
if (nextChar == ']') {
return;
}
x.back();

if (cursor == ']') {
break;
}

x.back();

parseTokener(x, jsonParserConfiguration);
break;
case ']':
if (strictMode) {
cursor = x.nextClean();
boolean isEoF = x.end();

if (isEoF) {
break;
case ']':
if (jsonParserConfiguration.isStrictMode()) {
nextChar = x.nextClean();
if (nextChar != 0) {
throw x.syntaxError("invalid character found after end of array: " + nextChar);
}
}

return;
default:
throw x.syntaxError("Expected a ',' or ']'");
}

if (x.getArrayLevel() == 0) {
throw x.syntaxError(getInvalidCharErrorMsg(cursor));
}

x.back();
}
}
break;
default:
x.back();
boolean currentCharIsQuote = x.getPrevious() == '"';
boolean quoteIsNotNextToValidChar = x.getPreviousChar() != ',' && x.getPreviousChar() != '[';

if (strictMode && currentCharIsQuote && quoteIsNotNextToValidChar) {
throw x.syntaxError(getInvalidCharErrorMsg(cursor));
}

this.myArrayList.add(x.nextValue(jsonParserConfiguration));
parseTokener(x, jsonParserConfiguration);
}
}

/**
* Throws JSONException if JSONTokener has reached end of file, usually when array is unclosed. No ']' found,
* instead EoF.
*
* @param x the JSONTokener being evaluated.
* @throws JSONException if JSONTokener has reached end of file.
*/
private void throwErrorIfEoF(JSONTokener x) {
if (x.end()) {
throw x.syntaxError(String.format("Expected a ',' or ']' but instead found '%s'", x.getPrevious()));
}
}

Expand Down Expand Up @@ -1932,6 +1962,7 @@ private void addAll(Object array, boolean wrap) throws JSONException {
private void addAll(Object array, boolean wrap, int recursionDepth) {
addAll(array, wrap, recursionDepth, new JSONParserConfiguration());
}

/**
* Add an array's elements to the JSONArray.
*`
Expand Down Expand Up @@ -1978,7 +2009,6 @@ private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserCo
"JSONArray initial value should be a string or collection or array.");
}
}

/**
* Create a new JSONException in a common format for incorrect conversions.
* @param idx index of the item
Expand Down Expand Up @@ -2007,4 +2037,7 @@ private static JSONException wrongValueFormatException(
, cause);
}

private static String getInvalidCharErrorMsg(char cursor) {
return String.format("invalid character '%s' found after end of array", cursor);
}
}
Loading
Loading