diff --git a/adapters/src/main/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactory.java b/adapters/src/main/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactory.java index 16b819b..712d091 100644 --- a/adapters/src/main/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactory.java +++ b/adapters/src/main/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactory.java @@ -100,30 +100,22 @@ import javax.annotation.Nullable; * *

If an unknown subtype is encountered when decoding: *

* *

If an unknown type is encountered when encoding: *

* *

If the same subtype has multiple labels the first one is used when encoding. */ public final class PolymorphicJsonAdapterFactory implements JsonAdapter.Factory { - /** - * Thin wrapper around {@link JsonAdapter} to allow {@link PolymorphicJsonAdapter} to - * distinguish between {@code JsonAdapter} added due to a {@code defaultValue} or added - * by users of {@link PolymorphicJsonAdapterFactory}. - */ - private abstract static class DefaultJsonAdapter extends JsonAdapter { - } - final Class baseType; final String labelKey; final List labels; @@ -205,19 +197,15 @@ public final class PolymorphicJsonAdapterFactory implements JsonAdapter.Facto } private JsonAdapter buildFallbackJsonAdapter(final T defaultValue) { - return new DefaultJsonAdapter() { - @Nullable - @Override - public Object fromJson(JsonReader reader) throws IOException { + return new JsonAdapter() { + @Override public @Nullable Object fromJson(JsonReader reader) throws IOException { reader.skipValue(); return defaultValue; } - @Override - public void toJson(JsonWriter writer, @Nullable Object value) throws IOException { - throw new IOException("This method should never be called. " - + "If you find this on your stacktraces please report it " - + "to the https://github.com/square/moshi project"); + @Override public void toJson(JsonWriter writer, Object value) throws IOException { + throw new IllegalArgumentException("Expected one of " + subtypes + " but found " + value + + ", a " + value.getClass() + ". Register this subtype."); } }; } @@ -295,13 +283,8 @@ public final class PolymorphicJsonAdapterFactory implements JsonAdapter.Facto int labelIndex = reader.selectString(labelOptions); if (labelIndex == -1 && this.fallbackJsonAdapter == null) { - throw new JsonDataException("Expected one of " - + labels - + " for key '" - + labelKey - + "' but found '" - + reader.nextString() - + "'. Register a subtype for this label."); + throw new JsonDataException("Expected one of " + labels + " for key '" + labelKey + + "' but found '" + reader.nextString() + "'. Register a subtype for this label."); } return labelIndex; } @@ -314,17 +297,11 @@ public final class PolymorphicJsonAdapterFactory implements JsonAdapter.Facto int labelIndex = subtypes.indexOf(type); final JsonAdapter adapter; if (labelIndex == -1) { - if (fallbackJsonAdapter == null || fallbackJsonAdapter instanceof DefaultJsonAdapter) { - throw new IllegalArgumentException("Expected one of " - + subtypes - + " but found " - + value - + ", a " - + value.getClass() - + ". Register this subtype."); - } else { - adapter = fallbackJsonAdapter; + if (fallbackJsonAdapter == null) { + throw new IllegalArgumentException("Expected one of " + subtypes + " but found " + value + + ", a " + value.getClass() + ". Register this subtype."); } + adapter = fallbackJsonAdapter; } else { adapter = jsonAdapters.get(labelIndex); } diff --git a/adapters/src/test/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactoryTest.java b/adapters/src/test/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactoryTest.java index dd01ef1..e8745bb 100644 --- a/adapters/src/test/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactoryTest.java +++ b/adapters/src/test/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactoryTest.java @@ -23,11 +23,10 @@ import com.squareup.moshi.Moshi; import java.io.IOException; import java.util.Collections; import java.util.Map; +import javax.annotation.Nullable; import okio.Buffer; import org.junit.Test; -import javax.annotation.Nullable; - import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -114,15 +113,18 @@ public final class PolymorphicJsonAdapterFactoryTest { .withSubtype(Success.class, "success") .withSubtype(Error.class, "error") .withFallbackJsonAdapter(new JsonAdapter() { - @Override - public Object fromJson(JsonReader reader) throws IOException { - reader.skipValue(); + @Override public Object fromJson(JsonReader reader) throws IOException { + reader.beginObject(); + assertThat(reader.nextName()).isEqualTo("type"); + assertThat(reader.nextString()).isEqualTo("data"); + assertThat(reader.nextName()).isEqualTo("value"); + assertThat(reader.nextString()).isEqualTo("Okay!"); + reader.endObject(); return new EmptyMessage(); } - @Override - public void toJson(JsonWriter writer, @Nullable Object value) { - throw new RuntimeException("Not implemented as not needed for the test"); + @Override public void toJson(JsonWriter writer, @Nullable Object value) { + throw new AssertionError(); } }) ) @@ -185,14 +187,11 @@ public final class PolymorphicJsonAdapterFactoryTest { .withSubtype(Success.class, "success") .withSubtype(Error.class, "error") .withFallbackJsonAdapter(new JsonAdapter() { - @Nullable - @Override - public Object fromJson(JsonReader reader) { + @Override public Object fromJson(JsonReader reader) { throw new RuntimeException("Not implemented as not needed for the test"); } - @Override - public void toJson(JsonWriter writer, @Nullable Object value) throws IOException { + @Override public void toJson(JsonWriter writer, Object value) throws IOException { writer.name("type").value("injected by fallbackJsonAdapter"); } }))