Class DiscriminatorBasedTypeAdapterFactory.Adapter<T>

  • Enclosing class:
    DiscriminatorBasedTypeAdapterFactory

    public static class DiscriminatorBasedTypeAdapterFactory.Adapter<T>
    extends com.google.gson.TypeAdapter<T>
    An adapter for serializing/deserializing instances of type T, where T represents a generated model that defines a discriminator to aid in deserialization. This TypeAdapter will use the cached DiscriminatorMetadata to select the proper Class for the deserialization target.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T read​(com.google.gson.stream.JsonReader in)
      This method is responsible for deserializing a JSON object into an instance of the class identified by the discriminator mapping information.
      void write​(com.google.gson.stream.JsonWriter out, T value)
      We wouldn't normally expect Gson to call our write() method because we should never actually have instances of a "oneOf" base class (they are implemented as abstract base classes).
      • Methods inherited from class com.google.gson.TypeAdapter

        fromJson, fromJson, fromJsonTree, nullSafe, toJson, toJson, toJsonTree
      • Methods inherited from class java.lang.Object

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

      • write

        public void write​(com.google.gson.stream.JsonWriter out,
                          T value)
                   throws java.io.IOException
        We wouldn't normally expect Gson to call our write() method because we should never actually have instances of a "oneOf" base class (they are implemented as abstract base classes). However, in some serialization scenarios, Gson will use the DECLARED type (rather than an object's actual RUNTIME type) when trying to find a suitable TypeAdapter. One scenario where this occurs is where a class contains a field of type List of X, where X is a oneOf base class with a discriminator. So in those cases where we're asked to serialize something, we'll just delegate to a TypeAdapter that's associated with the object's specific runtime type (i.e. the subclass).
        Specified by:
        write in class com.google.gson.TypeAdapter<T>
        Throws:
        java.io.IOException
      • read

        public T read​(com.google.gson.stream.JsonReader in)
               throws java.io.IOException
        This method is responsible for deserializing a JSON object into an instance of the class identified by the discriminator mapping information. We'll perform the following steps: 1) Do a parse of the JSON object to obtain the JsonElement parse tree. This step produces an intermediate form of the deserialized JSON object and consumes the entire object within the JsonReader. 2) Retrieve the discriminator value from the JsonElement parse tree. 3) Select the proper deserialization target class by looking up the discriminator value in the cached mapping info. 4) Deserialize the JsonElement parse tree into an instance of the selected deserialization target class.
        Specified by:
        read in class com.google.gson.TypeAdapter<T>
        Throws:
        java.io.IOException