mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Fix JsonUtf8Writer to be strict about names in the wrong place. (#502)
This commit is contained in:
committed by
Jesse Wilson
parent
941229b6c9
commit
78091aeb46
@@ -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;
|
||||
|
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user