> For the complete documentation index, see [llms.txt](https://doc.shiker.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.shiker.tech/spring-boot-can-kao-wen-dang/7.-he-xin-te-xing/7.7.-json.md).

# 7.7. JSON

Spring Boot 提供与三个 JSON 映射库的集成：

* gson
* Jackson
* JSON-B

Jackson 是首选的默认库。

#### 7.7.1. Jackson

提供了 Jackson 的自动配置，并且 Jackson 是`spring-boot-starter-json`. 当 Jackson 在类路径上时，`ObjectMapper`会自动配置一个 bean。提供了几个配置属性用于[自定义`ObjectMapper`](https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.spring-mvc.customize-jackson-objectmapper).

**6.1.1. 自定义序列化器和反序列化器**

如果您使用 Jackson 来序列化和反序列化 JSON 数据，您可能需要编写自己的`JsonSerializer`类和`JsonDeserializer`类。自定义序列化程序通常[通过模块向 Jackson 注册](https://github.com/FasterXML/jackson-docs/wiki/JacksonHowToCustomSerializers)，但 Spring Boot 提供了一个`@JsonComponent`注解，可以更轻松地直接注册 Spring Bean。

您可以直接在`JsonSerializer` `JsonDeserializer` 或`KeyDeserializer`实现上使用`@JsonComponent`注释。您还可以在包含序列化器/反序列化器作为内部类的类上使用它，如以下示例所示：

```
@JsonComponent
public class MyJsonComponent {
​
    public static class Serializer extends JsonSerializer<MyObject> {
​
        @Override
        public void serialize(MyObject value, JsonGenerator jgen, SerializerProvider serializers) throws IOException {
            jgen.writeStartObject();
            jgen.writeStringField("name", value.getName());
            jgen.writeNumberField("age", value.getAge());
            jgen.writeEndObject();
        }
​
    }
​
    public static class Deserializer extends JsonDeserializer<MyObject> {
​
        @Override
        public MyObject deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
            ObjectCodec codec = jsonParser.getCodec();
            JsonNode tree = codec.readTree(jsonParser);
            String name = tree.get("name").textValue();
            int age = tree.get("age").intValue();
            return new MyObject(name, age);
        }
​
    }
​
}
```

`ApplicationContext`中的所有`@JsonComponent`bean 都会自动向 Jackson 注册。因为`@JsonComponent`是用元注解的`@Component`，所以通常的组件扫描规则适用。

Spring Boot 还提供了[`JsonObjectSerializer`](https://github.com/spring-projects/spring-boot/tree/v2.7.3/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jackson/JsonObjectSerializer.java)和[`JsonObjectDeserializer`](https://github.com/spring-projects/spring-boot/tree/v2.7.3/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jackson/JsonObjectDeserializer.java)基类，它们在序列化对象时提供了标准 Jackson 版本的有用替代方案。有关详细信息，请参阅[`JsonObjectSerializer`](https://docs.spring.io/spring-boot/docs/2.7.3/api/org/springframework/boot/jackson/JsonObjectSerializer.html)和[`JsonObjectDeserializer`](https://docs.spring.io/spring-boot/docs/2.7.3/api/org/springframework/boot/jackson/JsonObjectDeserializer.html)的Javadoc。

上面的例子可以重写为使用`JsonObjectSerializer`/`JsonObjectDeserializer`如下：

```
@JsonComponent
public class MyJsonComponent {
​
    public static class Serializer extends JsonObjectSerializer<MyObject> {
​
        @Override
        protected void serializeObject(MyObject value, JsonGenerator jgen, SerializerProvider provider)
                throws IOException {
            jgen.writeStringField("name", value.getName());
            jgen.writeNumberField("age", value.getAge());
        }
​
    }
​
    public static class Deserializer extends JsonObjectDeserializer<MyObject> {
​
        @Override
        protected MyObject deserializeObject(JsonParser jsonParser, DeserializationContext context, ObjectCodec codec,
                JsonNode tree) throws IOException {
            String name = nullSafeValue(tree.get("name"), String.class);
            int age = nullSafeValue(tree.get("age"), Integer.class);
            return new MyObject(name, age);
        }
​
    }
​
}
```

7.7.**1.2. 混合**

Jackson 支持 mixins，可用于将其他注解混合到已在目标类上声明的注释中。Spring Boot 的 Jackson 自动配置将扫描您应用程序的包以查找带有`@JsonMixin`注解的类，并将它们注册到自动配置的`ObjectMapper`中。通过 Spring Boot 的`JsonMixinModule`注册.

#### 7.7.2. gson

提供了 Gson 的自动配置。当 Gson 在类路径上时，会自动配置一个`Gson` bean。提供了几个`spring.gson.*`配置属性用于自定义配置。要进行更多控制，可以使用一个或多个`GsonBuilderCustomizer` bean。

#### 7.7.3. JSON-B

提供了 JSON-B 的自动配置。当 JSON-B API 和实现在类路径上时，将自动配置一个`Jsonb` bean。首选的 JSON-B 实现是为其提供依赖管理的 Apache Johnzon。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-boot-can-kao-wen-dang/7.-he-xin-te-xing/7.7.-json.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
