summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2015-01-21 19:17:47 -0800
committerElliott Hughes <enh@google.com>2015-01-22 13:14:43 -0800
commit8e2f8aff8b2c9493ebffb8ad3825e58d02253471 (patch)
tree4889b9959ab5a6e4271fdd971af7235dd0f4ff3a
parent1d5df1fbd8c786e0f936cb0175e4a3f288fc1890 (diff)
downloadlibcore-8e2f8aff8b2c9493ebffb8ad3825e58d02253471.zip
libcore-8e2f8aff8b2c9493ebffb8ad3825e58d02253471.tar.gz
libcore-8e2f8aff8b2c9493ebffb8ad3825e58d02253471.tar.bz2
Throw JSONException rather than NumberFormatException for an invalid escape.
Only our documentation claims that any of the JSON code throws NumberFormatException, and throwing JSONException seems much more in keeping with the rest of the API (and makes it easier for callers to handle errors). Bug: https://code.google.com/p/android/issues/detail?id=103641 Change-Id: I4f4ebfd983f4ccb2a1f266a0bfa5732174df26f6
-rw-r--r--json/src/main/java/org/json/JSONTokener.java11
-rw-r--r--json/src/test/java/org/json/JSONObjectTest.java9
2 files changed, 14 insertions, 6 deletions
diff --git a/json/src/main/java/org/json/JSONTokener.java b/json/src/main/java/org/json/JSONTokener.java
index 202e2e6..8caecc8 100644
--- a/json/src/main/java/org/json/JSONTokener.java
+++ b/json/src/main/java/org/json/JSONTokener.java
@@ -188,8 +188,6 @@ public class JSONTokener {
* not include it in the returned string.
*
* @param quote either ' or ".
- * @throws NumberFormatException if any unicode escape sequences are
- * malformed.
*/
public String nextString(char quote) throws JSONException {
/*
@@ -235,9 +233,6 @@ public class JSONTokener {
* immediately follow a backslash. The backslash '\' should have already
* been read. This supports both unicode escapes "u000A" and two-character
* escapes "\n".
- *
- * @throws NumberFormatException if any unicode escape sequences are
- * malformed.
*/
private char readEscapeCharacter() throws JSONException {
char escaped = in.charAt(pos++);
@@ -248,7 +243,11 @@ public class JSONTokener {
}
String hex = in.substring(pos, pos + 4);
pos += 4;
- return (char) Integer.parseInt(hex, 16);
+ try {
+ return (char) Integer.parseInt(hex, 16);
+ } catch (NumberFormatException nfe) {
+ throw syntaxError("Invalid escape sequence: " + hex);
+ }
case 't':
return '\t';
diff --git a/json/src/test/java/org/json/JSONObjectTest.java b/json/src/test/java/org/json/JSONObjectTest.java
index e89db94..a1b7b13 100644
--- a/json/src/test/java/org/json/JSONObjectTest.java
+++ b/json/src/test/java/org/json/JSONObjectTest.java
@@ -1029,4 +1029,13 @@ public class JSONObjectTest extends TestCase {
} catch (JSONException e) {
}
}
+
+ // https://code.google.com/p/android/issues/detail?id=103641
+ public void testInvalidUnicodeEscape() {
+ try {
+ new JSONObject("{\"q\":\"\\u\", \"r\":[]}");
+ fail();
+ } catch (JSONException expected) {
+ }
+ }
}