Skip to content

Commit fe597d2

Browse files
author
rikkarth
committed
fix(stleary#887): complete strictMode for JSONArray
1 parent d02ac0f commit fe597d2

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/main/java/org/json/JSONArray.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,42 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
144144
}
145145
}
146146
}
147+
148+
if (jsonParserConfiguration.isStrictMode()) {
149+
validateInput(x);
150+
}
151+
}
152+
153+
/**
154+
* Checks if Array adheres to strict mode guidelines, if not, throws JSONException providing back the input in the
155+
* error message.
156+
*
157+
* @param x tokener used to examine input.
158+
* @throws JSONException if input is not compliant with strict mode guidelines;
159+
*/
160+
private void validateInput(JSONTokener x) {
161+
char nextChar = x.getPrevious();
162+
163+
boolean isEndOfArray = nextChar == ']';
164+
boolean nextCharacterIsNotEoF = x.nextClean() != 0;
165+
166+
if (isEndOfArray && nextCharacterIsNotEoF) {
167+
String completeInput = collectCompleteInput(x);
168+
throw new JSONException("Provided Array is not compliant with strict mode guidelines: " + completeInput);
169+
}
170+
}
171+
172+
private String collectCompleteInput(JSONTokener x) {
173+
String nonCompliantStringAfterArray = collectNonCompliantStringAfterArray(x);
174+
return myArrayList + nonCompliantStringAfterArray;
175+
}
176+
177+
private String collectNonCompliantStringAfterArray(JSONTokener x) {
178+
StringBuilder sb = new StringBuilder().append(x.getPrevious());
179+
while(x.nextClean() != 0){
180+
sb.append(x.getPrevious());
181+
}
182+
return sb.toString();
147183
}
148184

149185
/**

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ public void givenInvalidInputArrays_testStrictModeTrue_shouldThrowJsonException(
4646
() -> new JSONArray(testCase, jsonParserConfiguration)));
4747
}
4848

49+
@Test
50+
public void givenValidDoubleArray_testStrictModeTrue_shouldNotThrowJsonException() {
51+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
52+
.withStrictMode(true);
53+
54+
String testCase = "[[\"c\"],[\"a\"]]";
55+
56+
new JSONArray(testCase, jsonParserConfiguration);
57+
}
58+
4959
@Test
5060
public void givenCompliantJSONArrayFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
5161
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonArray.json"))) {
@@ -208,6 +218,15 @@ public void verifyMaxDepthThenDuplicateKey() {
208218
*/
209219
private List<String> getNonCompliantJSONList() {
210220
return Arrays.asList(
221+
"[]asdf",
222+
"[]]",
223+
"[]}",
224+
"[][",
225+
"[]{",
226+
"[],",
227+
"[]:",
228+
"[],[",
229+
"[],{",
211230
"[1,2];[3,4]",
212231
"[test]",
213232
"[{'testSingleQuote': 'testSingleQuote'}]",

0 commit comments

Comments
 (0)