Package com.google.gson.typeadapters
Class RuntimeTypeAdapterFactory<T>
- java.lang.Object
-
- com.google.gson.typeadapters.RuntimeTypeAdapterFactory<T>
-
- All Implemented Interfaces:
TypeAdapterFactory
public final class RuntimeTypeAdapterFactory<T> extends java.lang.Object implements TypeAdapterFactory
Adapts values whose runtime type may differ from their declaration type. This is necessary when a field's type is not the same type that GSON should create when deserializing that field. For example, consider these types:abstract class Shape { int x; int y; } class Circle extends Shape { int radius; } class Rectangle extends Shape { int width; int height; } class Diamond extends Shape { int width; int height; } class Drawing { Shape bottomShape; Shape topShape; }
Without additional type information, the serialized JSON is ambiguous. Is the bottom shape in this drawing a rectangle or a diamond?
{ "bottomShape": { "width": 10, "height": 5, "x": 0, "y": 0 }, "topShape": { "radius": 2, "x": 4, "y": 1 } }
{ "bottomShape": { "type": "Diamond", "width": 10, "height": 5, "x": 0, "y": 0 }, "topShape": { "type": "Circle", "radius": 2, "x": 4, "y": 1 } }
"type"
) and the type labels ("Rectangle"
) are configurable.Registering Types
Create aRuntimeTypeAdapterFactory
by passing the base type and type field name to theof(java.lang.Class<T>, java.lang.String, boolean)
factory method. If you don't supply an explicit type field name,"type"
will be used.RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class, "type");
shapeAdapterFactory.registerSubtype(Rectangle.class, "Rectangle"); shapeAdapterFactory.registerSubtype(Circle.class, "Circle"); shapeAdapterFactory.registerSubtype(Diamond.class, "Diamond");
Gson gson = new GsonBuilder() .registerTypeAdapterFactory(shapeAdapterFactory) .create();
GsonBuilder
, this API supports chaining:RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class) .registerSubtype(Rectangle.class) .registerSubtype(Circle.class) .registerSubtype(Diamond.class);
Serialization and deserialization
In order to serialize and deserialize a polymorphic object, you must specify the base type explicitly.Diamond diamond = new Diamond(); String json = gson.toJson(diamond, Shape.class);
Shape shape = gson.fromJson(json, Shape.class);
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Class<?>
baseType
private java.util.Map<java.lang.String,java.lang.Class<?>>
labelToSubtype
private boolean
maintainType
private boolean
recognizeSubtypes
private java.util.Map<java.lang.Class<?>,java.lang.String>
subtypeToLabel
private java.lang.String
typeFieldName
-
Constructor Summary
Constructors Modifier Constructor Description private
RuntimeTypeAdapterFactory(java.lang.Class<?> baseType, java.lang.String typeFieldName, boolean maintainType)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> TypeAdapter<R>
create(Gson gson, TypeToken<R> type)
Returns a type adapter fortype
, or null if this factory doesn't supporttype
.static <T> RuntimeTypeAdapterFactory<T>
of(java.lang.Class<T> baseType)
Creates a new runtime type adapter forbaseType
using"type"
as the type field name.static <T> RuntimeTypeAdapterFactory<T>
of(java.lang.Class<T> baseType, java.lang.String typeFieldName)
Creates a new runtime type adapter using forbaseType
usingtypeFieldName
as the type field name.static <T> RuntimeTypeAdapterFactory<T>
of(java.lang.Class<T> baseType, java.lang.String typeFieldName, boolean maintainType)
Creates a new runtime type adapter using forbaseType
usingtypeFieldName
as the type field name.RuntimeTypeAdapterFactory<T>
recognizeSubtypes()
Ensures that this factory will handle not just the givenbaseType
, but any subtype of that type.RuntimeTypeAdapterFactory<T>
registerSubtype(java.lang.Class<? extends T> type)
Registerstype
identified by itssimple name
.RuntimeTypeAdapterFactory<T>
registerSubtype(java.lang.Class<? extends T> type, java.lang.String label)
Registerstype
identified bylabel
.
-
-
-
Field Detail
-
baseType
private final java.lang.Class<?> baseType
-
typeFieldName
private final java.lang.String typeFieldName
-
labelToSubtype
private final java.util.Map<java.lang.String,java.lang.Class<?>> labelToSubtype
-
subtypeToLabel
private final java.util.Map<java.lang.Class<?>,java.lang.String> subtypeToLabel
-
maintainType
private final boolean maintainType
-
recognizeSubtypes
private boolean recognizeSubtypes
-
-
Method Detail
-
of
public static <T> RuntimeTypeAdapterFactory<T> of(java.lang.Class<T> baseType, java.lang.String typeFieldName, boolean maintainType)
Creates a new runtime type adapter using forbaseType
usingtypeFieldName
as the type field name. Type field names are case sensitive.- Parameters:
maintainType
- true if the type field should be included in deserialized objects
-
of
public static <T> RuntimeTypeAdapterFactory<T> of(java.lang.Class<T> baseType, java.lang.String typeFieldName)
Creates a new runtime type adapter using forbaseType
usingtypeFieldName
as the type field name. Type field names are case sensitive.
-
of
public static <T> RuntimeTypeAdapterFactory<T> of(java.lang.Class<T> baseType)
Creates a new runtime type adapter forbaseType
using"type"
as the type field name.
-
recognizeSubtypes
public RuntimeTypeAdapterFactory<T> recognizeSubtypes()
Ensures that this factory will handle not just the givenbaseType
, but any subtype of that type.
-
registerSubtype
public RuntimeTypeAdapterFactory<T> registerSubtype(java.lang.Class<? extends T> type, java.lang.String label)
Registerstype
identified bylabel
. Labels are case sensitive.- Throws:
java.lang.IllegalArgumentException
- if eithertype
orlabel
have already been registered on this type adapter.
-
registerSubtype
public RuntimeTypeAdapterFactory<T> registerSubtype(java.lang.Class<? extends T> type)
Registerstype
identified by itssimple name
. Labels are case sensitive.- Throws:
java.lang.IllegalArgumentException
- if eithertype
or its simple name have already been registered on this type adapter.
-
create
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type)
Description copied from interface:TypeAdapterFactory
Returns a type adapter fortype
, or null if this factory doesn't supporttype
.- Specified by:
create
in interfaceTypeAdapterFactory
-
-