From 6d2de1f6f22dfe5b384bb48ffb8321b24ea86ab2 Mon Sep 17 00:00:00 2001 From: TheDoctorOne Date: Sat, 4 Sep 2021 02:14:57 +0300 Subject: [PATCH 1/3] Added parsing support for enum that has overridden toString() method. --- .../java/com/google/gson/internal/bind/TypeAdapters.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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 81dda90359..fdc6a4a46a 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 @@ -756,6 +756,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(Class classOfT) { @@ -773,6 +774,7 @@ public EnumTypeAdapter(Class classOfT) { @SuppressWarnings("unchecked") T constant = (T)(field.get(null)); String name = constant.name(); + String toStringVal = constant.toString(); SerializedName annotation = field.getAnnotation(SerializedName.class); if (annotation != null) { name = annotation.value(); @@ -781,6 +783,7 @@ public EnumTypeAdapter(Class classOfT) { } } nameToConstant.put(name, constant); + stringToConstant.put(toStringVal, constant); constantToName.put(constant, name); } } catch (IllegalAccessException e) { @@ -792,7 +795,11 @@ public EnumTypeAdapter(Class classOfT) { in.nextNull(); return null; } - return nameToConstant.get(in.nextString()); + String key = in.nextString(); + T constant = nameToConstant.get(key); + if(constant == null) + return stringToConstant.get(key); + return constant; } @Override public void write(JsonWriter out, T value) throws IOException { From 61aa59f14160d6406b22b76b32435852c7b3381c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Tue, 23 Nov 2021 09:03:40 -0800 Subject: [PATCH 2/3] Fix a tiny formatting problem --- .../main/java/com/google/gson/internal/bind/TypeAdapters.java | 4 ++-- 1 file changed, 2 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 a9872a06e6..6896dccc62 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 @@ -809,7 +809,7 @@ public EnumTypeAdapter(final Class classOfT) { } String key = in.nextString(); T constant = nameToConstant.get(key); - if(constant == null) + if (constant == null) return stringToConstant.get(key); return constant; } @@ -919,4 +919,4 @@ public static TypeAdapterFactory newTypeHierarchyFactory( } }; } -} \ No newline at end of file +} From 74c4d6243ab6cfc949153676a7637ea0b7663368 Mon Sep 17 00:00:00 2001 From: "Mahmut H. Kocas" Date: Mon, 21 Feb 2022 11:16:21 +0300 Subject: [PATCH 3/3] Fixed formatting issue --- .../main/java/com/google/gson/internal/bind/TypeAdapters.java | 4 +--- 1 file changed, 1 insertion(+), 3 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 6896dccc62..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 @@ -809,9 +809,7 @@ public EnumTypeAdapter(final Class classOfT) { } String key = in.nextString(); T constant = nameToConstant.get(key); - if (constant == null) - return stringToConstant.get(key); - return constant; + return (constant == null) ? stringToConstant.get(key) : constant; } @Override public void write(JsonWriter out, T value) throws IOException {