Merge pull request #1061 from stleary/restore-lenient-jsonarray
restore-lenient-jsonarray allow consecutive commas and insert null
This commit is contained in:
@@ -131,9 +131,9 @@ public class JSONArray implements Iterable<Object> {
|
||||
* @param x A JSONTokener instance from which the JSONArray is constructed.
|
||||
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||
* @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;
|
||||
switch (x.nextClean()) {
|
||||
case 0:
|
||||
@@ -153,11 +153,11 @@ public class JSONArray implements Iterable<Object> {
|
||||
return true;
|
||||
}
|
||||
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()) {
|
||||
throw x.syntaxError("Strict mode error: Expected a valid array element");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
x.back();
|
||||
break;
|
||||
|
||||
@@ -1548,4 +1548,22 @@ public class JSONArrayTest {
|
||||
nestedArray.add(buildNestedArray(maxDepth - 1));
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user