Class PackageSpecificTypeAdapter<T>

java.lang.Object
com.google.gson.TypeAdapter<T>
org.eclipse.mosaic.lib.gson.TypeFieldTypeAdapter<T>
org.eclipse.mosaic.lib.util.gson.PackageSpecificTypeAdapter<T>

public final class PackageSpecificTypeAdapter<T> extends TypeFieldTypeAdapter<T>
This TypeAdapterFactory allows to create an object from JSON definition based on a "type" attribute. According to the value of the value in the "type" attribute, a class is loaded and an object of this class is instantiated. Since this explicitly works with simple class names only, a search space of possible packages need to be defined, which are used to resolve the actual class.
  • Constructor Details

    • PackageSpecificTypeAdapter

      public PackageSpecificTypeAdapter(com.google.gson.TypeAdapterFactory parentFactory, com.google.gson.Gson gson)
  • Method Details

    • searchInPackageOfClass

      public PackageSpecificTypeAdapter<T> searchInPackageOfClass(Class<?> clazz)
      Adds the package of the given class to the search space. All classes within this package are candidates to be loaded by a given type name.
    • searchInPackage

      public PackageSpecificTypeAdapter<T> searchInPackage(Package searchPackage)
      Adds the given package to the search space. All classes within this package are candidates to be loaded by a given type name.
    • searchInPackage

      public PackageSpecificTypeAdapter<T> searchInPackage(String packageName)
      Adds the given fully qualified package name to the search space. All classes within the given package are candidates to be loaded by a given type name.
    • withClassLoader

      public PackageSpecificTypeAdapter<T> withClassLoader(ClassLoader classLoader)
      Defines a specific class loader from which the searched classes are loaded.
    • fromTypeName

      protected Class<?> fromTypeName(String type)
      Description copied from class: TypeFieldTypeAdapter
      Translates the name of the type to the class to deserialize.
      Specified by:
      fromTypeName in class TypeFieldTypeAdapter<T>
      Parameters:
      type - the name of the type
      Returns:
      the class associated with the type
    • toTypeName

      protected String toTypeName(Class<?> typeClass)
      Description copied from class: TypeFieldTypeAdapter
      Translates the class of the object to serialize to an unambiguous type name.
      Specified by:
      toTypeName in class TypeFieldTypeAdapter<T>
      Parameters:
      typeClass - the class to serialize
      Returns:
      the type name which is associated with the class to serialize