Skip to content

Commit 46a1c9a

Browse files
author
John J. Aylward
committed
Adds test case to confirm the parsing of control characters
1 parent 80f9e48 commit 46a1c9a

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,26 @@ public void wrapObject() {
15641564
assertTrue("expected val3", "val3".equals(mapJsonObject.query("/key3")));
15651565
}
15661566

1567+
1568+
/**
1569+
* RFC 7159 defines control characters to be U+0000 through U+001F. This test verifies that the parser is checking for these in expected ways.
1570+
*/
1571+
@Test
1572+
public void jsonObjectParseControlCharacters(){
1573+
for(int i = 0;i<=0x001f;i++){
1574+
final String charString = String.valueOf((char)i);
1575+
final String source = "{\"key\":\""+charString+"\"}";
1576+
try {
1577+
JSONObject jo = new JSONObject(source);
1578+
assertTrue("Expected "+charString+"("+i+") in the JSON Object but did not find it.",charString.equals(jo.getString("key")));
1579+
} catch (JSONException ex) {
1580+
assertTrue("Only \\0 (U+0000), \\n (U+000A), and \\r (U+000D) should cause an error. Instead "+charString+"("+i+") caused an error",
1581+
i=='\0' || i=='\n' || i=='\r'
1582+
);
1583+
}
1584+
}
1585+
}
1586+
15671587
/**
15681588
* Explore how JSONObject handles parsing errors.
15691589
*/

0 commit comments

Comments
 (0)