From b860b6da4fffdd59ae839334405e3f0aec1146fe Mon Sep 17 00:00:00 2001 From: Eric Cochran Date: Sun, 15 Apr 2018 19:48:32 -0700 Subject: [PATCH] Make error message for dangling names consistent. (#501) --- .../java/com/squareup/moshi/JsonValueWriter.java | 5 ++++- .../test/java/com/squareup/moshi/JsonWriterTest.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/moshi/src/main/java/com/squareup/moshi/JsonValueWriter.java b/moshi/src/main/java/com/squareup/moshi/JsonValueWriter.java index cc532f2..9879739 100644 --- a/moshi/src/main/java/com/squareup/moshi/JsonValueWriter.java +++ b/moshi/src/main/java/com/squareup/moshi/JsonValueWriter.java @@ -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; diff --git a/moshi/src/test/java/com/squareup/moshi/JsonWriterTest.java b/moshi/src/test/java/com/squareup/moshi/JsonWriterTest.java index 0fa61fd..2fe44b6 100644 --- a/moshi/src/test/java/com/squareup/moshi/JsonWriterTest.java +++ b/moshi/src/test/java/com/squareup/moshi/JsonWriterTest.java @@ -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"); + } + } }