@JsonSerialize

1. 针对属性

@JsonSerialize 注解可指定自定义序列化器来序列化实体,可以配置在实体类、Getter 方法或属性字段上:

  1. @Getter
  2. @Setter
  3. public static class User {
  4. private String name;
  5. @JsonSerialize(using = CustomDateSerializer.class)
  6. private Date birthday;
  7. }

我们显式对 User 实体的 birthday 属性进行自定义的序列化逻辑:

  1. public class CustomDateSerializer extends StdSerializer<Date> {
  2. private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  3. public CustomDateSerializer() {
  4. this(null);
  5. }
  6. protected CustomDateSerializer(Class<Date> t) {
  7. super(t);
  8. }
  9. @Override
  10. public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException {
  11. gen.writeString(format.format(value));
  12. }
  13. }

对其进行序列化:

  1. public static void main(String[] args) throws JsonProcessingException {
  2. User user = new User();
  3. user.setName("张三");
  4. user.setBirthday(new Date());
  5. System.out.println(MAPPER.writeValueAsString(user));
  6. }
  7. // 输出结果:{"name":"张三","birthday":"2021-09-10 11:13:33"}

2. 针对对象

如果想要对整个类进行自定义的序列化:

  1. @Getter
  2. @Setter
  3. @JsonSerialize(using = CustomUserSerializer.class)
  4. public static class User {
  5. private String name;
  6. private Date birthday;
  7. }

自定义序列化器的逻辑为:

  1. public class CustomUserSerializer extends StdSerializer<JsonSerializeTest.User> {
  2. private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  3. public CustomUserSerializer() {
  4. this(null);
  5. }
  6. protected CustomUserSerializer(Class<JsonSerializeTest.User> t) {
  7. super(t);
  8. }
  9. @Override
  10. public void serialize(JsonSerializeTest.User value, JsonGenerator gen, SerializerProvider provider) throws IOException {
  11. gen.writeStartObject();
  12. gen.writeStringField("name", value.getName() + "^^");
  13. gen.writeStringField("birthday", format.format(value.getBirthday()) + "^^");
  14. gen.writeEndObject();
  15. }
  16. }
  17. // 输出结果:{"name":"张三^^","birthday":"2021-09-10 13:02:26^^"}

@JsonDeserialize

1. 针对属性

�@JsonDeserialize 注解用于自定义反序列化的逻辑:

  1. @Getter
  2. @Setter
  3. public static class User {
  4. private String name;
  5. @JsonDeserialize(using = CustomDateDeserializer.class)
  6. private Date birthday;
  7. }

自定义反序列化器:

  1. public class CustomDateDeserializer extends StdDeserializer<Date> {
  2. private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  3. public CustomDateDeserializer() {
  4. this(null);
  5. }
  6. protected CustomDateDeserializer(Class<?> vc) {
  7. super(vc);
  8. }
  9. @Override
  10. public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  11. String date = p.getText();
  12. try {
  13. return format.parse(date);
  14. } catch (ParseException e) {
  15. throw new RuntimeException(e);
  16. }
  17. }
  18. }

对其进行反序列化:

  1. public static void main(String[] args) throws JsonProcessingException {
  2. User userD = MAPPER.readValue("{\"name\":\"张三\",\"birthday\":\"2021-09-10 11:13:33\"}", User.class);
  3. System.out.println(userD.getBirthday());
  4. }
  5. // 输出结果:Fri Sep 10 11:13:33 CST 2021
  6. // 如果不加自定义的反序列化器,Jackson 默认是无法识别这种日期格式的

2. 针对对象

如果想要对整个类进行自定义的反序列化:

  1. @Getter
  2. @Setter
  3. @JsonDeserialize(using = CustomUserDeserializer.class)
  4. public static class User {
  5. private String name;
  6. private Date birthday;
  7. }

自定义反序列化器的逻辑为:

  1. public class CustomUserDeserializer extends StdDeserializer<JsonDeserializeTest.User> {
  2. private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  3. public CustomUserDeserializer() {
  4. this(null);
  5. }
  6. protected CustomUserDeserializer(Class<?> vc) {
  7. super(vc);
  8. }
  9. @Override
  10. public JsonDeserializeTest.User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  11. JsonDeserializeTest.User user = new JsonDeserializeTest.User();
  12. ObjectCodec codec = p.getCodec();
  13. JsonNode jsonNode = codec.readTree(p);
  14. try {
  15. String name = jsonNode.get("name").asText();
  16. user.setName(name);
  17. String birthday = jsonNode.get("birthday").asText();
  18. user.setBirthday(format.parse(birthday));
  19. return user;
  20. } catch (Exception e) {
  21. throw new RuntimeException(e);
  22. }
  23. }
  24. }

通过 Module 注册

通过注解声明的方式比较局限,此外,我们也可以通过 Jackson 提供的 Module 机制把自定义的序列化、反序列化器注册到 ObjectMapper 上:

  1. CustomUserDeserializer userDeserializer = new CustomUserDeserializer();
  2. // 创建一个模块,将自定义序列化、反序列化器注册到模块
  3. SimpleModule module = new SimpleModule();
  4. module.addDeserializer(JsonDeserializeTest.User.class, userDeserializer);
  5. // 注册模块到ObjectMapper
  6. MAPPER.registerModule(module);