Compare commits

..
2 changed files with 22 additions and 4 deletions
+4 -4
View File
@@ -131,9 +131,9 @@ public class JSONArray implements Iterable<Object> {
* @param x A JSONTokener instance from which the JSONArray is constructed. * @param x A JSONTokener instance from which the JSONArray is constructed.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser. * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
* @param isInitial Boolean indicating position of char * @param isInitial Boolean indicating position of char
* @return * @return true if a syntax error has occurred, otherwise false
*/ */
private static boolean checkForSyntaxError(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) { private boolean checkForSyntaxError(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) {
char nextChar; char nextChar;
switch (x.nextClean()) { switch (x.nextClean()) {
case 0: case 0:
@@ -153,11 +153,11 @@ public class JSONArray implements Iterable<Object> {
return true; return true;
} }
if (nextChar == ',') { if (nextChar == ',') {
// consecutive commas are not allowed in strict mode // Consecutive commas are not allowed in strict mode.
// Otherwise, the tokener is backed up, and a null object is inserted by the calling code.
if (jsonParserConfiguration.isStrictMode()) { if (jsonParserConfiguration.isStrictMode()) {
throw x.syntaxError("Strict mode error: Expected a valid array element"); throw x.syntaxError("Strict mode error: Expected a valid array element");
} }
return true;
} }
x.back(); x.back();
break; break;
@@ -1548,4 +1548,22 @@ public class JSONArrayTest {
nestedArray.add(buildNestedArray(maxDepth - 1)); nestedArray.add(buildNestedArray(maxDepth - 1));
return nestedArray; return nestedArray;
} }
@Test
public void TestLenientCommas() {
String str = "[1,,3]";
// Test should fail if default strictMode is true, pass with an extra null entry if false
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
try {
new JSONArray(str);
fail("Expected to throw exception due to invalid string");
} catch (JSONException e) { /* no action is needed here */ }
} else {
JSONArray jsonArray = new JSONArray(str);
assertEquals("JSONArray in non-strictMode should contain a null entry",
"[1,null,3]", jsonArray.toString());
}
}
} }