Skip to content

Commit 87406e4

Browse files
authored
Merge pull request #879 from Simulant87/add-syntax-error-details
extend syntax error information
2 parents 45dede4 + 0fcf352 commit 87406e4

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ public String nextString(char quote) throws JSONException {
299299
case 0:
300300
case '\n':
301301
case '\r':
302-
throw this.syntaxError("Unterminated string");
302+
throw this.syntaxError("Unterminated string. " +
303+
"Character with int code " + (int) c + " is not allowed within a quoted string.");
303304
case '\\':
304305
c = this.next();
305306
switch (c) {
@@ -319,10 +320,12 @@ public String nextString(char quote) throws JSONException {
319320
sb.append('\r');
320321
break;
321322
case 'u':
323+
String next = this.next(4);
322324
try {
323-
sb.append((char)Integer.parseInt(this.next(4), 16));
325+
sb.append((char)Integer.parseInt(next, 16));
324326
} catch (NumberFormatException e) {
325-
throw this.syntaxError("Illegal escape.", e);
327+
throw this.syntaxError("Illegal escape. " +
328+
"\\u must be followed by a 4 digit hexadecimal number. \\" + next + " is not valid.", e);
326329
}
327330
break;
328331
case '"':
@@ -332,7 +335,7 @@ public String nextString(char quote) throws JSONException {
332335
sb.append(c);
333336
break;
334337
default:
335-
throw this.syntaxError("Illegal escape.");
338+
throw this.syntaxError("Illegal escape. Escape sequence \\" + c + " is not valid.");
336339
}
337340
break;
338341
default:

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

+54
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)