Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore JSON object trailing comma in lenient JsonUtf8Reader. #1841

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions moshi/src/main/java/com/squareup/moshi/JsonUtf8Reader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,15 @@ internal class JsonUtf8Reader : JsonReader {
PEEKED_SINGLE_QUOTED_NAME
}

'}' -> if (peekStack != JsonScope.NONEMPTY_OBJECT) {
'}' -> {
if (peekStack == JsonScope.NONEMPTY_OBJECT && !lenient) {
throw syntaxError(
"Expected name. Use JsonReader.setLenient(true) to accept " +
"malformed JSON",
)
}
buffer.readByte() // consume the '}'.
PEEKED_END_OBJECT
} else {
throw syntaxError("Expected name")
}

else -> {
Expand All @@ -213,8 +217,7 @@ internal class JsonUtf8Reader : JsonReader {
}
}
}
peeked = next
return next
return setPeeked(next)
}

JsonScope.DANGLING_NAME -> {
Expand Down
59 changes: 45 additions & 14 deletions moshi/src/test/java/com/squareup/moshi/JsonUtf8ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1260,20 +1260,6 @@ public void strictExtraCommasInMaps() throws IOException {
}
}

@Test
public void lenientExtraCommasInMaps() throws IOException {
JsonReader reader = newReader("{\"a\":\"b\",}");
reader.setLenient(true);
reader.beginObject();
assertThat(reader.nextName()).isEqualTo("a");
assertThat(reader.nextString()).isEqualTo("b");
try {
reader.peek();
fail();
} catch (JsonEncodingException expected) {
}
}

@Test
public void malformedDocuments() throws IOException {
assertDocument("{]", BEGIN_OBJECT, JsonEncodingException.class);
Expand Down Expand Up @@ -1356,6 +1342,51 @@ public void lenientInvalidEscape() throws IOException {
assertThat(reader.nextString()).isEqualTo("string");
}

@Test
public void trailingCommaInJsonObject() throws Exception {
JsonReader reader = newReader("{\"a\":1,}");
reader.beginObject();
assertThat(reader.nextName()).isEqualTo("a");
assertThat(reader.nextInt()).isEqualTo(1);
try {
reader.endObject();
fail();
} catch (JsonEncodingException expected) {
assertThat(expected.getMessage())
.isEqualTo(
"Expected name. Use JsonReader.setLenient(true) to accept malformed "
+ "JSON at path $.a");
}
reader = newReader("{\"a\": 1, }");
reader.beginObject();
assertThat(reader.nextName()).isEqualTo("a");
assertThat(reader.nextInt()).isEqualTo(1);
reader.setLenient(true);
reader.endObject();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
}

@Test
public void trailingCommaInJsonArray() throws Exception {
JsonReader reader = newReader("[1, ]");
reader.beginArray();
assertThat(reader.nextInt()).isEqualTo(1);
try {
reader.endArray();
fail();
} catch (JsonEncodingException expected) {
assertThat(expected.getMessage())
.isEqualTo("Use JsonReader.setLenient(true) to accept malformed JSON at path $[1]");
}
reader = newReader("[1,]");
reader.beginArray();
assertThat(reader.nextInt()).isEqualTo(1);
reader.setLenient(true);
reader.nextNull();
reader.endArray();
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
}

private void assertDocument(String document, Object... expectations) throws IOException {
JsonReader reader = newReader(document);
reader.setLenient(true);
Expand Down