Class GsonDecoder

All Implemented Interfaces:
Decoder<Object>

public class GsonDecoder extends AbstractDataBufferDecoder<Object>
Decoder that reads a byte stream into JSON and converts it to Objects with Google Gson.

Flux<*> target types are not available because non-blocking parsing is not supported, so this decoder targets only Mono<*> types. Attempting to decode to a Flux<*> will result in a UnsupportedOperationException being thrown at runtime.

Since:
7.0
Author:
Brian Clozel
  • Constructor Details

    • GsonDecoder

      public GsonDecoder()
      Construct a new decoder using a default Gson instance and the "application/json" and "application/*+json" MIME types.
    • GsonDecoder

      public GsonDecoder(com.google.gson.Gson gson, MimeType... mimeTypes)
      Construct a new decoder using the given Gson instance and the provided MIME types.
      Parameters:
      gson - the gson instance to use
      mimeTypes - the mime types the decoder should support
  • Method Details

    • canDecode

      public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType)
      Description copied from interface: Decoder
      Whether the decoder supports the given target element type and the MIME type of the source stream.
      Specified by:
      canDecode in interface Decoder<Object>
      Overrides:
      canDecode in class AbstractDecoder<Object>
      Parameters:
      elementType - the target element type for the output stream
      mimeType - the mime type associated with the stream to decode (can be null if not specified)
      Returns:
      true if supported, false otherwise
    • decode

      public reactor.core.publisher.Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String,Object> hints)
      Description copied from interface: Decoder
      Decode a DataBuffer input stream into a Flux of T.
      Specified by:
      decode in interface Decoder<Object>
      Overrides:
      decode in class AbstractDataBufferDecoder<Object>
      Parameters:
      inputStream - the DataBuffer input stream to decode
      elementType - the expected type of elements in the output stream; this type must have been previously passed to the Decoder.canDecode(ResolvableType, MimeType) method and it must have returned true.
      mimeType - the MIME type associated with the input stream (optional)
      hints - additional information about how to do decode
      Returns:
      the output stream with decoded elements
    • decode

      public @Nullable Object decode(DataBuffer buffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String,Object> hints) throws DecodingException
      Description copied from interface: Decoder
      Decode a data buffer to an Object of type T. This is useful for scenarios, that distinct messages (or events) are decoded and handled individually, in fully aggregated form.
      Parameters:
      buffer - the DataBuffer to decode
      targetType - the expected output type
      mimeType - the MIME type associated with the data
      hints - additional information about how to do decode
      Returns:
      the decoded value, possibly null
      Throws:
      DecodingException