Class DefaultObjectTypeReferenceHandler

    • Constructor Detail

      • DefaultObjectTypeReferenceHandler

        public DefaultObjectTypeReferenceHandler​(SerializationContext serializationContext)
        Creates a new instance
        Parameters:
        serializationContext - a SerializationContext to keep track of the references
    • Method Detail

      • shouldWriteReference

        public boolean shouldWriteReference​(ObjectType type)
        Given an ObjectType retrieves if should be written as a reference or not.

        Default implementation returns `true` to maintain backwards compatibility.

        Specified by:
        shouldWriteReference in interface ObjectTypeReferenceHandler
        Parameters:
        type - the type to write
        Returns:
        whether the type should be written or not.
      • writeReference

        public Optional<String> writeReference​(ObjectType type,
                                               com.google.gson.stream.JsonWriter writer)
        Uses the writer to write a reference to the given type.

        This method returns the written reference, so that it can be used as an id to deserialize. However, some implementations might be unable to handle some specific types, which is why the reference might be Optional.empty().

        In such case, it's the invoker responsibility to either attempt using a different implementation of this contract or to just treat the type explicitly instead of through a reference.

        Specified by:
        writeReference in interface ObjectTypeReferenceHandler
        Parameters:
        type - the type to write
        writer - a JsonWriter
        Returns:
        the reference that was written or Optional.empty()