Added tests to ensure that subclass objects that are of a parameterized type are serialized per their declared type in lists or maps.
This commit is contained in:
parent
40045dc2e4
commit
e60274ed35
|
@ -70,6 +70,49 @@ public class MoreSpecificTypeSerializationTest extends TestCase {
|
||||||
assertEquals(3, sub.get("s").getAsInt());
|
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<String>("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<ParameterizedBase<String>> list = new ArrayList<ParameterizedBase<String>>();
|
||||||
|
list.add(new ParameterizedBase<String>("one"));
|
||||||
|
list.add(new ParameterizedSub<String>("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<String, ParameterizedBase<String>> map = new HashMap<String, ParameterizedBase<String>>();
|
||||||
|
map.put("base", new ParameterizedBase<String>("one"));
|
||||||
|
map.put("sub", new ParameterizedSub<String>("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 {
|
private static class Base {
|
||||||
int b;
|
int b;
|
||||||
Base(int b) {
|
Base(int b) {
|
||||||
|
@ -100,4 +143,36 @@ public class MoreSpecificTypeSerializationTest extends TestCase {
|
||||||
this.map = map;
|
this.map = map;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class ParameterizedBase<T> {
|
||||||
|
T t;
|
||||||
|
ParameterizedBase(T t) {
|
||||||
|
this.t = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ParameterizedSub<T> extends ParameterizedBase<T> {
|
||||||
|
T s;
|
||||||
|
ParameterizedSub(T t, T s) {
|
||||||
|
super(t);
|
||||||
|
this.s = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ClassWithParameterizedBaseFields {
|
||||||
|
ParameterizedBase<String> b;
|
||||||
|
ClassWithParameterizedBaseFields(ParameterizedBase<String> b) {
|
||||||
|
this.b = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ClassWithContainersOfParameterizedBaseFields {
|
||||||
|
Collection<ParameterizedBase<String>> collection;
|
||||||
|
Map<String, ParameterizedBase<String>> map;
|
||||||
|
ClassWithContainersOfParameterizedBaseFields(Collection<ParameterizedBase<String>> collection,
|
||||||
|
Map<String, ParameterizedBase<String>> map) {
|
||||||
|
this.collection = collection;
|
||||||
|
this.map = map;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user