Fix JsonUtf8Writer to be strict about names in the wrong place. (#502)

This commit is contained in:
Eric Cochran
2018-04-15 06:39:04 -07:00
committed by Jesse Wilson
parent 941229b6c9
commit 78091aeb46
2 changed files with 35 additions and 1 deletions

View File

@@ -138,7 +138,8 @@ final class JsonUtf8Writer extends JsonWriter {
if (stackSize == 0) {
throw new IllegalStateException("JsonWriter is closed.");
}
if (deferredName != null) {
int context = peekScope();
if ((context != EMPTY_OBJECT && context != NONEMPTY_OBJECT) || deferredName != null) {
throw new IllegalStateException("Nesting problem.");
}
deferredName = name;

View File

@@ -571,4 +571,37 @@ public final class JsonWriterTest {
writer.close();
writer.close();
}
@Test public void nameNotInObjectFails() throws IOException {
JsonWriter writer = factory.newWriter();
try {
writer.name("a");
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Nesting problem.");
}
}
@Test public void missingValueInObjectIsANestingProblem() throws IOException {
JsonWriter writer = factory.newWriter();
writer.beginObject();
writer.name("a");
try {
writer.name("b");
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Nesting problem.");
}
}
@Test public void nameInArrayIsANestingProblem() throws IOException {
JsonWriter writer = factory.newWriter();
writer.beginArray();
try {
writer.name("a");
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Nesting problem.");
}
}
}