本文介绍下fastjson自定义序列化的各种操作。
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
添加如下maven依赖即可:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency>
fastjson入口类是com.alibaba.fastjson.JSON,一个最简单默认的序列化代码如下:
User user = new User(); String str= JSON.toJSONString(user);
fastjson支持多种方式自定义序列化。
SerializerFeature常用属性如下:
名称 | 含义 |
---|---|
WriteNonStringKeyAsString | 如果key不为String,则转换为String |
WriteNonStringValueAsString | 如果value不为String,则转换为String |
WriteMapNullValue | 输出为空的字段 |
WriteNullStringAsEmpty | String为null时输出"" |
WriteNullNumberAsZero | number为null时输出0 |
WriteDateUseDateFormat | 修改日期格式,yyyy-MM-dd |
常用的属性介绍完了,咱们先来看下效果,新建一个User类,代码如下:
@Data public class User implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private Integer age; private String name; private String sex; private Date createDate; private Map<Integer, Integer> dataMap; }
运行类代码如下:
public static void main(String[] args) { User user = new User(); user.setId(1000); user.setName("Java碎碎念"); user.setCreateDate(new Date()); Map<Integer, Integer> datamap = new HashMap<>(); datamap.put(1, 100); datamap.put(2, 200); user.setDataMap(datamap); System.out.println("默认序列化结果:/n" + JSON.toJSONString(user)); System.out.println("指定WriteNonStringKeyAsString序列化结果:/n" + JSON.toJSONString(user, SerializerFeature.WriteNonStringKeyAsString)); System.out.println("指定WriteNonStringValueAsString序列化结果:/n" + JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString)); System.out.println("指定WriteMapNullValue序列化结果:/n" + JSON.toJSONString(user, SerializerFeature.WriteMapNullValue)); System.out.println("指定WriteNullStringAsEmpty序列化结果:/n" + JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty)); System.out.println("指定WriteNullNumberAsZero序列化结果:/n" + JSON.toJSONString(user, SerializerFeature.WriteNullNumberAsZero)); System.out.println("指定WriteDateUseDateFormat序列化结果:/n" + JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat)); }
运行结果:
默认序列化结果: {"createDate":1575038247084,"dataMap":{1:100,2:200},"id":1000,"name":"Java碎碎念"} 指定WriteNonStringKeyAsString序列化结果: {"createDate":1575038247084,"dataMap":{"1":100,"2":200},"id":1000,"name":"Java碎碎念"} 指定WriteNonStringValueAsString序列化结果: {"createDate":1575038247084,"dataMap":{1:"100",2:"200"},"id":"1000","name":"Java碎碎念"} 指定WriteMapNullValue序列化结果: {"age":null,"createDate":1575038247084,"dataMap":{1:100,2:200},"id":1000,"name":"Java碎碎念","sex":null} 指定WriteNullStringAsEmpty序列化结果: {"createDate":1575038247084,"dataMap":{1:100,2:200},"id":1000,"name":"Java碎碎念","sex":""} 指定WriteNullNumberAsZero序列化结果: {"age":0,"createDate":1575038247084,"dataMap":{1:100,2:200},"id":1000,"name":"Java碎碎念"} 指定WriteDateUseDateFormat序列化结果: {"createDate":"2019-11-29 22:37:27","dataMap":{1:100,2:200},"id":1000,"name":"Java碎碎念"}
JSONField是fastjson的一个注解,可以用在字段上,也可以用在getter/setter方法上面。
2.1 JSONField注解
注解主要内容如下:
package com.alibaba.fastjson.annotation; public @interface JSONField { // 配置序列化和反序列化的顺序,1.1.42版本之后才支持 int ordinal() default 0; // 指定字段的名称 String name() default ""; // 指定字段的格式,对日期格式有用 String format() default ""; // 是否序列化 boolean serialize() default true; // 是否反序列化 boolean deserialize() default true; }
2.2 JSONField注解配置方式
@Data public class User implements Serializable { @JSONField(name = "_id") private Integer id; }
@JSONField(name="_id") public Integer getId() { return id; } @JSONField(name="_id") public void setId(Integer id) { this.id = id; }
@JSONField(format="yyyy-MM-dd") private Date createDate;
@JSONField(serialize=false) private Integer age;
@JSONField(ordinal = 2) private String name;
下面直接修改代码看下效果,修改后User类如下:
@Data public class User implements Serializable { private static final long serialVersionUID = 1L; @JSONField(name = "_id", ordinal = 3) private Integer id; @JSONField(serialize = false, ordinal = 4) private Integer age; @JSONField(ordinal = 1) private String name; @JSONField(ordinal = 2) private String sex; @JSONField(format = "yyyy-MM-dd", ordinal = 5) private Date createDate; }
测试类运行结果如下:
默认序列化结果: {"age":18,"createDate":1575006127174,"id":1000,"name":"Java碎碎念"} 使用JSONField后序列化结果: {"name":"Java碎碎念","_id":1000,"createDate":"2019-11-29"}
通过运行结果可以看到,使用@JSONField注解变化如下:
结果和预期一致,说明自定义序列化没有问题,到此fastjson自定义序列化的功能已经全部实现,有问题欢迎留言沟通哦!