From d347128e6fbfbe307cfbfde384235f43c80045f1 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Sat, 25 Jun 2011 20:04:14 +0000 Subject: [PATCH] Implemented support for serializing objects of type Bar without the need to specify their type explicitly in toJson method. --- .../google/gson/ReflectingFieldNavigator.java | 17 ++++++++++++++++- .../gson/functional/RawSerializationTest.java | 12 +++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/gson/src/main/java/com/google/gson/ReflectingFieldNavigator.java b/gson/src/main/java/com/google/gson/ReflectingFieldNavigator.java index eee28e61..0e88b77f 100644 --- a/gson/src/main/java/com/google/gson/ReflectingFieldNavigator.java +++ b/gson/src/main/java/com/google/gson/ReflectingFieldNavigator.java @@ -18,6 +18,7 @@ package com.google.gson; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; import java.util.ArrayList; import java.util.List; @@ -59,7 +60,7 @@ final class ReflectingFieldNavigator { || exclusionStrategy.shouldSkipClass(fieldAttributes.getDeclaredClass())) { continue; // skip } - Type resolvedTypeOfField = fieldAttributes.getResolvedType(); + Type resolvedTypeOfField = getMoreSpecificType(fieldAttributes.getResolvedType(), obj, fieldAttributes); boolean visitedWithCustomHandler = visitor.visitFieldUsingCustomHandler(fieldAttributes, resolvedTypeOfField, obj); if (!visitedWithCustomHandler) { @@ -72,6 +73,20 @@ final class ReflectingFieldNavigator { } } + @SuppressWarnings("unchecked") + private Type getMoreSpecificType(Type type, Object obj, FieldAttributes fieldAttributes) { + try { + if (obj != null && (Object.class == type || type instanceof TypeVariable)) { + Object fieldValue = fieldAttributes.get(obj); + if (fieldValue != null) { + type = fieldValue.getClass(); + } + } + } catch (IllegalAccessException e) { + } + return type; + } + private List getAllFields(Type type, Type declaredType) { List fields = fieldsCache.getElement(type); if (fields == null) { diff --git a/gson/src/test/java/com/google/gson/functional/RawSerializationTest.java b/gson/src/test/java/com/google/gson/functional/RawSerializationTest.java index da5e93d6..de0322bf 100644 --- a/gson/src/test/java/com/google/gson/functional/RawSerializationTest.java +++ b/gson/src/test/java/com/google/gson/functional/RawSerializationTest.java @@ -18,7 +18,6 @@ package com.google.gson.functional; import java.util.Arrays; import java.util.Collection; -import junit.framework.AssertionFailedError; import junit.framework.TestCase; import com.google.gson.Gson; @@ -54,12 +53,11 @@ public class RawSerializationTest extends TestCase { public void testParameterizedObject() { Bar bar = new Bar(new Foo(1)); String expectedJson = "{\"t\":{\"b\":1}}"; - try { - String json = gson.toJson(bar); - assertEquals(expectedJson, json); - } catch (AssertionFailedError expected) { - } - String json = gson.toJson(bar, new TypeToken>(){}.getType()); + // Ensure that serialization works without specifying the type explicitly + String json = gson.toJson(bar); + assertEquals(expectedJson, json); + // Ensure that serialization also works when the type is specified explicitly + json = gson.toJson(bar, new TypeToken>(){}.getType()); assertEquals(expectedJson, json); }