diff --git a/gson/GSON 2.0 NOTES.txt b/gson/GSON 2.0 NOTES.txt index 9fa8c930..82a216a0 100644 --- a/gson/GSON 2.0 NOTES.txt +++ b/gson/GSON 2.0 NOTES.txt @@ -45,4 +45,11 @@ GSON 2.x lets the runtime throw a StackOverflowError com.google.gson.functional.CircularReferenceTest.testCircularSerialization com.google.gson.functional.CircularReferenceTest.testSelfReferenceSerialization com.google.gson.functional.CircularReferenceTest.testSelfReferenceArrayFieldSerialization -com.google.gson.functional.CircularReferenceTest.testSelfReferenceCustomHandlerSerialization \ No newline at end of file +com.google.gson.functional.CircularReferenceTest.testSelfReferenceCustomHandlerSerialization + + +GSON 1.x sometimes sets subclass fields when an InstanceCreator returns a subclass. + This occurs only when the value is a field of another object: not when its + the top level object or a collection element. +GSON 2.x sets fields of the requested type only +com.google.gson.functional.InstanceCreatorTest.testInstanceCreatorReturnsSubTypeForField diff --git a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java index b9646330..eaa4d98a 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java @@ -25,7 +25,6 @@ import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; - import java.io.IOException; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; @@ -63,9 +62,7 @@ public class ReflectiveTypeAdapterFactory implements TypeAdapter.Factory { } ObjectConstructor constructor = constructorConstructor.getConstructor(type); - - return new Adapter(context, constructor, type, - getBoundFields(context, type, raw)); + return new Adapter(constructor, getBoundFields(context, type, raw)); } private ReflectiveTypeAdapterFactory.BoundField createBoundField( @@ -142,16 +139,11 @@ public class ReflectiveTypeAdapterFactory implements TypeAdapter.Factory { } public final class Adapter extends TypeAdapter { - private final MiniGson context; private final ObjectConstructor constructor; - private final TypeToken type; private final Map boundFields; - private Adapter(MiniGson context, ObjectConstructor constructor, - TypeToken type, Map boundFields) { - this.context = context; + private Adapter(ObjectConstructor constructor, Map boundFields) { this.constructor = constructor; - this.type = type; this.boundFields = boundFields; } @@ -194,16 +186,6 @@ public class ReflectiveTypeAdapterFactory implements TypeAdapter.Factory { return; } - // TODO: GSON includes subclass fields during serialization -// if (false) { -// Class runtimeType = value.getClass(); -// if (runtimeType != type.getRawType()) { -// TypeAdapter adapter = context.getAdapter(runtimeType); -// ((TypeAdapter) adapter).write(writer, value); -// return; -// } -// } - writer.beginObject(); try { for (BoundField boundField : boundFields.values()) { diff --git a/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java b/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java index e673cf86..8446cb37 100644 --- a/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java +++ b/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java @@ -77,6 +77,6 @@ public class InstanceCreatorTest extends TestCase { String json = "{base:{baseName:'Base',subName:'SubRevised'}}"; ClassWithBaseField target = gson.fromJson(json, ClassWithBaseField.class); assertTrue(target.base instanceof Sub); - assertEquals("SubRevised", ((Sub)target.base).subName); + assertEquals(Sub.SUB_NAME, ((Sub)target.base).subName); } }