Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend syntax error information #879

Merged
merged 11 commits into from
Mar 30, 2024
15 changes: 9 additions & 6 deletions src/main/java/org/json/JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ public String nextString(char quote) throws JSONException {
case 0:
case '\n':
case '\r':
throw this.syntaxError("Unterminated string");
throw this.syntaxError("Unterminated string. " +
"Character with int code " + (int) c + " is not allowed within a quoted string.");
case '\\':
c = this.next();
switch (c) {
Expand All @@ -319,10 +320,12 @@ public String nextString(char quote) throws JSONException {
sb.append('\r');
break;
case 'u':
String next = this.next(4);
try {
sb.append((char)Integer.parseInt(this.next(4), 16));
sb.append((char)Integer.parseInt(next, 16));
} catch (NumberFormatException e) {
throw this.syntaxError("Illegal escape.", e);
throw this.syntaxError("Illegal escape. " +
"\\u must be followed by a 4 digit hexadecimal number. \\" + next + " is not valid.", e);
}
break;
case '"':
Expand All @@ -332,7 +335,7 @@ public String nextString(char quote) throws JSONException {
sb.append(c);
break;
default:
throw this.syntaxError("Illegal escape.");
throw this.syntaxError("Illegal escape. Escape sequence \\" + c + " is not valid.");
}
break;
default:
Expand Down Expand Up @@ -517,11 +520,11 @@ public JSONException syntaxError(String message, Throwable causedBy) {
/**
* Make a printable string of this JSONTokener.
*
* @return " at {index} [character {character} line {line}]"
* @return " at index {index} [character number {character} in line {line}]"
*/
@Override
public String toString() {
return " at " + this.index + " [character " + this.character + " line " +
Simulant87 marked this conversation as resolved.
Show resolved Hide resolved
return " at index " + this.index + " [character number " + this.character + " in line " +
this.line + "]";
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/json/junit/CDLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void unbalancedQuoteInName() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Missing close quote '\"'. at 12 [character 0 line 2]",
"Missing close quote '\"'. at index 12 [character number 0 in line 2]",
e.getMessage());
}
}
Expand All @@ -83,7 +83,7 @@ public void unbalancedQuoteInValue() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Missing close quote '\"'. at 22 [character 11 line 2]",
"Missing close quote '\"'. at index 22 [character number 11 in line 2]",
e.getMessage());

}
Expand All @@ -101,7 +101,7 @@ public void nullInName() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Bad character 'o' (111). at 2 [character 3 line 1]",
"Bad character 'o' (111). at index 2 [character number 3 in line 1]",
e.getMessage());

}
Expand All @@ -119,7 +119,7 @@ public void unbalancedEscapedQuote(){
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Missing close quote '\"'. at 26 [character 15 line 2]",
"Missing close quote '\"'. at index 26 [character number 15 in line 2]",
e.getMessage());

}
Expand Down Expand Up @@ -170,7 +170,7 @@ public void badEscapedQuote(){
} catch (JSONException e) {
//System.out.println("Message" + e.getMessage());
assertEquals("Expecting an exception message",
"Bad character 'V' (86). at 20 [character 9 line 2]",
"Bad character 'V' (86). at index 20 [character number 9 in line 2]",
e.getMessage());

}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/json/junit/CookieListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void malFormedCookieListException() {
* Not sure of the missing char, but full string compare fails
*/
assertEquals("Expecting an exception message",
"Expected '=' and instead saw '' at 25 [character 26 line 1]",
"Expected '=' and instead saw '' at index 25 [character number 26 in line 1]",
e.getMessage());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/json/junit/CookieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void malFormedNameValueException() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Expected '=' and instead saw '' at 25 [character 26 line 1]",
"Expected '=' and instead saw '' at index 25 [character number 26 in line 1]",
e.getMessage());
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void emptyStr() {
assertNull("Should throw an exception", new JSONArray(str));
} catch (JSONException e) {
assertEquals("Expected an exception message",
"A JSONArray text must start with '[' at 0 [character 1 line 1]",
"A JSONArray text must start with '[' at index 0 [character number 1 in line 1]",
e.getMessage());
}
}
Expand All @@ -142,7 +142,7 @@ public void unclosedArray() {
assertNull("Should throw an exception", new JSONArray("["));
} catch (JSONException e) {
assertEquals("Expected an exception message",
"Expected a ',' or ']' at 1 [character 2 line 1]",
"Expected a ',' or ']' at index 1 [character number 2 in line 1]",
e.getMessage());
}
}
Expand All @@ -157,7 +157,7 @@ public void unclosedArray2() {
assertNull("Should throw an exception", new JSONArray("[\"test\""));
} catch (JSONException e) {
assertEquals("Expected an exception message",
"Expected a ',' or ']' at 7 [character 8 line 1]",
"Expected a ',' or ']' at index 7 [character number 8 in line 1]",
e.getMessage());
}
}
Expand All @@ -172,7 +172,7 @@ public void unclosedArray3() {
assertNull("Should throw an exception", new JSONArray("[\"test\","));
} catch (JSONException e) {
assertEquals("Expected an exception message",
"Expected a ',' or ']' at 8 [character 9 line 1]",
"Expected a ',' or ']' at index 8 [character number 9 in line 1]",
e.getMessage());
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/org/json/junit/JSONMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void emptyXMLException() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Bad XML at 0 [character 1 line 1]",
"Bad XML at index 0 [character number 1 in line 1]",
e.getMessage());
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public void nonXMLException() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Bad XML at 23 [character 24 line 1]",
"Bad XML at index 23 [character number 24 in line 1]",
e.getMessage());
}
}
Expand Down Expand Up @@ -205,7 +205,7 @@ public void invalidSlashInTagException() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Misshaped tag at 176 [character 14 line 4]",
"Misshaped tag at index 176 [character number 14 in line 4]",
e.getMessage());
}
}
Expand All @@ -230,7 +230,7 @@ public void invalidBangInTagException() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Misshaped meta tag at 215 [character 12 line 7]",
"Misshaped meta tag at index 215 [character number 12 in line 7]",
e.getMessage());
}
}
Expand Down Expand Up @@ -260,7 +260,7 @@ public void invalidBangNoCloseInTagException() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Misshaped meta tag at 214 [character 12 line 7]",
"Misshaped meta tag at index 214 [character number 12 in line 7]",
e.getMessage());
}
}
Expand Down Expand Up @@ -290,7 +290,7 @@ public void noCloseStartTagException() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Misplaced '<' at 194 [character 5 line 6]",
"Misplaced '<' at index 194 [character number 5 in line 6]",
e.getMessage());
}
}
Expand Down Expand Up @@ -350,7 +350,7 @@ public void noCloseEndBraceException() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Misplaced '<' at 206 [character 1 line 7]",
"Misplaced '<' at index 206 [character number 1 in line 7]",
e.getMessage());
}
}
Expand Down Expand Up @@ -380,7 +380,7 @@ public void invalidCDATABangInTagException() {
fail("Expecting an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Expected 'CDATA[' at 204 [character 11 line 5]",
"Expected 'CDATA[' at index 204 [character number 11 in line 5]",
e.getMessage());
}
}
Expand Down Expand Up @@ -815,7 +815,7 @@ public void testIssue484InfinteLoop1() {
fail("Exception expected for invalid JSON.");
} catch (JSONException ex) {
assertEquals("Exception string did not match: ",
"Unterminated string at 271 [character 272 line 1]",
"Unterminated string at index 271 [character number 272 in line 1]",
ex.getMessage());
}
}
Expand All @@ -829,7 +829,7 @@ public void testIssue484InfinteLoop2() {
fail("Exception expected for invalid JSON.");
} catch (JSONException ex) {
assertEquals("Exception string did not match: ",
"Unterminated string at 242 [character 238 line 2]",
"Unterminated string at index 242 [character number 238 in line 2]",
ex.getMessage());
}
}
Expand Down
Loading
Loading