[[features.json]]
= JSON

Spring Boot provides integration with the following JSON mapping libraries:

- Jackson 3
- Jackson 2
- Gson
- JSON-B
- Kotlin Serialization

Jackson 3 is the preferred and default library.

Support for Jackson 2 is deprecated and will be removed in a future Spring Boot 4.x release.
It is provided purely to ease the migration from Jackson 2 to Jackson 3 and should not be relied up in the longer term.



[[features.json.jackson]]
== Jackson 3

Auto-configuration for Jackson 3 is provided and Jackson is part of `spring-boot-starter-json`.
When Jackson is on the classpath a javadoc:tools.jackson.databind.json.JsonMapper[] bean is automatically configured.
Several configuration properties are provided for xref:how-to:spring-mvc.adoc#howto.spring-mvc.customize-jackson-jsonmapper[customizing the configuration of the javadoc:tools.jackson.databind.json.JsonMapper[]].



[[features.json.jackson.custom-serializers-and-deserializers]]
=== Custom Serializers and Deserializers

If you use Jackson to serialize and deserialize JSON data, you might want to write your own javadoc:tools.jackson.databind.ValueSerializer[] and javadoc:tools.jackson.databind.ValueDeserializer[] classes.
Custom serializers are usually https://github.com/FasterXML/jackson-docs/wiki/JacksonHowToCustomSerializers[registered with Jackson through a module], but Spring Boot provides an alternative javadoc:org.springframework.boot.jackson.JacksonComponent[format=annotation] annotation that makes it easier to directly register Spring Beans.

You can use the javadoc:org.springframework.boot.jackson.JacksonComponent[format=annotation] annotation directly on javadoc:tools.jackson.databind.ValueSerializer[], javadoc:tools.jackson.databind.ValueDeserializer[] or javadoc:tools.jackson.databind.KeyDeserializer[] implementations.
You can also use it on classes that contain serializers/deserializers as inner classes, as shown in the following example:

include-code::MyJacksonComponent[]

All javadoc:org.springframework.boot.jackson.JacksonComponent[format=annotation] beans in the javadoc:org.springframework.context.ApplicationContext[] are automatically registered with Jackson.
Because javadoc:org.springframework.boot.jackson.JacksonComponent[format=annotation] is meta-annotated with javadoc:org.springframework.stereotype.Component[format=annotation], the usual component-scanning rules apply.

Spring Boot also provides javadoc:org.springframework.boot.jackson.ObjectValueSerializer[] and javadoc:org.springframework.boot.jackson.ObjectValueDeserializer[] base classes that provide useful alternatives to the standard Jackson versions when serializing objects.
See javadoc:org.springframework.boot.jackson.ObjectValueSerializer[] and javadoc:org.springframework.boot.jackson.ObjectValueDeserializer[] in the API documentation for details.

The example above can be rewritten to use javadoc:org.springframework.boot.jackson.ObjectValueSerializer[] and javadoc:org.springframework.boot.jackson.ObjectValueDeserializer[] as follows:

include-code::object/MyJacksonComponent[]



[[features.json.jackson.mixins]]
=== Mixins

Jackson has support for mixins that can be used to mix additional annotations into those already declared on a target class.
Spring Boot's Jackson auto-configuration will scan your application's packages for classes annotated with javadoc:org.springframework.boot.jackson.JacksonMixin[format=annotation] and register them with the auto-configured javadoc:tools.jackson.databind.json.JsonMapper[].
The registration is performed by Spring Boot's javadoc:org.springframework.boot.jackson.JacksonMixinModule[].



[[features.json.jackson2]]
== Jackson 2

Deprecated auto-configuration for Jackson 2 is provided by the `spring-boot-jackson2` module.
When this module is on the classpath a javadoc:com.fasterxml.jackson.databind.ObjectMapper[] bean is automatically configured.
Several `+spring.jackson2.*+` configuration properties are provided for customizing the configuration.
To take more control, define one or more javadoc:org.springframework.boot.jackson2.autoconfigure.Jackson2ObjectMapperBuilderCustomizer[] beans.

When both Jackson 3 and Jackson 2 are present, various configuration properties can be used to indicate that Jackson 2 is preferred:

- configprop:spring.graphql.rsocket.preferred-json-mapper[]
- configprop:spring.http.codecs.preferred-json-mapper[] (used by Spring WebFlux and reactive HTTP clients)
- configprop:spring.http.converters.preferred-json-mapper[] (used by Spring MVC and imperative HTTP clients)
- configprop:spring.rsocket.preferred-mapper[]
- configprop:spring.websocket.messaging.preferred-json-mapper[]

In each case, set the relevant property to `jackson2` to indicate that Jackson 2 is preferred.



[[features.json.gson]]
== Gson

Auto-configuration for Gson is provided.
When Gson is on the classpath a javadoc:com.google.gson.Gson[] bean is automatically configured.
Several `+spring.gson.*+` configuration properties are provided for customizing the configuration.
To take more control, one or more javadoc:org.springframework.boot.gson.autoconfigure.GsonBuilderCustomizer[] beans can be used.



[[features.json.json-b]]
== JSON-B

Auto-configuration for JSON-B is provided.
When the JSON-B API and an implementation are on the classpath a javadoc:jakarta.json.bind.Jsonb[] bean will be automatically configured.
The preferred JSON-B implementation is Eclipse Yasson for which dependency management is provided.



[[features.json.kotlin-serialization]]
== Kotlin Serialization

Auto-configuration for Kotlin Serialization is provided.
When `kotlinx-serialization-json` is on the classpath a https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json/[Json] bean is automatically configured.
Several `+spring.kotlinx.serialization.json.*+` configuration properties are provided for customizing the configuration.
