test for issue 58

This commit is contained in:
Inderjeet Singh 2008-10-14 18:54:34 +00:00
parent 90a8d6cf76
commit 646d94d420
1 changed files with 26 additions and 0 deletions

View File

@ -125,6 +125,32 @@ public class CustomTypeAdaptersTest extends TestCase {
assertEquals(7, target.getBag().getIntValue());
}
public void testCustomTypeAdapterAppliesToSubClasses() {
Gson gson = new GsonBuilder().registerTypeAdapter(Base.class, new JsonSerializer<Base> () {
public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("value", src.baseValue);
return json;
}
}).create();
Base b = new Base();
String json = gson.toJson(b);
assertTrue(json.contains("value"));
b = new Derived();
json = gson.toJson(b, Base.class);
assertTrue(json.contains("value"));
assertFalse(json.contains("derivedValue"));
}
private static class Base {
int baseValue = 2;
}
private static class Derived extends Base {
int derivedValue = 3;
}
private Gson createGsonObjectWithFooTypeAdapter() {
return new GsonBuilder().registerTypeAdapter(Foo.class, new FooTypeAdapter()).create();
}