Class TypeFieldTypeAdapter<T>

java.lang.Object
com.google.gson.TypeAdapter<T>
org.eclipse.mosaic.lib.gson.TypeFieldTypeAdapter<T>
Type Parameters:
T - the base type of which objects are created during deserialization
Direct Known Subclasses:
GeoAreaAdapterFactory.GeoAreaAdapter, PackageSpecificTypeAdapter

public abstract class TypeFieldTypeAdapter<T> extends com.google.gson.TypeAdapter<T>
A type adapter which creates an object of a specific type based on a hidden "type" field. An implementation of this abstract class must specify which type name translates to which target class.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    TypeFieldTypeAdapter(com.google.gson.TypeAdapterFactory parentFactory, com.google.gson.Gson gson)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    By default, a missing "type" field in the input JSON string leads to an error.
    protected abstract Class<?>
    Translates the name of the type to the class to deserialize.
    read(com.google.gson.stream.JsonReader in)
     
    protected abstract String
    toTypeName(Class<?> typeClass)
    Translates the class of the object to serialize to an unambiguous type name.
    void
    write(com.google.gson.stream.JsonWriter out, T value)
     

    Methods inherited from class com.google.gson.TypeAdapter

    fromJson, fromJson, fromJsonTree, nullSafe, toJson, toJson, toJsonTree

    Methods inherited from class java.lang.Object

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

    • TypeFieldTypeAdapter

      protected TypeFieldTypeAdapter(com.google.gson.TypeAdapterFactory parentFactory, com.google.gson.Gson gson)
  • Method Details

    • allowNullType

      protected void allowNullType()
      By default, a missing "type" field in the input JSON string leads to an error. By calling this method any proceeding deserialization processes allow this missing field by passing null to fromTypeName(java.lang.String) instead of throwing an exception.
    • fromTypeName

      protected abstract Class<?> fromTypeName(String type)
      Translates the name of the type to the class to deserialize.
      Parameters:
      type - the name of the type
      Returns:
      the class associated with the type
    • toTypeName

      protected abstract String toTypeName(Class<?> typeClass)
      Translates the class of the object to serialize to an unambiguous type name.
      Parameters:
      typeClass - the class to serialize
      Returns:
      the type name which is associated with the class to serialize
    • read

      public T read(com.google.gson.stream.JsonReader in)
      Specified by:
      read in class com.google.gson.TypeAdapter<T>
    • write

      public void write(com.google.gson.stream.JsonWriter out, T value) throws IOException
      Specified by:
      write in class com.google.gson.TypeAdapter<T>
      Throws:
      IOException