Added support for runtime type determination while serializing array elements.
Created a utility class Reflection to hold methods to find Runtime type and creating new Instances.
This commit is contained in:
parent
aa067056c3
commit
6e3bf07300
@ -44,15 +44,17 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
|
||||
TypeAdapter<?> componentTypeAdapter = context.getAdapter(TypeToken.get(componentType));
|
||||
@SuppressWarnings("unchecked") // create() doesn't define a type parameter
|
||||
TypeAdapter<T> result = new ArrayTypeAdapter(
|
||||
componentTypeAdapter, $Gson$Types.getRawType(componentType));
|
||||
context, componentTypeAdapter, $Gson$Types.getRawType(componentType));
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
private final MiniGson context;
|
||||
private final Class<E> componentType;
|
||||
private final TypeAdapter<E> componentTypeAdapter;
|
||||
|
||||
public ArrayTypeAdapter(TypeAdapter<E> componentTypeAdapter, Class<E> componentType) {
|
||||
public ArrayTypeAdapter(MiniGson context, TypeAdapter<E> componentTypeAdapter, Class<E> componentType) {
|
||||
this.context = context;
|
||||
this.componentTypeAdapter = componentTypeAdapter;
|
||||
this.componentType = componentType;
|
||||
}
|
||||
@ -86,7 +88,10 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
|
||||
writer.beginArray();
|
||||
for (int i = 0, length = Array.getLength(array); i < length; i++) {
|
||||
final E value = (E) Array.get(array, i);
|
||||
componentTypeAdapter.write(writer, value);
|
||||
Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(componentType, array, value);
|
||||
TypeAdapter t = runtimeType != componentType ?
|
||||
context.getAdapter(TypeToken.get(runtimeType)) : componentTypeAdapter;
|
||||
t.write(writer, value);
|
||||
}
|
||||
writer.endArray();
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ public final class CollectionTypeAdapter<E> extends TypeAdapter<Collection<E>> {
|
||||
return null;
|
||||
}
|
||||
|
||||
Collection<E> collection = MiniGson.newInstance(constructor);
|
||||
Collection<E> collection = Reflection.newInstance(constructor);
|
||||
reader.beginArray();
|
||||
while (reader.hasNext()) {
|
||||
E instance = elementTypeAdapter.read(reader);
|
||||
|
@ -16,18 +16,17 @@
|
||||
|
||||
package com.google.gson.internal.bind;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
/**
|
||||
* A basic binding between JSON and Java objects.
|
||||
*/
|
||||
@ -67,23 +66,6 @@ public final class MiniGson {
|
||||
this.factories = Collections.unmodifiableList(factories);
|
||||
}
|
||||
|
||||
// TODO: this should use Joel's unsafe constructor stuff
|
||||
static <T> T newInstance(Constructor<T> constructor) {
|
||||
try {
|
||||
Object[] args = null;
|
||||
return constructor.newInstance(args);
|
||||
} catch (InstantiationException e) {
|
||||
// TODO: JsonParseException ?
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
// TODO: don't wrap if cause is unchecked!
|
||||
// TODO: JsonParseException ?
|
||||
throw new RuntimeException(e.getTargetException());
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type adapter for {@code} type.
|
||||
*
|
||||
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2011 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.google.gson.internal.bind;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
|
||||
final class Reflection {
|
||||
/**
|
||||
* Finds a compatible runtime type if it is more specific
|
||||
* In case of a field of an object, parent is the object instance, and child is the field value.
|
||||
* In case of an Array, parent is the array instance, and the child is the array element.
|
||||
*/
|
||||
public static Type getRuntimeTypeIfMoreSpecific(Type type, Object parent, Object child) {
|
||||
if (parent == null || child == null) {
|
||||
return type;
|
||||
}
|
||||
if (type == Object.class || type instanceof TypeVariable || type instanceof Class<?>) {
|
||||
type = (Class<?>) child.getClass();
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
// TODO: this should use Joel's unsafe constructor stuff
|
||||
public static <T> T newInstance(Constructor<T> constructor) {
|
||||
try {
|
||||
Object[] args = null;
|
||||
return constructor.newInstance(args);
|
||||
} catch (InstantiationException e) {
|
||||
// TODO: JsonParseException ?
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
// TODO: don't wrap if cause is unchecked!
|
||||
// TODO: JsonParseException ?
|
||||
throw new RuntimeException(e.getTargetException());
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -60,7 +60,7 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
|
||||
|
||||
T instance;
|
||||
if (constructor != null) {
|
||||
instance = (T) MiniGson.newInstance(constructor);
|
||||
instance = (T) Reflection.newInstance(constructor);
|
||||
} else {
|
||||
try {
|
||||
instance = (T) unsafeAllocator.newInstance(rawType);
|
||||
@ -122,7 +122,7 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
|
||||
throws IOException, IllegalAccessException {
|
||||
Object fieldValue = field.get(value);
|
||||
Type declaredTypeOfField = fieldType.getType();
|
||||
Type resolvedTypeOfField = getMoreSpecificType(declaredTypeOfField, value, fieldValue);
|
||||
Type resolvedTypeOfField = Reflection.getRuntimeTypeIfMoreSpecific(declaredTypeOfField, value, fieldValue);
|
||||
TypeAdapter t = resolvedTypeOfField != declaredTypeOfField ?
|
||||
context.getAdapter(TypeToken.get(resolvedTypeOfField)) : this.typeAdapter;
|
||||
t.write(writer, fieldValue);
|
||||
@ -135,16 +135,6 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
|
||||
};
|
||||
}
|
||||
|
||||
private static Type getMoreSpecificType(Type type, Object obj, Object fieldValue) {
|
||||
if (obj == null || fieldValue == null) {
|
||||
return type;
|
||||
}
|
||||
if (type == Object.class || type instanceof TypeVariable || type instanceof Class<?>) {
|
||||
type = (Class<?>) fieldValue.getClass();
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
public static class FactoryImpl implements Factory {
|
||||
public boolean serializeField(Class<?> declaringClazz, Field f, Type declaredType) {
|
||||
return true;
|
||||
|
@ -79,7 +79,7 @@ public final class StringToValueMapTypeAdapter<V> extends TypeAdapter<Map<String
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, V> map = MiniGson.newInstance(constructor);
|
||||
Map<String, V> map = Reflection.newInstance(constructor);
|
||||
reader.beginObject();
|
||||
while (reader.hasNext()) {
|
||||
String key = reader.nextName();
|
||||
|
Loading…
Reference in New Issue
Block a user