Class 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 Detail

      • PackageSpecificTypeAdapter

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

      • 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.
      • 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