Skip to content

Commit

Permalink
Explicitly throw JsonException for unparseable input + some tests
Browse files Browse the repository at this point in the history
Change-Id: I36f456a05111f5d90a7d471a3927497c792faf79
  • Loading branch information
Ilia Motornyi committed Apr 18, 2017
1 parent c8ea1ac commit 7fa023f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions elemental/src/elemental/json/impl/JsonTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ String nextString(int startChar) throws JsonException {
case '\r':
case '\n':
throw new JsonException("");
case INVALID_CHAR:
throw new JsonException("Invalid string: closing " + startChar + " is not found");
case '\\':
c = next();
switch (c) {
Expand Down
66 changes: 66 additions & 0 deletions elemental/tests/elemental/json/JsonUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,72 @@ public void testIllegalParse() {
}
}

public void testUnclosedString() {
try {
JsonUtil.parse("\"a");
fail("Expected JsonException to be thrown");
} catch (JsonException je) {
// Expected
}
}

public void testUnclosedEmptyString() {
try {
JsonUtil.parse("\"");
fail("Expected JsonException to be thrown");
} catch (JsonException je) {
// Expected
}
}

public void testUnclosedArray() {
try {
JsonUtil.parse("[1");
fail("Expected JsonException to be thrown");
} catch (JsonException je) {
// Expected
}
}

public void testUnclosedEmptyArray() {
try {
JsonUtil.parse("[");
fail("Expected JsonException to be thrown");
} catch (JsonException je) {
// Expected
}
}

public void testUnclosedObject() {
try {
JsonUtil.parse("{'a");
fail("Expected JsonException to be thrown");
} catch (JsonException je) {
// Expected
}
try {
JsonUtil.parse("{'a'");
fail("Expected JsonException to be thrown");
} catch (JsonException je) {
// Expected
}
try {
JsonUtil.parse("{'a':");
fail("Expected JsonException to be thrown");
} catch (JsonException je) {
// Expected
}
}

public void testUnclosedEmptyObject() {
try {
JsonUtil.parse("{");
fail("Expected JsonException to be thrown");
} catch (JsonException je) {
// Expected
}
}

public void testLegalParse() {
JsonValue obj = JsonUtil.parse(
"{ \"a\":1, \"b\":\"hello\", \"c\": true,"
Expand Down

0 comments on commit 7fa023f

Please sign in to comment.