Skip to content

Commit

Permalink
Support EnumMap deserialization (#2071)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 authored Feb 4, 2022
1 parent e2e851c commit 565b7a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -199,7 +200,26 @@ private <T> ObjectConstructor<T> newDefaultImplementationConstructor(
}

if (Map.class.isAssignableFrom(rawType)) {
if (ConcurrentNavigableMap.class.isAssignableFrom(rawType)) {
// Only support creation of EnumMap, but not of custom subtypes; for them type parameters
// and constructor parameter might have completely different meaning
if (rawType == EnumMap.class) {
return new ObjectConstructor<T>() {
@Override public T construct() {
if (type instanceof ParameterizedType) {
Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
if (elementType instanceof Class) {
@SuppressWarnings("rawtypes")
T map = (T) new EnumMap((Class) elementType);
return map;
} else {
throw new JsonIOException("Invalid EnumMap type: " + type.toString());
}
} else {
throw new JsonIOException("Invalid EnumMap type: " + type.toString());
}
}
};
} else if (ConcurrentNavigableMap.class.isAssignableFrom(rawType)) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new ConcurrentSkipListMap<Object, Object>();
Expand Down
17 changes: 17 additions & 0 deletions gson/src/test/java/com/google/gson/functional/EnumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
/**
Expand Down Expand Up @@ -150,13 +153,27 @@ public void testEnumCaseMapping() {
public void testEnumSet() {
EnumSet<Roshambo> foo = EnumSet.of(Roshambo.ROCK, Roshambo.PAPER);
String json = gson.toJson(foo);
assertEquals("[\"ROCK\",\"PAPER\"]", json);

Type type = new TypeToken<EnumSet<Roshambo>>() {}.getType();
EnumSet<Roshambo> bar = gson.fromJson(json, type);
assertTrue(bar.contains(Roshambo.ROCK));
assertTrue(bar.contains(Roshambo.PAPER));
assertFalse(bar.contains(Roshambo.SCISSORS));
}

public void testEnumMap() throws Exception {
EnumMap<MyEnum, String> map = new EnumMap<MyEnum, String>(MyEnum.class);
map.put(MyEnum.VALUE1, "test");
String json = gson.toJson(map);
assertEquals("{\"VALUE1\":\"test\"}", json);

Type type = new TypeToken<EnumMap<MyEnum, String>>() {}.getType();
EnumMap<?, ?> actualMap = gson.fromJson("{\"VALUE1\":\"test\"}", type);
Map<?, ?> expectedMap = Collections.singletonMap(MyEnum.VALUE1, "test");
assertEquals(expectedMap, actualMap);
}

public enum Roshambo {
ROCK {
@Override Roshambo defeats() {
Expand Down

0 comments on commit 565b7a1

Please sign in to comment.