Parity with Gson behavior where we use runtime type of an object while serializing instead of the declared type of the field.
This commit is contained in:
parent
5c620c7e0a
commit
2813385c33
|
@ -74,7 +74,7 @@ final class ReflectingFieldNavigator {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private Type getMoreSpecificType(Type type, Object obj, FieldAttributes fieldAttributes) {
|
private static Type getMoreSpecificType(Type type, Object obj, FieldAttributes fieldAttributes) {
|
||||||
try {
|
try {
|
||||||
if (obj != null && (Object.class == type || type instanceof TypeVariable)) {
|
if (obj != null && (Object.class == type || type instanceof TypeVariable)) {
|
||||||
Object fieldValue = fieldAttributes.get(obj);
|
Object fieldValue = fieldAttributes.get(obj);
|
||||||
|
|
|
@ -21,6 +21,7 @@ import java.lang.reflect.AccessibleObject;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.lang.reflect.TypeVariable;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -122,7 +123,11 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
|
||||||
@Override void write(JsonWriter writer, Object value)
|
@Override void write(JsonWriter writer, Object value)
|
||||||
throws IOException, IllegalAccessException {
|
throws IOException, IllegalAccessException {
|
||||||
Object fieldValue = field.get(value);
|
Object fieldValue = field.get(value);
|
||||||
((TypeAdapter) typeAdapter).write(writer, fieldValue);
|
Type declaredTypeOfField = fieldType.getType();
|
||||||
|
Type resolvedTypeOfField = getMoreSpecificType(declaredTypeOfField, value, fieldValue);
|
||||||
|
TypeAdapter t = resolvedTypeOfField != declaredTypeOfField ?
|
||||||
|
context.getAdapter(TypeToken.get(resolvedTypeOfField)) : this.typeAdapter;
|
||||||
|
t.write(writer, fieldValue);
|
||||||
}
|
}
|
||||||
@Override void read(JsonReader reader, Object value)
|
@Override void read(JsonReader reader, Object value)
|
||||||
throws IOException, IllegalAccessException {
|
throws IOException, IllegalAccessException {
|
||||||
|
@ -132,6 +137,15 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Type getMoreSpecificType(Type type, Object obj, Object fieldValue) {
|
||||||
|
if (obj != null && (Object.class == type || type instanceof TypeVariable)) {
|
||||||
|
if (fieldValue != null) {
|
||||||
|
type = fieldValue.getClass();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
public static class FactoryImpl implements Factory {
|
public static class FactoryImpl implements Factory {
|
||||||
public boolean serializeField(Class<?> declaringClazz, Field f, Type declaredType) {
|
public boolean serializeField(Class<?> declaringClazz, Field f, Type declaredType) {
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user