1. Overview
1.概述
This quick tutorial will show how to serialize a Java entity with Jackson 2 using a Custom Serializer.
这个快速教程将展示如何使用自定义序列化器用Jackson 2序列化一个Java实体。
If you want to dig deeper and learn other cool things you can do with the Jackson 2 – head on over to the main Jackson tutorial.
如果你想深入了解并学习你可以用杰克逊2做的其他很酷的事情–请到杰克逊主教程。
2. Standard Serialization of an Object Graph
2.对象图的标准序列化
Let’s define 2 simple entities and see how Jackson serializes these without any custom logic:
让我们定义2个简单的实体,看看Jackson是如何在没有任何自定义逻辑的情况下序列化这些实体的。
public class User {
public int id;
public String name;
}
public class Item {
public int id;
public String itemName;
public User owner;
}
Now, let’s serialize an Item entity with a User entity:
现在,让我们将一个Item实体与一个User实体序列化。
Item myItem = new Item(1, "theItem", new User(2, "theUser"));
String serialized = new ObjectMapper().writeValueAsString(myItem);
This will result in a full JSON representation for both entities:
这将导致两个实体的完整JSON表示。
{
"id": 1,
"itemName": "theItem",
"owner": {
"id": 2,
"name": "theUser"
}
}
3. Custom Serializer on the ObjectMapper
3.在ObjectMapper上的自定义串行器
Now, let’s simplify the JSON output above by only serializing the id of the User, not the entire User object; we’d like to get the following, simpler JSON:
现在,让我们简化上面的JSON输出,只对User的id进行序列化,而不是整个User对象;我们想得到下面这个更简单的JSON。
{
"id": 25,
"itemName": "FEDUfRgS",
"owner": 15
}
Simply put, we’ll have to define a custom Serializer for Item objects:
简单地说,我们必须为Item对象定义一个自定义Serializer。
public class ItemSerializer extends StdSerializer<Item> {
public ItemSerializer() {
this(null);
}
public ItemSerializer(Class<Item> t) {
super(t);
}
@Override
public void serialize(
Item value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("id", value.id);
jgen.writeStringField("itemName", value.itemName);
jgen.writeNumberField("owner", value.owner.id);
jgen.writeEndObject();
}
}
Now, we need to register this custom serializer with the ObjectMapper for the Item class, and perform the serialization:
现在,我们需要为Item类的ObjectMapper注册这个自定义序列化器,并执行序列化。
Item myItem = new Item(1, "theItem", new User(2, "theUser"));
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Item.class, new ItemSerializer());
mapper.registerModule(module);
String serialized = mapper.writeValueAsString(myItem);
That’s it – we now have a simpler, custom JSON serialization of the Item->User entities.
就是这样–我们现在有了一个更简单的、自定义的JSON序列化的Item->User实体。
4. Custom Serializer on the Class
4.类上的自定义串行器
We can also register the serializer directly on the class, instead of on the ObjectMapper:
我们还可以直接在类上注册序列化器,而不是在ObjectMapper上注册。
@JsonSerialize(using = ItemSerializer.class)
public class Item {
...
}
Now, when performing standard serialization:
现在,在执行标准序列化时。
Item myItem = new Item(1, "theItem", new User(2, "theUser"));
String serialized = new ObjectMapper().writeValueAsString(myItem);
We will get the custom JSON output, created by the serializer, specified via @JsonSerialize:
我们将得到自定义的JSON输出,由序列化器创建,通过@JsonSerialize指定。
{
"id": 25,
"itemName": "FEDUfRgS",
"owner": 15
}
This is helpful when the ObjectMapper cannot be accessed and configured directly.
当ObjectMapper不能被直接访问和配置时,这很有帮助。
5. Conclusion
5.结论
This article illustrated how to get to a custom JSON output with Jackson 2, by using Serializers.
本文说明了如何通过使用序列化器,用Jackson 2获得自定义的JSON输出。
The implementation of all these examples and code snippets can be found on GitHub – this is a Maven-based project, so it should be easy to import and run as it is.
所有这些例子和代码片段的实现都可以在GitHub – 这是一个基于Maven的项目,所以应该很容易导入并按原样运行。