Package com.squareup.moshi.adapters
Class EnumJsonAdapter<T extends Enum<T>>
java.lang.Object
com.squareup.moshi.JsonAdapter<T>
com.squareup.moshi.adapters.EnumJsonAdapter<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 your
Moshi.Builder:
Moshi moshi = new Moshi.Builder()
.add(CurrencyCode.class, EnumJsonAdapter.create(CurrencyCode.class)
.withUnknownFallback(CurrencyCode.USD))
.build();
-
Nested Class Summary
Nested classes/interfaces inherited from class com.squareup.moshi.JsonAdapter
com.squareup.moshi.JsonAdapter.Factory -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Enum<T>>
EnumJsonAdapter<T>fromJson(com.squareup.moshi.JsonReader reader) voidtoString()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.Methods inherited from class com.squareup.moshi.JsonAdapter
failOnUnknown, fromJson, fromJson, fromJsonValue, indent, lenient, nonNull, nullSafe, serializeNulls, toJson, toJson, toJsonValue
-
Method Details
-
create
-
withUnknownFallback
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
- Specified by:
fromJsonin classcom.squareup.moshi.JsonAdapter<T extends Enum<T>>- Throws:
IOException
-
toJson
- Specified by:
toJsonin classcom.squareup.moshi.JsonAdapter<T extends Enum<T>>- Throws:
IOException
-
toString
-