2 s510626666 S510626666 于 2015.06.01 09:38 提问

Java 8 代码要写junit测试,但是看不懂代码请大神指教
@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;
}

}

2个回答

edouardzyc
edouardzyc   2015.06.01 09:50
已采纳

粗看下,一共做了5个动作,都是json的序列化和反序列化操作
1. LocalDate序列化
2. LocalDate反序列化
3. LocalDateTime序列化
4. LocalDateTime反序列化
5. ZonedDateTime序列化
应该是测试不同的时间类型对json的序列化和反序列化

caozhy
caozhy   Ds   Rxr 2015.06.01 09:51
Csdn user default icon
上传中...
上传图片
插入图片