Make error message for dangling names consistent. (#501)

This commit is contained in:
Eric Cochran
2018-04-15 19:48:32 -07:00
committed by GitHub
parent 3a2367036c
commit b860b6da4f
2 changed files with 16 additions and 1 deletions

View File

@@ -76,9 +76,12 @@ final class JsonValueWriter extends JsonWriter {
}
@Override public JsonWriter endObject() throws IOException {
if (peekScope() != EMPTY_OBJECT || deferredName != null) {
if (peekScope() != EMPTY_OBJECT) {
throw new IllegalStateException("Nesting problem.");
}
if (deferredName != null) {
throw new IllegalStateException("Dangling name: " + deferredName);
}
promoteValueToName = false;
stackSize--;
stack[stackSize] = null;

View File

@@ -604,4 +604,16 @@ public final class JsonWriterTest {
assertThat(expected).hasMessage("Nesting problem.");
}
}
@Test public void danglingNameFails() throws IOException {
JsonWriter writer = factory.newWriter();
writer.beginObject();
writer.name("a");
try {
writer.endObject();
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Dangling name: a");
}
}
}