Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing Map<Enum,Obj> - 501 #1950

Merged
merged 4 commits into from Feb 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -758,6 +758,7 @@ public void write(JsonWriter out, Locale value) throws IOException {

private static final class EnumTypeAdapter<T extends Enum<T>> extends TypeAdapter<T> {
private final Map<String, T> nameToConstant = new HashMap<String, T>();
private final Map<String, T> stringToConstant = new HashMap<String, T>();
private final Map<T, String> constantToName = new HashMap<T, String>();

public EnumTypeAdapter(final Class<T> classOfT) {
Expand All @@ -784,6 +785,8 @@ public EnumTypeAdapter(final Class<T> 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();
Expand All @@ -792,6 +795,7 @@ public EnumTypeAdapter(final Class<T> classOfT) {
}
}
nameToConstant.put(name, constant);
stringToConstant.put(toStringVal, constant);
constantToName.put(constant, name);
}
} catch (IllegalAccessException e) {
Expand All @@ -803,7 +807,9 @@ public EnumTypeAdapter(final Class<T> 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 {
Expand Down Expand Up @@ -911,4 +917,4 @@ public static <T1> TypeAdapterFactory newTypeHierarchyFactory(
}
};
}
}
}