diff --git a/gson/src/test/java/com/google/gson/functional/MoreSpecificTypeSerializationTest.java b/gson/src/test/java/com/google/gson/functional/MoreSpecificTypeSerializationTest.java index 2232b2dc..8f890403 100644 --- a/gson/src/test/java/com/google/gson/functional/MoreSpecificTypeSerializationTest.java +++ b/gson/src/test/java/com/google/gson/functional/MoreSpecificTypeSerializationTest.java @@ -70,6 +70,49 @@ public class MoreSpecificTypeSerializationTest extends TestCase { assertEquals(3, sub.get("s").getAsInt()); } + /** + * For parameterized type, Gson ignores the more-specific type and sticks to the declared type + */ + public void testParameterizedSubclassFields() { + ClassWithParameterizedBaseFields target = new ClassWithParameterizedBaseFields( + new ParameterizedSub("one", "two")); + String json = gson.toJson(target); + assertTrue(json.contains("\"t\":\"one\"")); + assertFalse(json.contains("\"s\"")); + } + + /** + * For parameterized type in a List, Gson ignores the more-specific type and sticks to + * the declared type + */ + public void testListOfParameterizedSubclassFields() { + Collection> list = new ArrayList>(); + list.add(new ParameterizedBase("one")); + list.add(new ParameterizedSub("two", "three")); + ClassWithContainersOfParameterizedBaseFields target = + new ClassWithContainersOfParameterizedBaseFields(list, null); + String json = gson.toJson(target); + assertTrue(json, json.contains("{\"t\":\"one\"}")); + assertFalse(json, json.contains("\"s\":")); + } + + /** + * For parameterized type in a map, Gson ignores the more-specific type and sticks to the + * declared type + */ + public void testMapOfParameterizedSubclassFields() { + Map> map = new HashMap>(); + map.put("base", new ParameterizedBase("one")); + map.put("sub", new ParameterizedSub("two", "three")); + ClassWithContainersOfParameterizedBaseFields target = + new ClassWithContainersOfParameterizedBaseFields(null, map); + JsonObject json = gson.toJsonTree(target).getAsJsonObject().get("map").getAsJsonObject(); + assertEquals("one", json.get("base").getAsJsonObject().get("t").getAsString()); + JsonObject sub = json.get("sub").getAsJsonObject(); + assertEquals("two", sub.get("t").getAsString()); + assertNull(sub.get("s")); + } + private static class Base { int b; Base(int b) { @@ -100,4 +143,36 @@ public class MoreSpecificTypeSerializationTest extends TestCase { this.map = map; } } + + private static class ParameterizedBase { + T t; + ParameterizedBase(T t) { + this.t = t; + } + } + + private static class ParameterizedSub extends ParameterizedBase { + T s; + ParameterizedSub(T t, T s) { + super(t); + this.s = s; + } + } + + private static class ClassWithParameterizedBaseFields { + ParameterizedBase b; + ClassWithParameterizedBaseFields(ParameterizedBase b) { + this.b = b; + } + } + + private static class ClassWithContainersOfParameterizedBaseFields { + Collection> collection; + Map> map; + ClassWithContainersOfParameterizedBaseFields(Collection> collection, + Map> map) { + this.collection = collection; + this.map = map; + } + } }