Class GsonEncoder
java.lang.Object
org.springframework.core.codec.AbstractEncoder<Object>
org.springframework.http.codec.json.GsonEncoder
- All Implemented Interfaces:
Encoder<Object>, 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 AbstractEncoder
logger -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new encoder using a defaultGsoninstance and the"application/json"and"application/*+json"MIME types.GsonEncoder(com.google.gson.Gson gson, MimeType... mimeTypes) Construct a new encoder using the givenGsoninstance and the provided MIME types. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanEncode(ResolvableType elementType, @Nullable MimeType mimeType) Whether the encoder supports the given source element type and the MIME type for the output stream.reactor.core.publisher.Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Encode a stream of Objects of typeTinto aDataBufferoutput stream.encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Encode an Object of type T to a data buffer.Return "streaming" media types for which flushing should be performed automatically vs at the end of the input stream.protected booleanisStreamingMediaType(@Nullable MimeType mimeType) Return the separator to use for the given mime type.voidsetStreamingMediaTypes(List<MediaType> mediaTypes) Configure "streaming" media types for which flushing should be performed automatically vs at the end of the stream.Methods inherited from class AbstractEncoder
getEncodableMimeTypes, getLogger, setLoggerMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Encoder
getEncodableMimeTypes, getEncodableMimeTypesMethods inherited from interface HttpMessageEncoder
getEncodeHints
-
Constructor Details
-
GsonEncoder
public GsonEncoder()Construct a new encoder using a defaultGsoninstance and the"application/json"and"application/*+json"MIME types. The"application/x-ndjson"is configured for streaming. -
GsonEncoder
Construct a new encoder using the givenGsoninstance and the provided MIME types. UsesetStreamingMediaTypes(List)for configuring streaming media types.- Parameters:
gson- the gson instance to usemimeTypes- the mime types the decoder should support
-
-
Method Details
-
setStreamingMediaTypes
-
getStreamingMediaTypes
Description copied from interface:HttpMessageEncoderReturn "streaming" media types for which flushing should be performed automatically vs at the end of the input stream.- Specified by:
getStreamingMediaTypesin interfaceHttpMessageEncoder<Object>
-
canEncode
Description copied from interface:EncoderWhether the encoder supports the given source element type and the MIME type for the output stream. -
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:EncoderEncode a stream of Objects of typeTinto aDataBufferoutput stream.- Specified by:
encodein interfaceEncoder<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 ofMonoshould be used.bufferFactory- for creating output streamDataBuffer'selementType- the expected type of elements in the input stream; this type must have been previously passed to theEncoder.canEncode(ResolvableType, MimeType)method and it must have returnedtrue.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:EncoderEncode 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
UnsupportedOperationExceptionand it is expected that some encoders cannot produce a single buffer or cannot do so synchronously (for example, encoding aResource).- Specified by:
encodeValuein interfaceEncoder<Object>- Parameters:
value- the value to be encodedbufferFactory- for creating the outputDataBuffervalueType- the type for the value being encodedmimeType- the MIME type for the output content (optional)hints- additional information about how to encode- Returns:
- the encoded content
-
isStreamingMediaType
-