Package com.squareup.moshi.adapters
Class EnumJsonAdapter<T extends java.lang.Enum<T>>
- java.lang.Object
-
- com.squareup.moshi.JsonAdapter<T>
-
- com.squareup.moshi.adapters.EnumJsonAdapter<T>
-
public final class EnumJsonAdapter<T extends java.lang.Enum<T>> extends com.squareup.moshi.JsonAdapter<T>A JsonAdapter for enums that allows having a fallback enum value when a deserialized string does not match any enum value. To use, add this as an adapter for your enum type on yourMoshi.Builder:Moshi moshi = new Moshi.Builder() .add(CurrencyCode.class, EnumJsonAdapter.create(CurrencyCode.class) .withUnknownFallback(CurrencyCode.USD)) .build();
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T extends java.lang.Enum<T>>
EnumJsonAdapter<T>create(java.lang.Class<T> enumType)TfromJson(com.squareup.moshi.JsonReader reader)voidtoJson(com.squareup.moshi.JsonWriter writer, T value)java.lang.StringtoString()EnumJsonAdapter<T>withUnknownFallback(T fallbackValue)Create a new adapter for this enum with a fallback value to use when the JSON string does not match any of the enum's constants.
-
-
-
Method Detail
-
create
public static <T extends java.lang.Enum<T>> EnumJsonAdapter<T> create(java.lang.Class<T> enumType)
-
withUnknownFallback
public EnumJsonAdapter<T> withUnknownFallback(@Nullable T fallbackValue)
Create a new adapter for this enum with a fallback value to use when the JSON string does not match any of the enum's constants. Note that this value will not be used when the JSON value is null, absent, or not a string. Also, the string values are case-sensitive, and this fallback value will be used even on case mismatches.
-
fromJson
@Nullable public T fromJson(com.squareup.moshi.JsonReader reader) throws java.io.IOException
-
toJson
public void toJson(com.squareup.moshi.JsonWriter writer, T value) throws java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-