Class AbstractAdapter<A>

java.lang.Object
edu.uky.cs.nil.tt.io.AbstractAdapter<A>
Type Parameters:
A - the abstract type this adapter will handle
All Implemented Interfaces:
com.google.gson.JsonDeserializer<A>, com.google.gson.TypeAdapterFactory

public class AbstractAdapter<A> extends Object implements com.google.gson.TypeAdapterFactory, com.google.gson.JsonDeserializer<A>
An abstract adapter is used to configure a GsonBuilder to read and write objects that share a common parent class, including abstract types like interfaces and abstract classes. When an object of this adapter's type is written to JSON, a key is added to annotate the object with its runtime type. When that object is later read from JSON as the parent type, this type information is used to create the correct kind of object.

For example, Message is an abstract class, so while a Message can be written to JSON, it cannot be read as class Message, since that class cannot be instantiated. An abstract adapter can be used like this:


GsonBuilder builder = new GsonBuilder();
AbstractAdapter<Message> adapter = new AbstractAdapter<>(Message.class);
builder.registerTypeAdapterFactory(adapter);
builder.registerTypeAdapter(Message.class, adapter);
When a Message object is written to JSON, it will now include an additional key that gives its specific runtime type (i.e. which type of Message it is). This makes it possible to read the object from JSON as class Message, since the adapter will now know which specific type of Message object to create.
Author:
Stephen G. Ware
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The name of the JSON key whose value will be the object's runtime type
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new abstract adapter for the given abstract type.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> com.google.gson.TypeAdapter<T>
    create(com.google.gson.Gson gson, com.google.gson.reflect.TypeToken<T> type)
     
    deserialize(com.google.gson.JsonElement element, Type type, com.google.gson.JsonDeserializationContext context)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TYPE_NAME

      public static final String TYPE_NAME
      The name of the JSON key whose value will be the object's runtime type
      See Also:
  • Constructor Details

    • AbstractAdapter

      public AbstractAdapter(Class<A> type)
      Constructs a new abstract adapter for the given abstract type.
      Parameters:
      type - an abstract type
  • Method Details

    • create

      public <T> com.google.gson.TypeAdapter<T> create(com.google.gson.Gson gson, com.google.gson.reflect.TypeToken<T> type)
      Specified by:
      create in interface com.google.gson.TypeAdapterFactory
    • deserialize

      public A deserialize(com.google.gson.JsonElement element, Type type, com.google.gson.JsonDeserializationContext context) throws com.google.gson.JsonParseException
      Specified by:
      deserialize in interface com.google.gson.JsonDeserializer<A>
      Throws:
      com.google.gson.JsonParseException