zoukankan      html  css  js  c++  java
  • FastJson 自定义反序列化类

    反序列化类

    package com.seliote.demo.test;
    
    import com.alibaba.fastjson.parser.DefaultJSONParser;
    import com.alibaba.fastjson.parser.JSONToken;
    import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
    import org.springframework.util.Assert;
    
    import java.lang.reflect.Type;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeParseException;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * FastJson 时间格式自定义反序列化类
     *
     * @author LiYangDi
     * @since 2019/12/23
     */
    public class FastJsonLocalDateTimeDeserializer implements ObjectDeserializer {
    
        private static List<DateTimeFormatter> dateTimeFormatters = new LinkedList<>();
    
        static {
            // Add your own formatter to there
            dateTimeFormatters.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            dateTimeFormatters.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
            dateTimeFormatters.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"));
            dateTimeFormatters.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSS"));
        }
    
        @SuppressWarnings("unchecked")
        @Override
        public LocalDateTime deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
            final String input = parser.lexer.stringVal();
            LocalDateTime localDateTime = null;
            for (DateTimeFormatter dateTimeFormatter : dateTimeFormatters) {
                try {
                    localDateTime = LocalDateTime.parse(input, dateTimeFormatter);
                    // Format success, step over other DateTimeFormatter
                    break;
                } catch (DateTimeParseException ex) {
                    // do nothing to use next formatter
                }
            }
            Assert.notNull(localDateTime, "FastJson LocalDateTime use" +
                    " FastJsonTimestampDeserializer format error: " + input);
            return localDateTime;
        }
    
        @Override
        public int getFastMatchToken() {
            return JSONToken.LITERAL_INT;
        }
    }
    

    测试

    POJO

    package com.seliote.demo.pojo;
    
    import com.alibaba.fastjson.annotation.JSONField;
    import com.seliote.demo.test.FastJsonLocalDateTimeDeserializer;
    import lombok.Data;
    
    import java.time.LocalDateTime;
    
    /**
     * 测试 POJO
     *
     * @author LiYangDi
     * @since 2019/12/23
     */
    @Data
    public class Pojo {
    
        @JSONField(name = "time", deserializeUsing = FastJsonLocalDateTimeDeserializer.class)
        //@JSONField(name = "time")
        private LocalDateTime localDateTime;
    
        @JSONField(name = "name")
        private String name;
    }
    

    测试类

    package com.seliote.demo.test;
    
    import com.alibaba.fastjson.JSONObject;
    import com.seliote.demo.pojo.Pojo;
    import lombok.extern.slf4j.Slf4j;
    
    /**
     * <p>
     * 测试类
     * </p>
     *
     * @author LiYangDi
     * @since 2019/11/20
     */
    @Slf4j
    public class FastJsonTest {
    
        public static void main(String... args) {
            final String json = "{"time":"2019-12-23 20:18:02.111111111", "name":"seliote"}";
            Pojo pojo = JSONObject.parseObject(json, Pojo.class);
        }
    }
    
  • 相关阅读:
    wnmpa或lnmpa 服务器搭建和原理
    windows 桌面图标 隐藏 小盾牌标志
    C# 执行 CMD 终极稳定解决方案
    比较两个object是否相等
    Microsoft Store 加载失败
    ORA-12514: TNS:监听程序当前无法识别连接描述符中请求的服务
    Win10安装gcc、g++、make
    通过proxifier实现酸酸乳全局代理
    C# 字母转数字
    html中设置锚点定位的几种常见方法(#号定位)
  • 原文地址:https://www.cnblogs.com/seliote/p/12088661.html
Copyright © 2011-2022 走看看