mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 11:25:30 -04:00
fix(#887): regression parsing array with non-string and boolean values
This commit is contained in:
parent
0bace72ced
commit
7cc19483fb
@ -522,10 +522,10 @@ public class JSONTokener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strictMode) {
|
if (strictMode) {
|
||||||
boolean isBooleanOrNumeric = checkIfValueIsBooleanOrNumeric(string);
|
Object stringToVal = JSONObject.stringToValue(string);
|
||||||
|
|
||||||
if (isBooleanOrNumeric) {
|
if (stringToVal instanceof Number || stringToVal instanceof Boolean) {
|
||||||
return string;
|
return stringToVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new JSONException(String.format("Value is not surrounded by quotes: %s", string));
|
throw new JSONException(String.format("Value is not surrounded by quotes: %s", string));
|
||||||
@ -534,20 +534,6 @@ public class JSONTokener {
|
|||||||
return JSONObject.stringToValue(string);
|
return JSONObject.stringToValue(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkIfValueIsBooleanOrNumeric(Object valueToValidate) {
|
|
||||||
String stringToValidate = valueToValidate.toString();
|
|
||||||
if (stringToValidate.equals("true") || stringToValidate.equals("false")) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Double.parseDouble(stringToValidate);
|
|
||||||
return true;
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Skip characters until the next character is the requested character.
|
* Skip characters until the next character is the requested character.
|
||||||
* If the requested character is not found, no characters are skipped.
|
* If the requested character is not found, no characters are skipped.
|
||||||
|
@ -51,9 +51,35 @@ public class JSONParserConfigurationTest {
|
|||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
String testCase = "[[\"c\"],[\"a\"]]";
|
String testCase = "[[\"c\"], [10.2], [true, false, true]]";
|
||||||
|
|
||||||
new JSONArray(testCase, jsonParserConfiguration);
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
JSONArray arrayShouldContainStringAt0 = jsonArray.getJSONArray(0);
|
||||||
|
JSONArray arrayShouldContainNumberAt0 = jsonArray.getJSONArray(1);
|
||||||
|
JSONArray arrayShouldContainBooleanAt0 = jsonArray.getJSONArray(2);
|
||||||
|
|
||||||
|
assertTrue(arrayShouldContainStringAt0.get(0) instanceof String);
|
||||||
|
assertTrue(arrayShouldContainNumberAt0.get(0) instanceof Number);
|
||||||
|
assertTrue(arrayShouldContainBooleanAt0.get(0) instanceof Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidString_testStrictModeTrue_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCase = "[badString]";
|
||||||
|
|
||||||
|
JSONException je = assertThrows(JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals("Value is not surrounded by quotes: badString", je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleNumericArray() {
|
||||||
|
String expected = "[10]";
|
||||||
|
JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||||
|
assertEquals(expected, jsonArray.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user