From dfa651e7777f9bd3bb7b8b8f80cac6af90b2ac45 Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Thu, 2 Jun 2016 15:56:48 +0200 Subject: [PATCH 1/2] Make nextString throw a JSONException instead of a NumberFormatException for malformed input. --- JSONTokener.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/JSONTokener.java b/JSONTokener.java index 32548ed..92fe5c1 100644 --- a/JSONTokener.java +++ b/JSONTokener.java @@ -278,7 +278,11 @@ public class JSONTokener { sb.append('\r'); break; case 'u': - sb.append((char)Integer.parseInt(this.next(4), 16)); + try { + sb.append((char)Integer.parseInt(this.next(4), 16)); + } catch (NumberFormatException e) { + throw this.syntaxError("Illegal escape."); + } break; case '"': case '\'': From 16a86d73df322dc696e0dc38b4fdd49f65175497 Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Thu, 2 Jun 2016 16:11:15 +0200 Subject: [PATCH 2/2] Pass in the throwable that caused the error. --- JSONTokener.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/JSONTokener.java b/JSONTokener.java index 92fe5c1..7702b19 100644 --- a/JSONTokener.java +++ b/JSONTokener.java @@ -281,7 +281,7 @@ public class JSONTokener { try { sb.append((char)Integer.parseInt(this.next(4), 16)); } catch (NumberFormatException e) { - throw this.syntaxError("Illegal escape."); + throw this.syntaxError("Illegal escape.", e); } break; case '"': @@ -437,6 +437,16 @@ public class JSONTokener { return new JSONException(message + this.toString()); } + /** + * Make a JSONException to signal a syntax error. + * + * @param message The error message. + * @param causedBy The throwable that caused the error. + * @return A JSONException object, suitable for throwing + */ + public JSONException syntaxError(String message, Throwable causedBy) { + return new JSONException(message + this.toString(), causedBy); + } /** * Make a printable string of this JSONTokener.