diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java index 81870bc9c4..ab2152e242 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java @@ -758,6 +758,7 @@ public void write(JsonWriter out, Locale value) throws IOException { private static final class EnumTypeAdapter> extends TypeAdapter { private final Map nameToConstant = new HashMap(); + private final Map stringToConstant = new HashMap(); private final Map constantToName = new HashMap(); public EnumTypeAdapter(final Class classOfT) { @@ -784,6 +785,8 @@ public EnumTypeAdapter(final Class classOfT) { @SuppressWarnings("unchecked") T constant = (T)(constantField.get(null)); String name = constant.name(); + String toStringVal = constant.toString(); + SerializedName annotation = constantField.getAnnotation(SerializedName.class); if (annotation != null) { name = annotation.value(); @@ -792,6 +795,7 @@ public EnumTypeAdapter(final Class classOfT) { } } nameToConstant.put(name, constant); + stringToConstant.put(toStringVal, constant); constantToName.put(constant, name); } } catch (IllegalAccessException e) { @@ -803,7 +807,9 @@ public EnumTypeAdapter(final Class classOfT) { in.nextNull(); return null; } - return nameToConstant.get(in.nextString()); + String key = in.nextString(); + T constant = nameToConstant.get(key); + return (constant == null) ? stringToConstant.get(key) : constant; } @Override public void write(JsonWriter out, T value) throws IOException { @@ -911,4 +917,4 @@ public static TypeAdapterFactory newTypeHierarchyFactory( } }; } -} \ No newline at end of file +}