Class GsonEncoder

java.lang.Object
org.springframework.core.codec.AbstractEncoder<Object>
org.springframework.http.codec.json.GsonEncoder
All Implemented Interfaces:
Encoder<Object>, HttpMessageEncoder<Object>

public class GsonEncoder extends AbstractEncoder<Object> implements HttpMessageEncoder<Object>
Encode from an Object stream to a byte stream of JSON objects using Google Gson.
Since:
7.0
Author:
Brian Clozel
  • Constructor Details

    • GsonEncoder

      public GsonEncoder()
      Construct a new encoder using a default Gson instance and the "application/json" and "application/*+json" MIME types. The "application/x-ndjson" is configured for streaming.
    • GsonEncoder

      public GsonEncoder(com.google.gson.Gson gson, MimeType... mimeTypes)
      Construct a new encoder using the given Gson instance and the provided MIME types. Use setStreamingMediaTypes(List) for configuring streaming media types.
      Parameters:
      gson - the gson instance to use
      mimeTypes - the mime types the decoder should support
  • Method Details

    • setStreamingMediaTypes

      public void setStreamingMediaTypes(List<MediaType> mediaTypes)
      Configure "streaming" media types for which flushing should be performed automatically vs at the end of the stream.
    • getStreamingMediaTypes

      public List<MediaType> getStreamingMediaTypes()
      Description copied from interface: HttpMessageEncoder
      Return "streaming" media types for which flushing should be performed automatically vs at the end of the input stream.
      Specified by:
      getStreamingMediaTypes in interface HttpMessageEncoder<Object>
    • canEncode

      public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType)
      Description copied from interface: Encoder
      Whether the encoder supports the given source element type and the MIME type for the output stream.
      Specified by:
      canEncode in interface Encoder<Object>
      Overrides:
      canEncode in class AbstractEncoder<Object>
      Parameters:
      elementType - the type of elements in the source stream
      mimeType - the MIME type for the output stream (can be null if not specified)
      Returns:
      true if supported, false otherwise
    • encode

      public reactor.core.publisher.Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String,Object> hints)
      Description copied from interface: Encoder
      Encode a stream of Objects of type T into a DataBuffer output stream.
      Specified by:
      encode in interface Encoder<Object>
      Parameters:
      inputStream - the input stream of Objects to encode. If the input should be encoded as a single value rather than as a stream of elements, an instance of Mono should be used.
      bufferFactory - for creating output stream DataBuffer's
      elementType - the expected type of elements in the input stream; this type must have been previously passed to the Encoder.canEncode(ResolvableType, MimeType) method and it must have returned true.
      mimeType - the MIME type for the output content (optional)
      hints - additional information about how to encode
      Returns:
      the output stream
    • encodeValue

      public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String,Object> hints)
      Description copied from interface: Encoder
      Encode an Object of type T to a data buffer. This is useful for scenarios, that distinct messages (or events) are encoded and handled individually, in fully aggregated form.

      By default this method raises UnsupportedOperationException and it is expected that some encoders cannot produce a single buffer or cannot do so synchronously (for example, encoding a Resource).

      Specified by:
      encodeValue in interface Encoder<Object>
      Parameters:
      value - the value to be encoded
      bufferFactory - for creating the output DataBuffer
      valueType - the type for the value being encoded
      mimeType - the MIME type for the output content (optional)
      hints - additional information about how to encode
      Returns:
      the encoded content
    • isStreamingMediaType

      protected boolean isStreamingMediaType(@Nullable MimeType mimeType)
      Return the separator to use for the given mime type.

      By default, this method returns new line "\n" if the given mime type is one of the configured streaming mime types.