Merge pull request #640 from square/eric.null-enum
Allow null fallback enum value in EnumJsonAdapter.
This commit is contained in:
commit
3c470575f4
2 changed files with 15 additions and 8 deletions
|
@ -19,10 +19,11 @@ public final class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
|
|||
final String[] nameStrings;
|
||||
final T[] constants;
|
||||
final JsonReader.Options options;
|
||||
final boolean useFallbackValue;
|
||||
final @Nullable T fallbackValue;
|
||||
|
||||
public static <T extends Enum<T>> EnumJsonAdapter<T> create(Class<T> enumType) {
|
||||
return new EnumJsonAdapter<>(enumType, null);
|
||||
return new EnumJsonAdapter<>(enumType, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,16 +32,14 @@ public final class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
|
|||
* null, absent, or not a string. Also, the string values are case-sensitive, and this fallback
|
||||
* value will be used even on case mismatches.
|
||||
*/
|
||||
public EnumJsonAdapter<T> withUnknownFallback(T fallbackValue) {
|
||||
if (fallbackValue == null) {
|
||||
throw new NullPointerException("fallbackValue == null");
|
||||
}
|
||||
return new EnumJsonAdapter<>(enumType, fallbackValue);
|
||||
public EnumJsonAdapter<T> withUnknownFallback(@Nullable T fallbackValue) {
|
||||
return new EnumJsonAdapter<>(enumType, fallbackValue, true);
|
||||
}
|
||||
|
||||
EnumJsonAdapter(Class<T> enumType, @Nullable T fallbackValue) {
|
||||
EnumJsonAdapter(Class<T> enumType, @Nullable T fallbackValue, boolean useFallbackValue) {
|
||||
this.enumType = enumType;
|
||||
this.fallbackValue = fallbackValue;
|
||||
this.useFallbackValue = useFallbackValue;
|
||||
try {
|
||||
constants = enumType.getEnumConstants();
|
||||
nameStrings = new String[constants.length];
|
||||
|
@ -61,7 +60,7 @@ public final class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
|
|||
if (index != -1) return constants[index];
|
||||
|
||||
String path = reader.getPath();
|
||||
if (fallbackValue == null) {
|
||||
if (!useFallbackValue) {
|
||||
String name = reader.nextString();
|
||||
throw new JsonDataException("Expected one of "
|
||||
+ Arrays.asList(nameStrings) + " but was " + name + " at path " + path);
|
||||
|
|
|
@ -44,6 +44,14 @@ public final class EnumJsonAdapterTest {
|
|||
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
|
||||
}
|
||||
|
||||
@Test public void withNullFallbackValue() throws Exception {
|
||||
EnumJsonAdapter<Roshambo> adapter = EnumJsonAdapter.create(Roshambo.class)
|
||||
.withUnknownFallback(null);
|
||||
JsonReader reader = JsonReader.of(new Buffer().writeUtf8("\"SPOCK\""));
|
||||
assertThat(adapter.fromJson(reader)).isNull();
|
||||
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
|
||||
}
|
||||
|
||||
enum Roshambo {
|
||||
ROCK,
|
||||
PAPER,
|
||||
|
|
Loading…
Reference in a new issue