当前位置:   article > 正文

自定义fastjson反序列化

自定义fastjson反序列化

fastjson自定义反序列化实现类,demo对一个字符串进行日期格式化(可兼容多重格式)、String转Double和String转Integer。

1.自定义fastjson字符串日期格式化

自定义类DateCompatibilityFormat,继承ObjectDeserializer,如下:
  • 1
	/**
	 * fastjson custom class
	 * chenyouhong
	 * date format
	 */
	@Slf4j
	public class DateCompatibilityFormat implements ObjectDeserializer {


		//格式化样式
	    private static final List<String> formatStyles = Lists.newArrayList("yyyy.MM.dd", "yyyy.MM", "yyyy.MM.dd hh:mm:ss");

	    //关键代码
	    @Override
	    public Date deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
	        DateTime dateTime = null;
	        try {
	            for (String formatStyle: formatStyles) {
	                dateTime = this.getDateTime(formatStyle, parser.getLexer().stringVal());
	                if (dateTime != null) break;
	            }
	        } catch (Exception e) {
	            log.info("error {}", e);
	        }

	        return dateTime != null ? dateTime.toDate() : null;
	    }

	    //日期格式化
	    private DateTime getDateTime(String sFormat, String sDate) {
	        DateTime dateTime = null;
	        try {
	            dateTime = DateTimeFormat.forPattern(sFormat).parseDateTime(sDate);
	        } catch (Exception e) {
	            log.info("date format error, format={}, date={}, error={}", sFormat, sDate, e);
	        }

	        return dateTime;
	    }

	    //暂未知道具体作用
	    @Override
	    public int getFastMatchToken() {
	        return 0;
	    }

	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

2.对应Model加上相应的注解即可


	@Data
	public class SysUser implements Serializable {

	    private static final long serialVersionUID = 1L;

	    private String username;

	    private String loginCode;

	    private String password;

	    //日期格式化
	    @JSONField(deserializeUsing = DateCompatibilityFormat.class)
	    private Date createDate;

	    private String mobile;

		//String转Integer
	    @JSONField(deserializeUsing = StringToIntegerFormat.class)
	    private Integer age;

		//String转Double
	    @JSONField(deserializeUsing = StringToDoubleFormat.class)
	    private Double weight;

	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

3.测试方法

	@Test
	public void testFormat() {
		String json = " {\"createDate\":\"2015.03\", \"age\":\"20,189.89\"}";
		SysUser sysUser = JSON.parseObject(json, SysUser.class);

        try {

            DateTime dateTime = new DateTime(sysUser.getCreateDate());
            String s = dateTime.toString("yyyy-MM-dd");
            System.out.println(s);
            s = dateTime.toString("yyyy-MM");
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(JSON.toJSONString(sysUser));

	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

demo链接:https://github.com/13162576590/fastjson_demo

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/982413
推荐阅读
相关标签
  

闽ICP备14008679号