定制序列化之@JSONField的使用
fastjson支持多种方式定制序列化:
- 通过@JSONField定制序列化
- 通过@JSONType定制序列化
- 通过SerializeFilter定制序列化
- 通过ParseProcess定制反序列化
@JSONField的使用:
1. 利用@JSONField的format配置日期格式化,这个在上一篇文章有所展示。
2. 利用@JSONField的serialize指定字段不序列化:
package com.ant.jdk8.json; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; public class User { private String name; @JSONField(serialize=false) private int age; @JSONField(format="yyyy-MM-dd HH:mm:ss") private Date hireDate; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public User(String name, int age, Date hireDate) { this.name = name; this.age = age; this.hireDate = hireDate; } public User() { } }
package com.ant.jdk8.json; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.Date; import java.util.List; public class JsonDemo { public static void main(String[] args) { convertCollectionToJson(); } public static void convertCollectionToJson(){ List<User> userList = new ArrayList<>(); userList.add(new User("tom",20,new Date())); userList.add(new User("jack",25,new Date())); String jsonString = JSON.toJSONString(userList); System.out.println(jsonString); } }
3. 利用@JSONField的ordinal指定序列化的顺序(这个特性需要1.1.42以上版本):
package com.ant.jdk8.json; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; public class User { @JSONField(ordinal=3) private String name; @JSONField(ordinal=2) private int age; @JSONField(format="yyyy-MM-dd HH:mm:ss",ordinal = 1) private Date hireDate; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public User(String name, int age, Date hireDate) { this.name = name; this.age = age; this.hireDate = hireDate; } public User() { } }
package com.ant.jdk8.json; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.Date; import java.util.List; public class JsonDemo { public static void main(String[] args) { convertCollectionToJson(); } public static void convertCollectionToJson(){ List<User> userList = new ArrayList<>(); userList.add(new User("tom",20,new Date())); userList.add(new User("jack",25,new Date())); String jsonString = JSON.toJSONString(userList); System.out.println(jsonString); } }
4. 利用@JSONField的name指定字段的名称:
package com.ant.jdk8.json; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; public class User { @JSONField(ordinal=3,name="username") private String name; @JSONField(ordinal=2) private int age; @JSONField(format="yyyy-MM-dd HH:mm:ss",ordinal = 1) private Date hireDate; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public User(String name, int age, Date hireDate) { this.name = name; this.age = age; this.hireDate = hireDate; } public User() { } }
package com.ant.jdk8.json; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.Date; import java.util.List; public class JsonDemo { public static void main(String[] args) { convertCollectionToJson(); } public static void convertCollectionToJson(){ List<User> userList = new ArrayList<>(); userList.add(new User("tom",20,new Date())); userList.add(new User("jack",25,new Date())); String jsonString = JSON.toJSONString(userList); System.out.println(jsonString); } }
posted on 2018-06-24 18:03 shammgod_code 阅读(6695) 评论(0) 编辑 收藏 举报