zoukankan      html  css  js  c++  java
  • 定制序列化之@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);
        }
    
    }
    

  • 相关阅读:
    一行命令搞定AD数据库备份
    PowerShell实战4:批量修改AD账户EMail属性
    PowerShell实战1:Ping_Test
    分享/etc/sysctl.conf和/boot/loader.conf优化
    PowerShell实战5: 批量增加AD组成员
    bsd压力测试工具
    kernel : arp .....的訊息
    将域控制器移到新的 Active Directory 站
    pf限速限链接数
    FreeBSD笔记
  • 原文地址:https://www.cnblogs.com/i-hard-working/p/9221254.html
Copyright © 2011-2022 走看看