Wrote a test that invokes a custom deserializer for a wrapper primitive type.

This commit is contained in:
Inderjeet Singh 2008-10-30 18:26:16 +00:00
parent 3f53e8b223
commit 70c7728218
1 changed files with 25 additions and 0 deletions

View File

@ -195,4 +195,29 @@ public class CustomTypeAdaptersTest extends TestCase {
return context.serialize(src, typeOfSrc);
}
}
public void testCustomDeserializerForLong() {
final ClassWithBooleanField customDeserializerInvoked = new ClassWithBooleanField();
customDeserializerInvoked.value = false;
Gson gson = new GsonBuilder().registerTypeAdapter(Long.class, new JsonDeserializer<Long>() {
public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
customDeserializerInvoked.value = true;
String str = json.getAsJsonPrimitive().getAsString();
return str.length() == 0 ? null : Long.parseLong(str);
}
}).create();
String json = "{'value':null}";
ClassWithWrapperLongField target = gson.fromJson(json, ClassWithWrapperLongField.class);
assertNull(target.value);
assertTrue(customDeserializerInvoked.value);
}
private static class ClassWithWrapperLongField {
Long value;
}
private static class ClassWithBooleanField {
Boolean value;
}
}