Class GsonDecoder
java.lang.Object
org.springframework.core.codec.AbstractDecoder<Object>
org.springframework.core.codec.AbstractDataBufferDecoder<Object>
org.springframework.http.codec.json.GsonDecoder
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
-
Field Summary
Fields inherited from class AbstractDecoder
logger
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new decoder using a defaultGson
instance and the"application/json"
and"application/*+json"
MIME types.GsonDecoder
(com.google.gson.Gson gson, MimeType... mimeTypes) Construct a new decoder using the givenGson
instance and the provided MIME types. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canDecode
(ResolvableType elementType, @Nullable MimeType mimeType) Whether the decoder supports the given target element type and the MIME type of the source stream.reactor.core.publisher.Flux
<Object> decode
(Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Decode aDataBuffer
input stream into a Flux ofT
.decode
(DataBuffer buffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) Decode a data buffer to an Object of type T.Methods inherited from class AbstractDataBufferDecoder
decodeDataBuffer, decodeToMono, getMaxInMemorySize, setMaxInMemorySize
Methods inherited from class AbstractDecoder
getDecodableMimeTypes, getLogger, setLogger
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface Decoder
getDecodableMimeTypes
-
Constructor Details
-
GsonDecoder
public GsonDecoder()Construct a new decoder using a defaultGson
instance and the"application/json"
and"application/*+json"
MIME types. -
GsonDecoder
Construct a new decoder using the givenGson
instance and the provided MIME types.- Parameters:
gson
- the gson instance to usemimeTypes
- the mime types the decoder should support
-
-
Method Details
-
canDecode
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 interfaceDecoder<Object>
- Overrides:
canDecode
in classAbstractDecoder<Object>
- Parameters:
elementType
- the target element type for the output streammimeType
- the mime type associated with the stream to decode (can benull
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 aDataBuffer
input stream into a Flux ofT
.- Specified by:
decode
in interfaceDecoder<Object>
- Overrides:
decode
in classAbstractDataBufferDecoder<Object>
- Parameters:
inputStream
- theDataBuffer
input stream to decodeelementType
- the expected type of elements in the output stream; this type must have been previously passed to theDecoder.canDecode(ResolvableType, MimeType)
method and it must have returnedtrue
.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 DecodingExceptionDescription 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
- theDataBuffer
to decodetargetType
- the expected output typemimeType
- the MIME type associated with the datahints
- additional information about how to do decode- Returns:
- the decoded value, possibly
null
- Throws:
DecodingException
-