Skip to content

Commit 7cc1948

Browse files
author
rikkarth
committed
fix(stleary#887): regression parsing array with non-string and boolean values
1 parent 0bace72 commit 7cc1948

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/main/java/org/json/JSONTokener.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,10 @@ private Object parsedUnquotedText(char c, boolean strictMode) {
522522
}
523523

524524
if (strictMode) {
525-
boolean isBooleanOrNumeric = checkIfValueIsBooleanOrNumeric(string);
525+
Object stringToVal = JSONObject.stringToValue(string);
526526

527-
if (isBooleanOrNumeric) {
528-
return string;
527+
if (stringToVal instanceof Number || stringToVal instanceof Boolean) {
528+
return stringToVal;
529529
}
530530

531531
throw new JSONException(String.format("Value is not surrounded by quotes: %s", string));
@@ -534,20 +534,6 @@ private Object parsedUnquotedText(char c, boolean strictMode) {
534534
return JSONObject.stringToValue(string);
535535
}
536536

537-
private boolean checkIfValueIsBooleanOrNumeric(Object valueToValidate) {
538-
String stringToValidate = valueToValidate.toString();
539-
if (stringToValidate.equals("true") || stringToValidate.equals("false")) {
540-
return true;
541-
}
542-
543-
try {
544-
Double.parseDouble(stringToValidate);
545-
return true;
546-
} catch (NumberFormatException e) {
547-
return false;
548-
}
549-
}
550-
551537
/**
552538
* Skip characters until the next character is the requested character.
553539
* If the requested character is not found, no characters are skipped.

src/test/java/org/json/junit/JSONParserConfigurationTest.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,35 @@ public void givenValidDoubleArray_testStrictModeTrue_shouldNotThrowJsonException
5151
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
5252
.withStrictMode(true);
5353

54-
String testCase = "[[\"c\"],[\"a\"]]";
54+
String testCase = "[[\"c\"], [10.2], [true, false, true]]";
5555

56-
new JSONArray(testCase, jsonParserConfiguration);
56+
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
57+
JSONArray arrayShouldContainStringAt0 = jsonArray.getJSONArray(0);
58+
JSONArray arrayShouldContainNumberAt0 = jsonArray.getJSONArray(1);
59+
JSONArray arrayShouldContainBooleanAt0 = jsonArray.getJSONArray(2);
60+
61+
assertTrue(arrayShouldContainStringAt0.get(0) instanceof String);
62+
assertTrue(arrayShouldContainNumberAt0.get(0) instanceof Number);
63+
assertTrue(arrayShouldContainBooleanAt0.get(0) instanceof Boolean);
64+
}
65+
66+
@Test
67+
public void givenInvalidString_testStrictModeTrue_shouldThrowJsonException() {
68+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
69+
.withStrictMode(true);
70+
71+
String testCase = "[badString]";
72+
73+
JSONException je = assertThrows(JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
74+
75+
assertEquals("Value is not surrounded by quotes: badString", je.getMessage());
76+
}
77+
78+
@Test
79+
public void shouldHandleNumericArray() {
80+
String expected = "[10]";
81+
JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
82+
assertEquals(expected, jsonArray.toString());
5783
}
5884

5985
@Test

0 commit comments

Comments
 (0)