mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
add test cases for extended syntax error exception messages
This commit is contained in:
parent
6c160b7d1a
commit
f1c9d0787b
@ -320,10 +320,12 @@ public class JSONTokener {
|
|||||||
sb.append('\r');
|
sb.append('\r');
|
||||||
break;
|
break;
|
||||||
case 'u':
|
case 'u':
|
||||||
|
String next = this.next(4);
|
||||||
try {
|
try {
|
||||||
sb.append((char)Integer.parseInt(this.next(4), 16));
|
sb.append((char)Integer.parseInt(next, 16));
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw this.syntaxError("Illegal escape. \\u must be followed by a 4 digit number.", e);
|
throw this.syntaxError("Illegal escape. " +
|
||||||
|
"\\u must be followed by a 4 digit hexadecimal number. \\" + next + " is not valid.", e);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
|
@ -2193,6 +2193,60 @@ public class JSONObjectTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void jsonObjectParseControlCharacterEOFAssertExceptionMessage(){
|
||||||
|
char c = '\0';
|
||||||
|
final String source = "{\"key\":\"" + c + "\"}";
|
||||||
|
try {
|
||||||
|
JSONObject jo = new JSONObject(source);
|
||||||
|
fail("JSONException should be thrown");
|
||||||
|
} catch (JSONException ex) {
|
||||||
|
assertEquals("Unterminated string. " + "Character with int code 0" +
|
||||||
|
" is not allowed within a quoted string. at 8 [character 9 line 1]", ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void jsonObjectParseControlCharacterNewLineAssertExceptionMessage(){
|
||||||
|
char[] chars = {'\n', '\r'};
|
||||||
|
for( char c : chars) {
|
||||||
|
final String source = "{\"key\":\"" + c + "\"}";
|
||||||
|
try {
|
||||||
|
JSONObject jo = new JSONObject(source);
|
||||||
|
fail("JSONException should be thrown");
|
||||||
|
} catch (JSONException ex) {
|
||||||
|
assertEquals("Unterminated string. " + "Character with int code " + (int) c +
|
||||||
|
" is not allowed within a quoted string. at 9 [character 0 line 2]", ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void jsonObjectParseUTF8EncodingAssertExceptionMessage(){
|
||||||
|
String c = "\\u123x";
|
||||||
|
final String source = "{\"key\":\"" + c + "\"}";
|
||||||
|
try {
|
||||||
|
JSONObject jo = new JSONObject(source);
|
||||||
|
fail("JSONException should be thrown");
|
||||||
|
} catch (JSONException ex) {
|
||||||
|
assertEquals("Illegal escape. \\u must be followed by a 4 digit hexadecimal number. " +
|
||||||
|
"\\123x is not valid. at 14 [character 15 line 1]", ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void jsonObjectParseIllegalEscapeAssertExceptionMessage(){
|
||||||
|
String c = "\\x";
|
||||||
|
final String source = "{\"key\":\"" + c + "\"}";
|
||||||
|
try {
|
||||||
|
JSONObject jo = new JSONObject(source);
|
||||||
|
fail("JSONException should be thrown");
|
||||||
|
} catch (JSONException ex) {
|
||||||
|
assertEquals("Illegal escape. Escape sequence " + c + " is not valid." +
|
||||||
|
" at 10 [character 11 line 1]", ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Explore how JSONObject handles parsing errors.
|
* Explore how JSONObject handles parsing errors.
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user