Jackson ObjectMapper readValue过程

v1.整体调用栈

v2.看一下调用栈的两个方法

   

  

  

  resolve 方法中通过 Iterator i$ = this._beanProperties.iterator() 遍历属性的所有子属性,缓存对应的 deserializer。观察调用栈的方法,可以发现是循环调用的。

 

v3.比如寻找自定义的 LocalDateTime类的序列化实现类,看方法调用栈最上边的方法

  

 

  如果没有找到用户自定义的反序列化工具,则去找默认的标准反序列化工具

  

  

  

  deser = NumberDeserializers.find(rawType, clsName);
deser = DateDeserializers.find(rawType, clsName); 

  可以看一下 com.fasterxml.jackson.databind.deser.std.DateDeserializers 和 com.fasterxml.jackson.databind.deser.std.NumberDeserializers,遍豁然开朗。

v4.加入对应类型序列化工具类

复制代码
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import xxx.utils.json.deserializers.LocalDateDeserializer;
import xxx.utils.json.deserializers.LocalDateTimeDeserializer;
import xxx.utils.json.deserializers.LocalTimeDeserializer;
import xxx.utils.json.serializers.BigDecimalSerializer;
import xxx.utils.json.serializers.LocalDateSerializer;
import xxx.utils.json.serializers.LocalDateTimeSerializer;
import xxx.utils.json.serializers.LocalTimeSerializer;

public class JacksonHelper {
    private static final SimpleModule module = initModule();
    private static final ObjectMapper mapper;
    private static final ObjectMapper prettyMapper;

    public JacksonHelper() {
    }

    private static SimpleModule initModule() {
        return (new SimpleModule()).addSerializer(BigDecimal.class, new BigDecimalSerializer()).addSerializer(LocalTime.class, new LocalTimeSerializer()).addDeserializer(LocalTime.class, new LocalTimeDeserializer()).addSerializer(LocalDate.class, new LocalDateSerializer()).addDeserializer(LocalDate.class, new LocalDateDeserializer()).addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()).addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
    }

    public static JavaType genJavaType(Type type) {
        return getMapper().getTypeFactory().constructType(type);
    }

    public static ObjectMapper getMapper() {
        return mapper;
    }

    public static ObjectMapper getPrettyMapper() {
        return prettyMapper;
    }

    static {
        mapper = (new ObjectMapper()).registerModule(module).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true).configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        prettyMapper = mapper.copy().configure(SerializationFeature.INDENT_OUTPUT, true);
    }
}
复制代码
MAPPER = JacksonHelper.getMapper().registerModule((new SimpleModule(LocalDateTimeDeserializer2.class.getName())).addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer2()));
        MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);

 

复制代码
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
    public LocalDateTimeDeserializer() {
    }

    public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        String dateTimeStr = ((JsonNode)jp.getCodec().readTree(jp)).asText();
        return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    }
}
复制代码
复制代码
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeDeserializer2 extends JsonDeserializer<LocalDateTime> {
    public LocalDateTimeDeserializer2() {
    }

    public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        String dateTimeStr = ((JsonNode)jp.getCodec().readTree(jp)).asText();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(dateTimeStr, df);
    }
}
复制代码

 

  可见,ObjectMapper registerModule 最后注册的module会优先被发现。例如上边首先 第一个 Module加入了一个LocalDateTime反序列化工具类LocalDateTimeDeserializer,接着第二个Module加入了LocalDateTime反序列化工具类LocalDateTimeDeserializer2,最后得到的反序列化工具类是LocalDateTimeDeserializer2。

v5.java.util.Date日期类型解析

  日期格式:yyyy-MM-dd HH:mm:ss

  第一种方法:MAPPER.setConfig(MAPPER.getDeserializationConfig().with(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));

  第二种方法:自定义反序列化 MAPPER.registerModule((new SimpleModule(Date.class.getName())).addDeserializer(Date.class, DateDeserializer2.dateDeserializer));

复制代码
import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DateDeserializer2 extends DateDeserializers.DateDeserializer{
    public static final DateDeserializer2 dateDeserializer = new DateDeserializer2(
            DateDeserializers.DateDeserializer.instance
            , new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");

    public DateDeserializer2() {}

    public DateDeserializer2(DateDeserializers.DateDeserializer base, DateFormat df, String formatString) {
        super(base, df, formatString);
    }
}
复制代码

 

  默认的Date解析通过 DateDeserializers.DateDeserializer,时间的格式化处理是调用自己的StdDateFormat类来实现日期格式化

  

  而StdDateFormat定义的格式化如下

  

   

  DeserializationContext中为啥可以获取StdDateFormat(objectMapper readValue 时会创建DeserializationContext, 注入DeserializationConfig【包含BaseSettings(包含DateFormat)】)

  

  

 

  

 










本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/8108488.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值