Skip to content

Commit f1c9d07

Browse files
committed
add test cases for extended syntax error exception messages
1 parent 6c160b7 commit f1c9d07

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,12 @@ public String nextString(char quote) throws JSONException {
320320
sb.append('\r');
321321
break;
322322
case 'u':
323+
String next = this.next(4);
323324
try {
324-
sb.append((char)Integer.parseInt(this.next(4), 16));
325+
sb.append((char)Integer.parseInt(next, 16));
325326
} catch (NumberFormatException e) {
326-
throw this.syntaxError("Illegal escape. \\u must be followed by a 4 digit number.", e);
327+
throw this.syntaxError("Illegal escape. " +
328+
"\\u must be followed by a 4 digit hexadecimal number. \\" + next + " is not valid.", e);
327329
}
328330
break;
329331
case '"':

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,60 @@ public void jsonObjectParseControlCharacters(){
21932193
}
21942194
}
21952195

2196+
@Test
2197+
public void jsonObjectParseControlCharacterEOFAssertExceptionMessage(){
2198+
char c = '\0';
2199+
final String source = "{\"key\":\"" + c + "\"}";
2200+
try {
2201+
JSONObject jo = new JSONObject(source);
2202+
fail("JSONException should be thrown");
2203+
} catch (JSONException ex) {
2204+
assertEquals("Unterminated string. " + "Character with int code 0" +
2205+
" is not allowed within a quoted string. at 8 [character 9 line 1]", ex.getMessage());
2206+
}
2207+
}
2208+
2209+
@Test
2210+
public void jsonObjectParseControlCharacterNewLineAssertExceptionMessage(){
2211+
char[] chars = {'\n', '\r'};
2212+
for( char c : chars) {
2213+
final String source = "{\"key\":\"" + c + "\"}";
2214+
try {
2215+
JSONObject jo = new JSONObject(source);
2216+
fail("JSONException should be thrown");
2217+
} catch (JSONException ex) {
2218+
assertEquals("Unterminated string. " + "Character with int code " + (int) c +
2219+
" is not allowed within a quoted string. at 9 [character 0 line 2]", ex.getMessage());
2220+
}
2221+
}
2222+
}
2223+
2224+
@Test
2225+
public void jsonObjectParseUTF8EncodingAssertExceptionMessage(){
2226+
String c = "\\u123x";
2227+
final String source = "{\"key\":\"" + c + "\"}";
2228+
try {
2229+
JSONObject jo = new JSONObject(source);
2230+
fail("JSONException should be thrown");
2231+
} catch (JSONException ex) {
2232+
assertEquals("Illegal escape. \\u must be followed by a 4 digit hexadecimal number. " +
2233+
"\\123x is not valid. at 14 [character 15 line 1]", ex.getMessage());
2234+
}
2235+
}
2236+
2237+
@Test
2238+
public void jsonObjectParseIllegalEscapeAssertExceptionMessage(){
2239+
String c = "\\x";
2240+
final String source = "{\"key\":\"" + c + "\"}";
2241+
try {
2242+
JSONObject jo = new JSONObject(source);
2243+
fail("JSONException should be thrown");
2244+
} catch (JSONException ex) {
2245+
assertEquals("Illegal escape. Escape sequence " + c + " is not valid." +
2246+
" at 10 [character 11 line 1]", ex.getMessage());
2247+
}
2248+
}
2249+
21962250
/**
21972251
* Explore how JSONObject handles parsing errors.
21982252
*/

0 commit comments

Comments
 (0)