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 defaultGson
instance and the"application/json"
and"application/*+json"
MIME types.GsonEncoder
(com.google.gson.Gson gson, MimeType... mimeTypes) Construct a new encoder using the givenGson
instance and the provided MIME types. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canEncode
(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 typeT
into aDataBuffer
output 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 boolean
isStreamingMediaType
(@Nullable MimeType mimeType) Return the separator to use for the given mime type.void
setStreamingMediaTypes
(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, setLogger
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface Encoder
getEncodableMimeTypes, getEncodableMimeTypes
Methods inherited from interface HttpMessageEncoder
getEncodeHints
-
Constructor Details
-
GsonEncoder
public GsonEncoder()Construct a new encoder using a defaultGson
instance and the"application/json"
and"application/*+json"
MIME types. The"application/x-ndjson"
is configured for streaming. -
GsonEncoder
Construct a new encoder using the givenGson
instance 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:HttpMessageEncoder
Return "streaming" media types for which flushing should be performed automatically vs at the end of the input stream.- Specified by:
getStreamingMediaTypes
in interfaceHttpMessageEncoder<Object>
-
canEncode
Description copied from interface:Encoder
Whether 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:Encoder
Encode a stream of Objects of typeT
into aDataBuffer
output stream.- Specified by:
encode
in 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 ofMono
should 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: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 aResource
).- Specified by:
encodeValue
in interfaceEncoder<Object>
- Parameters:
value
- the value to be encodedbufferFactory
- for creating the outputDataBuffer
valueType
- 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
-