feat(#877): add additional validation, test case

This commit is contained in:
rikkarth 2024-04-27 22:37:21 +01:00
parent 7a8c21621c
commit 1e3f37be98
No known key found for this signature in database
GPG Key ID: 11E5F28B0AED6AC7
2 changed files with 11 additions and 3 deletions

View File

@ -121,6 +121,10 @@ public class JSONArray implements Iterable<Object> {
throwErrorIfEoF(x); throwErrorIfEoF(x);
if(strictMode && cursor == ']'){
throw x.syntaxError(getInvalidCharErrorMsg(cursor));
}
if (cursor == ']') { if (cursor == ']') {
break; break;
} }
@ -135,7 +139,7 @@ public class JSONArray implements Iterable<Object> {
boolean isNotEoF = !x.end(); boolean isNotEoF = !x.end();
if (isNotEoF && x.getArrayLevel() == 0) { if (isNotEoF && x.getArrayLevel() == 0) {
throw x.syntaxError(String.format("invalid character '%s' found after end of array", cursor)); throw x.syntaxError(getInvalidCharErrorMsg(cursor));
} }
x.back(); x.back();
@ -147,7 +151,7 @@ public class JSONArray implements Iterable<Object> {
boolean quoteIsNotNextToValidChar = x.getPreviousChar() != ',' && x.getPreviousChar() != '['; boolean quoteIsNotNextToValidChar = x.getPreviousChar() != ',' && x.getPreviousChar() != '[';
if (strictMode && currentCharIsQuote && quoteIsNotNextToValidChar) { if (strictMode && currentCharIsQuote && quoteIsNotNextToValidChar) {
throw x.syntaxError(String.format("invalid character '%s' found after end of array", cursor)); throw x.syntaxError(getInvalidCharErrorMsg(cursor));
} }
this.myArrayList.add(x.nextValue(jsonParserConfiguration)); this.myArrayList.add(x.nextValue(jsonParserConfiguration));
@ -1954,6 +1958,7 @@ public class JSONArray implements Iterable<Object> {
private void addAll(Object array, boolean wrap, int recursionDepth) { private void addAll(Object array, boolean wrap, int recursionDepth) {
addAll(array, wrap, recursionDepth, new JSONParserConfiguration()); addAll(array, wrap, recursionDepth, new JSONParserConfiguration());
} }
/** /**
* Add an array's elements to the JSONArray. * Add an array's elements to the JSONArray.
*` *`
@ -2000,7 +2005,6 @@ public class JSONArray implements Iterable<Object> {
"JSONArray initial value should be a string or collection or array."); "JSONArray initial value should be a string or collection or array.");
} }
} }
/** /**
* Create a new JSONException in a common format for incorrect conversions. * Create a new JSONException in a common format for incorrect conversions.
* @param idx index of the item * @param idx index of the item
@ -2029,4 +2033,7 @@ public class JSONArray implements Iterable<Object> {
, cause); , cause);
} }
private static String getInvalidCharErrorMsg(char cursor) {
return String.format("invalid character '%s' found after end of array", cursor);
}
} }

View File

@ -287,6 +287,7 @@ public class JSONParserConfigurationTest {
private List<String> getNonCompliantJSONList() { private List<String> getNonCompliantJSONList() {
return Arrays.asList( return Arrays.asList(
"[1],", "[1],",
"[1,]",
"[[1]\"sa\",[2]]a", "[[1]\"sa\",[2]]a",
"[1],\"dsa\": \"test\"", "[1],\"dsa\": \"test\"",
"[[a]]", "[[a]]",