Merge pull request #166 from square/jwilson.0602.invalid_escapes

Fail on invalid escapes.
This commit is contained in:
Jake Wharton
2016-06-02 10:12:59 -04:00
2 changed files with 21 additions and 0 deletions

View File

@@ -1081,7 +1081,10 @@ final class BufferedSourceJsonReader extends JsonReader {
case '\'':
case '"':
case '\\':
return (char) escaped;
default:
if (!lenient) throw syntaxError("Invalid escape sequence: \\" + (char) escaped);
return (char) escaped;
}
}

View File

@@ -1771,6 +1771,24 @@ public final class BufferedSourceJsonReaderTest {
}
}
@Test public void invalidEscape() throws IOException {
JsonReader reader = newReader("[\"str\\ing\"]");
reader.beginArray();
try {
reader.nextString();
fail();
} catch (IOException expected) {
assertThat(expected).hasMessage("Invalid escape sequence: \\i at path $[0]");
}
}
@Test public void lenientInvalidEscape() throws IOException {
JsonReader reader = newReader("[\"str\\ing\"]");
reader.setLenient(true);
reader.beginArray();
assertThat(reader.nextString()).isEqualTo("string");
}
@Test public void selectName() throws IOException {
JsonReader.Options abc = JsonReader.Options.of("a", "b", "c");