Class GsonEncoder

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

public class GsonEncoder extends org.springframework.core.codec.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
  • Field Summary

    Fields inherited from class org.springframework.core.codec.AbstractEncoder

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a new encoder using a default Gson instance and the "application/json" and "application/*+json" MIME types.
    GsonEncoder(com.google.gson.Gson gson, org.springframework.util.MimeType... mimeTypes)
    Construct a new encoder using the given Gson instance and the provided MIME types.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canEncode(org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType)
     
    reactor.core.publisher.Flux<org.springframework.core.io.buffer.DataBuffer>
    encode(Publisher<?> inputStream, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String,Object> hints)
     
    org.springframework.core.io.buffer.DataBuffer
    encodeValue(Object value, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType valueType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String,Object> hints)
     
    Return "streaming" media types for which flushing should be performed automatically vs at the end of the input stream.
    protected boolean
    isStreamingMediaType(@Nullable org.springframework.util.MimeType mimeType)
    Return the separator to use for the given mime type.
    void
    Configure "streaming" media types for which flushing should be performed automatically vs at the end of the stream.

    Methods inherited from class org.springframework.core.codec.AbstractEncoder

    getEncodableMimeTypes, getLogger, setLogger

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.core.codec.Encoder

    getEncodableMimeTypes, getEncodableMimeTypes

    Methods inherited from interface HttpMessageEncoder

    getEncodeHints
  • 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, org.springframework.util.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(org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType)
      Specified by:
      canEncode in interface org.springframework.core.codec.Encoder<Object>
      Overrides:
      canEncode in class org.springframework.core.codec.AbstractEncoder<Object>
    • encode

      public reactor.core.publisher.Flux<org.springframework.core.io.buffer.DataBuffer> encode(Publisher<?> inputStream, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String,Object> hints)
      Specified by:
      encode in interface org.springframework.core.codec.Encoder<Object>
    • encodeValue

      public org.springframework.core.io.buffer.DataBuffer encodeValue(Object value, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType valueType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String,Object> hints)
      Specified by:
      encodeValue in interface org.springframework.core.codec.Encoder<Object>
    • isStreamingMediaType

      protected boolean isStreamingMediaType(@Nullable org.springframework.util.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.