@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true)
.serializerByType(LocalDate.class, new JsonSerializer<LocalDate>() {
@Override
public void serialize(LocalDate value, JsonGenerator jgen,
SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(value.toString());
}
})
.deserializerByType(LocalDate.class, new JsonDeserializer<LocalDate>() {
@Override
public LocalDate deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException, JsonProcessingException {
return LocalDate.parse(jp.getValueAsString());
}
})
.serializerByType(LocalDateTime.class, new JsonSerializer<LocalDateTime>() {
@Override
public void serialize(LocalDateTime value, JsonGenerator jgen,
SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(value.toString());
}
})
.deserializerByType(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() {
@Override
public LocalDateTime deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException, JsonProcessingException {
return LocalDateTime.parse(jp.getValueAsString());
}
})
.serializerByType(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() {
@Override
public void serialize(ZonedDateTime value, JsonGenerator jgen,
SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString(value.toOffsetDateTime()
.toString());
}
});
return builder;
}
}
粗看下,一共做了5個動作,都是json的序列化和反序列化操作
1. LocalDate序列化
2. LocalDate反序列化
3. LocalDateTime序列化
4. LocalDateTime反序列化
5. ZonedDateTime序列化
應該是測試不同的時間類型對json的序列化和反序列化