Add Gson codecs for WebFlux

This commit adds new `GsonEncoder` and `GsonDecoder` for serializing and
deserializing JSON in a reactive fashion.

Because `Gson` itslef does not support decoding JSON in a non-blocking
way, the `GsonDecoder` does not support decoding to `Flux<*>` types.

Closes gh-27131
This commit is contained in:
Brian Clozel
2025-09-23 17:09:41 +02:00
parent d84bf18408
commit 20aac6dd8d
8 changed files with 574 additions and 0 deletions
@@ -545,6 +545,20 @@ The `ProtobufJsonDecoder` and `ProtobufJsonEncoder` variants support reading and
They require the "com.google.protobuf:protobuf-java-util" dependency. Note, the JSON variants do not support reading stream of messages,
see the {spring-framework-api}/http/codec/protobuf/ProtobufJsonDecoder.html[javadoc of `ProtobufJsonDecoder`] for more details.
[[webflux-codecs-gson]]
=== Google Gson
Applications can use the `GsonEncoder` and `GsonDecoder` to serialize and deserialize JSON documents thanks to the https://google.github.io/gson/[Google Gson] library .
This codec supports both JSON media types and the NDJSON format for streaming.
[NOTE]
====
`Gson` does not support non-blocking parsing, so the `GsonDecoder` does not support deserializing
to `Flux<*>` types. For example, if this decoder is used for deserializing a JSON stream or even a list of elements
as a `Flux<*>`, an `UnsupportedOperationException` will be thrown at runtime.
Applications should instead focus on deserializing bounded collections and use `Mono<List<*>>` as target types.
====
[[webflux-codecs-limits]]
=== Limits