Class DiscriminatorBasedTypeAdapterFactory

  • All Implemented Interfaces:
    com.google.gson.TypeAdapterFactory

    public class DiscriminatorBasedTypeAdapterFactory
    extends java.lang.Object
    implements com.google.gson.TypeAdapterFactory
    This class is registered with Gson to perform deserialization of classes that have discriminator metadata defined in them. This would result from an OpenAPI schema that defines a "discriminator" object to aid in the proper type selection during deserialization. Here is an example of a class that contains the discriminator metdata: public class Vehicle extends GenericModel { private static String discriminatorPropertyName = "vehicle_type"; private static java.util.Map<String, Class<?>> discriminatorMapping; static { discriminatorMapping = new java.util.HashMap<>(); discriminatorMapping.put("Truck", Truck.class); discriminatorMapping.put("Car", Car.class); } } In this example, we'd expect the JSON object to contain the "vehicle_type" field. If the field's value is "Truck", then we should deserialize the object into an instance of the Truck class, and if the field's value is "Car", then we should deserialize the object into an instance of the Car class. This factory's 'create' method will examine the Class object passed in to determine if it contains the discriminator metadata. If it does, then we'll construct a new TypeAdapter for the specific class and return it to Gson where it will be cached and used for any subsequent instances of the same class. Otherwise, we'll return null to indicate that this factory shouldn't be used to handle the specified class.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  DiscriminatorBasedTypeAdapterFactory.Adapter<T>
      An adapter for serializing/deserializing instances of type T, where T represents a generated model that defines a discriminator to aid in deserialization.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> com.google.gson.TypeAdapter<T> create​(com.google.gson.Gson gson, com.google.gson.reflect.TypeToken<T> type)
      This method will return a TypeAdapter instance if the specified type is found to contain the expected discriminator metdata, or null otherwise.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DiscriminatorBasedTypeAdapterFactory

        public DiscriminatorBasedTypeAdapterFactory()
    • Method Detail

      • create

        public <T> com.google.gson.TypeAdapter<T> create​(com.google.gson.Gson gson,
                                                         com.google.gson.reflect.TypeToken<T> type)
        This method will return a TypeAdapter instance if the specified type is found to contain the expected discriminator metdata, or null otherwise. We use reflection to scrape the discriminator information (property name and mapping data) from the Class, then cache that information in a DiscriminatorMetadata instance which is then used to construct the TypeAdapter. The TypeAdapter will then cache the DiscriminatorMetadata instance and use it for subsequent deserialization operations. Note that the TypeAdapter instance we return is bound to the specific Class described by the 'type' parameter.
        Specified by:
        create in interface com.google.gson.TypeAdapterFactory
        Parameters:
        gson - the Gson instance
        type - a TypeToken instance that contains the Class to be examined
        Returns:
        a TypeAdapter instance for the specified class or null