mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Merge pull request #640 from square/eric.null-enum
Allow null fallback enum value in EnumJsonAdapter.
This commit is contained in:
@@ -19,10 +19,11 @@ public final class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
|
|||||||
final String[] nameStrings;
|
final String[] nameStrings;
|
||||||
final T[] constants;
|
final T[] constants;
|
||||||
final JsonReader.Options options;
|
final JsonReader.Options options;
|
||||||
|
final boolean useFallbackValue;
|
||||||
final @Nullable T fallbackValue;
|
final @Nullable T fallbackValue;
|
||||||
|
|
||||||
public static <T extends Enum<T>> EnumJsonAdapter<T> create(Class<T> enumType) {
|
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
|
* null, absent, or not a string. Also, the string values are case-sensitive, and this fallback
|
||||||
* value will be used even on case mismatches.
|
* value will be used even on case mismatches.
|
||||||
*/
|
*/
|
||||||
public EnumJsonAdapter<T> withUnknownFallback(T fallbackValue) {
|
public EnumJsonAdapter<T> withUnknownFallback(@Nullable T fallbackValue) {
|
||||||
if (fallbackValue == null) {
|
return new EnumJsonAdapter<>(enumType, fallbackValue, true);
|
||||||
throw new NullPointerException("fallbackValue == null");
|
|
||||||
}
|
|
||||||
return new EnumJsonAdapter<>(enumType, fallbackValue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EnumJsonAdapter(Class<T> enumType, @Nullable T fallbackValue) {
|
EnumJsonAdapter(Class<T> enumType, @Nullable T fallbackValue, boolean useFallbackValue) {
|
||||||
this.enumType = enumType;
|
this.enumType = enumType;
|
||||||
this.fallbackValue = fallbackValue;
|
this.fallbackValue = fallbackValue;
|
||||||
|
this.useFallbackValue = useFallbackValue;
|
||||||
try {
|
try {
|
||||||
constants = enumType.getEnumConstants();
|
constants = enumType.getEnumConstants();
|
||||||
nameStrings = new String[constants.length];
|
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];
|
if (index != -1) return constants[index];
|
||||||
|
|
||||||
String path = reader.getPath();
|
String path = reader.getPath();
|
||||||
if (fallbackValue == null) {
|
if (!useFallbackValue) {
|
||||||
String name = reader.nextString();
|
String name = reader.nextString();
|
||||||
throw new JsonDataException("Expected one of "
|
throw new JsonDataException("Expected one of "
|
||||||
+ Arrays.asList(nameStrings) + " but was " + name + " at path " + path);
|
+ 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);
|
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 {
|
enum Roshambo {
|
||||||
ROCK,
|
ROCK,
|
||||||
PAPER,
|
PAPER,
|
||||||
|
Reference in New Issue
Block a user