From 70c7728218bafe63c6ef6242a84f474a576ae4c0 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Thu, 30 Oct 2008 18:26:16 +0000 Subject: [PATCH] Wrote a test that invokes a custom deserializer for a wrapper primitive type. --- .../functional/CustomTypeAdaptersTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java index 79ddb246..f600a49f 100644 --- a/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java @@ -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() { + 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; + } }