Package de.fraunhofer.iese.mydata.common
Class RuntimeTypeAdapterFactory<T>
- java.lang.Object
-
- de.fraunhofer.iese.mydata.common.RuntimeTypeAdapterFactory<T>
-
- Type Parameters:
T
- the generic type
- All Implemented Interfaces:
com.google.gson.TypeAdapterFactory
public final class RuntimeTypeAdapterFactory<T> extends Object implements com.google.gson.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:{ @code 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>)
factory method. If you don't supply an explicit type field name,"type"
will be used.{ @code RuntimeTypeAdapterFactory<Shape$gt; shapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class, "type"); }
Next register all of your subtypes. Every subtype must be explicitly registered. This protects your application from injection attacks. If you don't supply an explicit type label, the type's simple name will be used.shapeAdapter.registerSubtype(Rectangle.class, "Rectangle"); shapeAdapter.registerSubtype(Circle.class, "Circle"); shapeAdapter.registerSubtype(Diamond.class, "Diamond");
{ @code Gson gson = new GsonBuilder().registerTypeAdapterFactory(shapeAdapterFactory).create(); }
LikeGsonBuilder
, this API supports chaining:{ @code RuntimeTypeAdapterFactory shapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class).registerSubtype(Rectangle.class).registerSubtype(Circle.class).registerSubtype(Diamond.class); }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> com.google.gson.TypeAdapter<R>
create(com.google.gson.Gson gson, com.google.gson.reflect.TypeToken<R> type)
static <T> RuntimeTypeAdapterFactory<T>
of(Class<T> baseType)
Creates a new runtime type adapter forbaseType
using"type"
as the type field name.static <T> RuntimeTypeAdapterFactory<T>
of(Class<T> baseType, String typeFieldName)
Creates a new runtime type adapter using forbaseType
usingtypeFieldName
as the type field name.RuntimeTypeAdapterFactory<T>
registerSubtype(Class<? extends T> type)
Registerstype
identified by itssimple name
.RuntimeTypeAdapterFactory<T>
registerSubtype(Class<? extends T> type, String label)
Registerstype
identified bylabel
.
-
-
-
Method Detail
-
of
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType)
Creates a new runtime type adapter forbaseType
using"type"
as the type field name.- Type Parameters:
T
- the generic type- Parameters:
baseType
- the base type- Returns:
- the runtime type adapter factory
-
of
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName)
Creates a new runtime type adapter using forbaseType
usingtypeFieldName
as the type field name. Type field names are case sensitive.- Type Parameters:
T
- the generic type- Parameters:
baseType
- the base typetypeFieldName
- the type field name- Returns:
- the runtime type adapter factory
-
create
public <R> com.google.gson.TypeAdapter<R> create(com.google.gson.Gson gson, com.google.gson.reflect.TypeToken<R> type)
- Specified by:
create
in interfacecom.google.gson.TypeAdapterFactory
-
registerSubtype
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type)
Registerstype
identified by itssimple name
. Labels are case sensitive.- Parameters:
type
- the type- Returns:
- the runtime type adapter factory
- Throws:
IllegalArgumentException
- if eithertype
or its simple name have already been registered on this type adapter.
-
registerSubtype
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label)
Registerstype
identified bylabel
. Labels are case sensitive.- Parameters:
type
- the typelabel
- the label- Returns:
- the runtime type adapter factory
- Throws:
IllegalArgumentException
- if eithertype
orlabel
have already been registered on this type adapter.
-
-