-
- All Implemented Interfaces:
-
com.google.gson.TypeAdapterFactory
public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactoryAdapts 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?
This class addresses this problem by adding type information to the serialized JSON and honoring that type information when the JSON is deserialized:{@code * { * "bottomShape": { * "width": 10, * "height": 5, * "x": 0, * "y": 0 * }, * "topShape": { * "radius": 2, * "x": 4, * "y": 1 * } * } * }
Both the type field name ({@code * { * "bottomShape": { * "type": "Diamond", * "width": 10, * "height": 5, * "x": 0, * "y": 0 * }, * "topShape": { * "type": "Circle", * "radius": 2, * "x": 4, * "y": 1 * } * } * }{@code "type"}) and the type labels ({@code "Rectangle"}) are configurable. Registering TypesCreate a{@code RuntimeTypeAdapterFactory}by passing the base type and type field name to the of factory method. If you don't supply an explicit type field name,{@code "type"}will be used.
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.{@code * RuntimeTypeAdapterFactoryshapeAdapterFactory * = RuntimeTypeAdapterFactory.of(Shape.class, "type"); * }
Finally, register the type adapter factory in your application's GSON builder:{@code * shapeAdapter.registerSubtype(Rectangle.class, "Rectangle"); * shapeAdapter.registerSubtype(Circle.class, "Circle"); * shapeAdapter.registerSubtype(Diamond.class, "Diamond"); * }
Like{@code * Gson gson = new GsonBuilder() * .registerTypeAdapterFactory(shapeAdapterFactory) * .create(); * }{@code GsonBuilder}, this API supports chaining:{@code * RuntimeTypeAdapterFactoryshapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class) * .registerSubtype(Rectangle.class) * .registerSubtype(Circle.class) * .registerSubtype(Diamond.class); * }
-
-
Method Summary
Modifier and Type Method Description static <T> RuntimeTypeAdapterFactory<T>of(Class<T> baseType, String typeFieldName)Creates a new runtime type adapter using for {@code baseType}using{@code typeFieldName}asthe type field name.static <T> RuntimeTypeAdapterFactory<T>of(Class<T> baseType)Creates a new runtime type adapter for {@code baseType}using{@code "type"}as the type fieldname.RuntimeTypeAdapterFactory<T>registerSubtype(Class<out T> type, String label)Registers {@code type}identified by{@code label}.RuntimeTypeAdapterFactory<T>registerSubtype(Class<out T> type)Registers {@code type}identified by its simple name.<R> TypeAdapter<R>create(Gson gson, TypeToken<R> type)-
-
Method Detail
-
of
static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName)
Creates a new runtime type adapter using for
{@code baseType}using{@code typeFieldName}asthe type field name. Type field names are case sensitive.
-
of
static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType)
Creates a new runtime type adapter for
{@code baseType}using{@code "type"}as the type fieldname.
-
registerSubtype
RuntimeTypeAdapterFactory<T> registerSubtype(Class<out T> type, String label)
Registers
{@code type}identified by{@code label}. Labels are case sensitive.
-
registerSubtype
RuntimeTypeAdapterFactory<T> registerSubtype(Class<out T> type)
Registers
{@code type}identified by its simple name. Labels arecase sensitive.
-
-
-
-