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 fec728c..02023b7 100644
--- a/adapters/src/main/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactory.java
+++ b/adapters/src/main/java/com/squareup/moshi/adapters/PolymorphicJsonAdapterFactory.java
@@ -29,6 +29,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.annotation.CheckReturnValue;
+import javax.annotation.Nullable;
/**
* A JsonAdapter factory for objects that include type information in the JSON. When decoding JSON
@@ -100,19 +101,31 @@ import javax.annotation.CheckReturnValue;
*
If an unknown subtype is encountered when decoding, this will throw a {@link
* JsonDataException}. If an unknown type is encountered when encoding, this will throw an {@link
* IllegalArgumentException}.
+ *
+ *
If you want to specify a custom unknown fallback for decoding, you can do so via
+ * {@link #withDefaultValue(Object)}. This instance should be immutable, as it is shared.
*/
public final class PolymorphicJsonAdapterFactory implements JsonAdapter.Factory {
final Class baseType;
final String labelKey;
final List labels;
final List subtypes;
+ @Nullable final T defaultValue;
+ final boolean defaultValueSet;
PolymorphicJsonAdapterFactory(
- Class baseType, String labelKey, List labels, List subtypes) {
+ Class baseType,
+ String labelKey,
+ List labels,
+ List subtypes,
+ @Nullable T defaultValue,
+ boolean defaultValueSet) {
this.baseType = baseType;
this.labelKey = labelKey;
this.labels = labels;
this.subtypes = subtypes;
+ this.defaultValue = defaultValue;
+ this.defaultValueSet = defaultValueSet;
}
/**
@@ -125,7 +138,12 @@ public final class PolymorphicJsonAdapterFactory implements JsonAdapter.Facto
if (baseType == null) throw new NullPointerException("baseType == null");
if (labelKey == null) throw new NullPointerException("labelKey == null");
return new PolymorphicJsonAdapterFactory<>(
- baseType, labelKey, Collections.emptyList(), Collections.emptyList());
+ baseType,
+ labelKey,
+ Collections.emptyList(),
+ Collections.emptyList(),
+ null,
+ false);
}
/**
@@ -143,7 +161,25 @@ public final class PolymorphicJsonAdapterFactory implements JsonAdapter.Facto
newLabels.add(label);
List newSubtypes = new ArrayList<>(subtypes);
newSubtypes.add(subtype);
- return new PolymorphicJsonAdapterFactory<>(baseType, labelKey, newLabels, newSubtypes);
+ return new PolymorphicJsonAdapterFactory<>(baseType,
+ labelKey,
+ newLabels,
+ newSubtypes,
+ defaultValue,
+ defaultValueSet);
+ }
+
+ /**
+ * Returns a new factory that with default to {@code defaultValue} upon decoding of unrecognized
+ * labels. The default value should be immutable.
+ */
+ public PolymorphicJsonAdapterFactory withDefaultValue(@Nullable T defaultValue) {
+ return new PolymorphicJsonAdapterFactory<>(baseType,
+ labelKey,
+ labels,
+ subtypes,
+ defaultValue,
+ true);
}
@Override
@@ -157,7 +193,13 @@ public final class PolymorphicJsonAdapterFactory implements JsonAdapter.Facto
jsonAdapters.add(moshi.adapter(subtypes.get(i)));
}
- return new PolymorphicJsonAdapter(labelKey, labels, subtypes, jsonAdapters).nullSafe();
+ return new PolymorphicJsonAdapter(labelKey,
+ labels,
+ subtypes,
+ jsonAdapters,
+ defaultValue,
+ defaultValueSet
+ ).nullSafe();
}
static final class PolymorphicJsonAdapter extends JsonAdapter