1. 概述
在本快速教程中,我們將探討使用 Jackson 對 Java 映射的序列化和反序列化。
我們將演示如何將 Map<String, String>, Map<Object, String> 和 Map<Object, Object> 轉換為 JSON 格式的 Strings。
2. Maven 配置
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
我們可以從這裏獲取 Jackson 的最新版本:這裏。
3. Serialization
Serialization converts a Java object into a stream of bytes, which can be persisted or shared as needed. Java Maps are collections that map a key Object to a value Object, and are often the least intuitive objects to serialize.
3.1. Map<String, String> Serialization
For a simple case, let’s create a Map<String, String> and serialize it to JSON:
Map<String, String> map = new HashMap<>();
map.put("key", "value");
ObjectMapper mapper = new ObjectMapper();
String jsonResult = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);
ObjectMapper is Jackson’s serialization mapper. It allows us to serialize our map, and write it out as a pretty-printed JSON String using the toString() method in String:
{
"key" : "value"
}
3.2. Map<Object, String> Serialization
With a few extra steps, we can also serialize a map containing a custom Java class. Let’s create a MyPair class to represent a pair of related String objects.
Note: the getters/setters should be public, and we annotate toString() with @JsonValue to ensure Jackson uses this custom toString() when serializing:
public class MyPair {
private String first;
private String second;
@Override
@JsonValue
public String toString() {
return first + " and " + second;
}
// standard getter, setters, equals, hashCode, constructors
}
Then we’ll tell Jackson how to serialize MyPair by extending Jackson’s JsonSerializer:
public class MyPairSerializer extends JsonSerializer<MyPair> {
private ObjectMapper mapper = new ObjectMapper();
@Override
public void serialize(MyPair value,
JsonGenerator gen,
SerializerProvider serializers)
throws IOException, JsonProcessingException {
StringWriter writer = new StringWriter();
mapper.writeValue(writer, value);
gen.writeFieldName(writer.toString());
}
}
JsonSerializer, as the name suggests, serializes MyPair to JSON using MyPair‘s toString() method. Furthermore, Jackson provides many Serializer classes to fit our serialization requirements.
Next we apply MyPairSerializer to our Map<MyPair, String> with the @JsonSerialize annotation. Note that we’ve only told Jackson how to serialize MyPair because it already knows how to serialize String:
@JsonSerialize(keyUsing = MyPairSerializer.class)
Map<MyPair, String> map;
Then let’s test our map serialization:
map = new HashMap<>();
MyPair key = new MyPair("Abbott", "Costello");
map.put(key, "Comedy");
String jsonResult = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);
The serialized JSON output is:
{
"Abbott and Costello" : "Comedy"
}
3.3. Map<Object, Object> Serialization
The most complex case is serializing a Map<Object, Object>, but most of the work is already done. Let’s use Jackson’s MapSerializer for our map, and MyPairSerializer, from the previous section, for the map’s key and value types:
@JsonSerialize(keyUsing = MapSerializer.class)
Map<MyPair, MyPair> map;
@JsonSerialize(keyUsing = MyPairSerializer.class)
MyPair mapKey;
@JsonSerialize(keyUsing = MyPairSerializer.class)
MyPair mapValue;
Then let’s test out serializing our Map<MyPair, MyPair>:
mapKey = new MyPair("Abbott", "Costello");
mapValue = new MyPair("Comedy", "1940s");
map.put(mapKey, mapValue);
String jsonResult = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);
As expected, the @JsonKey annotation is used for this serialization. Further, we’ve used LinkedHashMap to fix the order of keys during its access and serialization.
Finally, let’s also see the results of serializing the selectionByFruit Map that contains instances of the Fruit class as a key:
@Test
public void givenMapWithObjectKeys_WhenSerialize_ThenUseJsonKeyForSerialization()
throws JsonProcessingException {
// Given
Map<Fruit, String> selectionByFruit = new LinkedHashMap<>();
selectionByFruit.put(FRUIT1, "Hagrid");
selectionByFruit.put(FRUIT2, "Hercules");
// When
String serializedValue = OBJECT_MAPPER.writeValueAsString(selectionByFruit);
// Then
Assertions.assertEquals("{\"Mango\":\"Hagrid\",\"Grapes\":\"Hercules\"}", serializedValue);
}
3.4. @JsonKey Annotation
While creating a Map, an object could be a key or a value. Further, we might need different serialization strategies when an object appears as a key in a Map vs. when it appears as a value. So, let’s learn how we can use the @JsonKey annotation to do so.
Let’s start by defining the Fruit class with two members, namely variety and name:
public class Fruit {
public String variety;
@JsonKey
public String name;
public Fruit(String variety, String name) {
this.variety = variety;
this.name = name;
}
@JsonValue
public String getFullName() {
return this.variety + " " + this.name;
}
}
We must note that we want to use its name for serialization whenever a Fruit object appears as a key in a Map vs. when it appears as a value.
Now, let’s initialize two instances of the Fruit class and a single instance of the ObjectMapper class for serialization purposes:
private static final Fruit FRUIT1 = new Fruit("Alphonso", "Mango");
private static final Fruit FRUIT2 = new Fruit("Black", "Grapes");
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
Next, we should remember that when serializing a standalone instance, the @JsonValue property is used for serialization. Let’s verify this for the two objects:
@Test
public void givenObject_WhenSerialize_ThenUseJsonValueForSerialization()
throws JsonProcessingException {
String serializedValueForFruit1 = OBJECT_MAPPER.writeValueAsString(FRUIT1);
Assertions.assertEquals("\"Alphonso Mango\"", serializedValueForFruit1);
String serializedValueForFruit2 = OBJECT_MAPPER.writeValueAsString(FRUIT2);
Assertions.assertEquals("\"Black Grapes\"", serializedValueForFruit2);
}
Further on, let’s serialize the selectionByFruit Map that contains instances of the Fruit class as a key:
@Test
public void givenMapWithObjectKeys_WhenSerialize_ThenUseJsonKeyForSerialization()
throws JsonProcessingException {
// Given
Map<Fruit, String> selectionByFruit = new LinkedHashMap<>();
selectionByFruit.put(FRUIT1, "Hagrid");
selectionByFruit.put(FRUIT2, "Hercules");
// When
String serializedValue = OBJECT_MAPPER.writeValueAsString(selectionByFruit);
// Then
Assertions.assertEquals("{\"Mango\":\"Hagrid\",\"Grapes\":\"Hercules\"}", serializedValue);
}
As expected, the @JsonKey annotation is used for this serialization. Further, we’ve used LinkedHashMap to fix the order of keys during its access and serialization.
Finally, let’s also see the results of serializing the selectionByFruit Map that contains instances of the Fruit class as a key:
@Test
public void givenMapWithObjectKeys_WhenSerialize_ThenUseJsonKeyForSerialization()
throws JsonProcessingException {
// Given
Map<Fruit, String> selectionByFruit = new LinkedHashMap<>();
selectionByFruit.put(FRUIT1, "Hagrid");
selectionByFruit.put(FRUIT2, "Hercules");
// When
String serializedValue = OBJECT_MAPPER.writeValueAsString(selectionByFruit);
// Then
Assertions.assertEquals("{\"Mango\":\"Hagrid\",\"Grapes\":\"Hercules\"}", serializedValue);
}
4. Deserialization
Deserialization converts a stream of bytes into a Java object that we can use in code. In this section, we’ll deserialize JSON input into
4.1. Map<String, String> Deserialization
For a simple case, let’s take a JSON-formatted input string and convert it to a
String jsonInput = "{\"key\": \"value\"}";
TypeReference<HashMap<String, String>> typeRef
= new TypeReference<HashMap<String, String>>() {};
Map<String, String> map = mapper.readValue(jsonInput, typeRef);
We use Jackson’s
{key=value}
4.2. Map<Object, String> Deserialization
Now let’s change our input JSON and the
String jsonInput = "{\"Abbott and Costello\" : \"Comedy\"}";
TypeReference<HashMap<MyPair, String>> typeRef
= new TypeReference<HashMap<MyPair, String>>() {};
Map<MyPair,String> map = mapper.readValue(jsonInput, typeRef);
We need to create a constructor for
public MyPair(String both) {
String[] pairs = both.split("and");
this.first = pairs[0].trim();
this.second = pairs[1].trim();
}
The
{Abbott and Costello=Comedy}
4.3. Map<Object,Object> Deserialization
Finally, let’s change our input JSON and the
String jsonInput = "{\"Abbott and Costello\" : \"Comedy and 1940s\"}";
TypeReference<HashMap<MyPair, MyPair>> typeRef
= new TypeReference<HashMap<MyPair, MyPair>>() {};
Map<MyPair,MyPair> map = mapper.readValue(jsonInput, typeRef);
The
{Abbott and Costello=Comedy and 1940s}
5. 結論
在本文中,我們學習瞭如何將 Java 中的 Map 對象序列化和反序列化為 JSON 格式的字符串。