码迷,mamicode.com
首页 > Web开发 > 详细

@JSONField的使用

时间:2018-06-24 19:38:32      阅读:453      评论:0      收藏:0      [点我收藏+]

标签:src   info   return   顺序   for   getname   通过   name   []   

fastjson支持多种方式定制序列化

  • 通过@JSONField定制序列化
  • 通过@JSONType定制序列化
  • 通过SerializeFilter定制序列化
  • 通过ParseProcess定制反序列化

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);
    }

}

技术分享图片

@JSONField的使用

标签:src   info   return   顺序   for   getname   通过   name   []   

原文地址:https://www.cnblogs.com/i-hard-working/p/9221254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!