From 7ee3e2787f45f5453b61a9af6c659993b4eba6d5 Mon Sep 17 00:00:00 2001 From: "Mahmut H. Kocas" Date: Tue, 22 Feb 2022 00:11:43 +0300 Subject: [PATCH] Parsing Map - 501 (#1950) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added parsing support for enum that has overridden toString() method. * Fix a tiny formatting problem * Fixed formatting issue Co-authored-by: Éamonn McManus --- .../com/google/gson/internal/bind/TypeAdapters.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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 4188f754..ef3a5594 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 @@ -775,6 +775,7 @@ public final class TypeAdapters { 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) { @@ -801,6 +802,8 @@ public final class TypeAdapters { @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(); @@ -809,6 +812,7 @@ public final class TypeAdapters { } } nameToConstant.put(name, constant); + stringToConstant.put(toStringVal, constant); constantToName.put(constant, name); } } catch (IllegalAccessException e) { @@ -820,7 +824,9 @@ public final class TypeAdapters { 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 { @@ -928,4 +934,4 @@ public final class TypeAdapters { } }; } -} \ No newline at end of file +}