Class RuntimeTypeAdapterFactory<T>
java.lang.Object
com.mapbox.geojson.internal.typeadapters.RuntimeTypeAdapterFactory<T>
- Type Parameters:
T- base type for this factory
- 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:
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
}
}
This class addresses this problem by adding type information to the
serialized JSON and honoring that type information when the JSON is
deserialized:
{
"bottomShape": {
"type": "Diamond",
"width": 10,
"height": 5,
"x": 0,
"y": 0
},
"topShape": {
"type": "Circle",
"radius": 2,
"x": 4,
"y": 1
}
}
Both the type field name ("type") and the type labels (
"Rectangle") are configurable.
Registering Types
Create aRuntimeTypeAdapterFactory by passing the base type and type field
name to the of(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");
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.
shapeAdapterFactory.registerSubtype(Rectangle.class, "Rectangle");
shapeAdapterFactory.registerSubtype(Circle.class, "Circle");
shapeAdapterFactory.registerSubtype(Diamond.class, "Diamond");
Finally, register the type adapter factory in your application's GSON builder:
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(shapeAdapterFactory)
.create();
Like GsonBuilder, this API supports chaining:
RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory =
RuntimeTypeAdapterFactory.of(Shape.class)
.registerSubtype(Rectangle.class)
.registerSubtype(Circle.class)
.registerSubtype(Diamond.class);
- Since:
- 4.6.0
-
Method Summary
Modifier and TypeMethodDescription<R> com.google.gson.TypeAdapter<R>create(com.google.gson.Gson gson, com.google.gson.reflect.TypeToken<R> type) static <T> RuntimeTypeAdapterFactory<T>Creates a new runtime type adapter forbaseTypeusing"type"as the type field name.static <T> RuntimeTypeAdapterFactory<T>Creates a new runtime type adapter using forbaseTypeusingtypeFieldNameas the type field name.static <T> RuntimeTypeAdapterFactory<T>Creates a new runtime type adapter using forbaseTypeusingtypeFieldNameas the type field name.registerSubtype(Class<? extends T> type) Registerstypeidentified by itssimple name.registerSubtype(Class<? extends T> type, String label) Registerstypeidentified bylabel.
-
Method Details
-
of
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName, boolean maintainType) Creates a new runtime type adapter using forbaseTypeusingtypeFieldNameas the type field name. Type field names are case sensitive.maintainTypeflag decide if the type will be stored in pojo or not.- Type Parameters:
T- base type- Parameters:
baseType- class of base typetypeFieldName- field name used to distinguish subtypesmaintainType- true if the type will be stored in pojo, false - otherwise- Returns:
- The RuntimeTypeAdaptorFactory instance created
-
of
Creates a new runtime type adapter using forbaseTypeusingtypeFieldNameas the type field name. Type field names are case sensitive.- Type Parameters:
T- base type- Parameters:
baseType- class of base typetypeFieldName- field name used to distinguish subtypes- Returns:
- The RuntimeTypeAdaptorFactory instance created
-
of
Creates a new runtime type adapter forbaseTypeusing"type"as the type field name.- Type Parameters:
T- base type- Parameters:
baseType- class of base type- Returns:
- The RuntimeTypeAdaptorFactory instance created
-
registerSubtype
Registerstypeidentified bylabel. Labels are case sensitive.- Parameters:
type- class of subtype of base typelabel- string value for field that distinguishes subtypes- Returns:
- The same object it is called on, so the calls can be chained
- Throws:
IllegalArgumentException- if eithertypeorlabelhave already been registered on this type adapter.
-
registerSubtype
Registerstypeidentified by itssimple name. Labels are case sensitive.- Parameters:
type- type name- Returns:
- The same object it is called on, so the calls can be chained
- Throws:
IllegalArgumentException- if eithertypeor its simple name have already been registered on this type adapter.
-
create
public <R> com.google.gson.TypeAdapter<R> create(com.google.gson.Gson gson, com.google.gson.reflect.TypeToken<R> type) - Specified by:
createin interfacecom.google.gson.TypeAdapterFactory
-