JSON

Spring Boot 提供與三個 JSON 對應函式庫的整合

  • Gson

  • Jackson

  • JSON-B

Jackson 是首選和預設的函式庫。

Jackson

已提供 Jackson 的自動組態,且 Jackson 是 spring-boot-starter-json 的一部分。當 Jackson 在類別路徑上時,會自動組態 ObjectMapper bean。提供多個組態屬性,用於自訂 ObjectMapper 的組態

自訂序列化器與反序列化器

如果您使用 Jackson 來序列化和反序列化 JSON 資料,您可能想要編寫自己的 JsonSerializerJsonDeserializer 類別。自訂序列化器通常透過模組向 Jackson 註冊,但 Spring Boot 提供了替代的 @JsonComponent 注解,讓直接註冊 Spring Beans 更容易。

您可以直接在 JsonSerializerJsonDeserializerKeyDeserializer 實作上使用 @JsonComponent 注解。您也可以在包含序列化器/反序列化器作為內部類別的類別上使用它,如下列範例所示

  • Java

  • Kotlin

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import org.springframework.boot.jackson.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);
		}

	}

}
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.SerializerProvider
import org.springframework.boot.jackson.JsonComponent
import java.io.IOException

@JsonComponent
class MyJsonComponent {

	class Serializer : JsonSerializer<MyObject>() {
		@Throws(IOException::class)
		override fun serialize(value: MyObject, jgen: JsonGenerator, serializers: SerializerProvider) {
			jgen.writeStartObject()
			jgen.writeStringField("name", value.name)
			jgen.writeNumberField("age", value.age)
			jgen.writeEndObject()
		}
	}

	class Deserializer : JsonDeserializer<MyObject>() {
		@Throws(IOException::class, JsonProcessingException::class)
		override fun deserialize(jsonParser: JsonParser, ctxt: DeserializationContext): MyObject {
			val codec = jsonParser.codec
			val tree = codec.readTree<JsonNode>(jsonParser)
			val name = tree["name"].textValue()
			val age = tree["age"].intValue()
			return MyObject(name, age)
		}
	}

}

ApplicationContext 中的所有 @JsonComponent beans 都會自動向 Jackson 註冊。由於 @JsonComponent 使用 @Component 進行元注解,因此適用通常的組件掃描規則。

Spring Boot 也提供了 JsonObjectSerializerJsonObjectDeserializer 基礎類別,當序列化物件時,它們為標準 Jackson 版本提供了有用的替代方案。有關詳細資訊,請參閱 API 文件中的 JsonObjectSerializerJsonObjectDeserializer

上面的範例可以改寫為使用 JsonObjectSerializer/JsonObjectDeserializer,如下所示

  • Java

  • Kotlin

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;

import org.springframework.boot.jackson.JsonComponent;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.boot.jackson.JsonObjectSerializer;

@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);
		}

	}

}
/*
 * Copyright 2012-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.docs.features.json.jackson.customserializersanddeserializers.`object`

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.ObjectCodec
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.SerializerProvider
import org.springframework.boot.jackson.JsonComponent
import org.springframework.boot.jackson.JsonObjectDeserializer
import org.springframework.boot.jackson.JsonObjectSerializer
import java.io.IOException

@JsonComponent
class MyJsonComponent {

	class Serializer : JsonObjectSerializer<MyObject>() {
		@Throws(IOException::class)
		override fun serializeObject(value: MyObject, jgen: JsonGenerator, provider: SerializerProvider) {
			jgen.writeStringField("name", value.name)
			jgen.writeNumberField("age", value.age)
		}
	}

	class Deserializer : JsonObjectDeserializer<MyObject>() {
		@Throws(IOException::class)
		override fun deserializeObject(jsonParser: JsonParser, context: DeserializationContext,
				codec: ObjectCodec, tree: JsonNode): MyObject {
			val name = nullSafeValue(tree["name"], String::class.java)
			val age = nullSafeValue(tree["age"], Int::class.java)
			return MyObject(name, age)
		}
	}

}

Mixins

Jackson 支援 mixins,可用於將額外的注解混合到目標類別上已宣告的注解中。Spring Boot 的 Jackson 自動組態將掃描您應用程式的套件中帶有 @JsonMixin 注解的類別,並將它們註冊到自動組態的 ObjectMapper。註冊由 Spring Boot 的 JsonMixinModule 執行。

Gson

已提供 Gson 的自動組態。當 Gson 在類別路徑上時,會自動組態 Gson bean。提供多個 spring.gson.* 組態屬性,用於自訂組態。為了取得更多控制權,可以使用一個或多個 GsonBuilderCustomizer beans。

JSON-B

已提供 JSON-B 的自動組態。當 JSON-B API 和實作在類別路徑上時,將自動組態 Jsonb bean。首選的 JSON-B 實作是 Eclipse Yasson,並為其提供依賴管理。