mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Fail on invalid escapes.
Related to https://github.com/google/gson/issues/824
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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");
|
||||
|
||||
|
Reference in New Issue
Block a user