Gson-序列化时排除字段

1 默认情况排序字段的示例

默认情况下,Gson会把瞬态(transient)字段和静态(static)字段在序列化/反序列化过程中排除。

1.1 编写核心类

MainApp:

package com.yiidian.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]) {

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();

        Student student = new Student();
        student.setRollNo(1);
        student.setName("eric");
        student.setVerified(true);
        student.setId(1);
        student.className = "VI";

        String jsonString = gson.toJson(student);
        System.out.println(jsonString);
    }

}

class Student {
    private int rollNo;
    private String name;
    private boolean verified;
    //瞬态字段
    private transient int id;
    //静态字段
    public static String className;

    public int getRollNo() {
        return rollNo;
    }

    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setVerified(boolean verified) {
        this.verified = verified;
    }

    public boolean isVerified() {
        return verified;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

1.2 运行测试

2 excludeFieldsWithModifiers方法排序字段的示例

另外,我们可以使用GsonBuilder类的excludeFieldsWithModifiers() 方法对具有特定修饰符的字段进行排除。

2.1 编写核心类

MainApp:

package com.yiidian.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Modifier;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]) {

        GsonBuilder builder = new GsonBuilder();
        //只对瞬态字段进行排除
        builder.excludeFieldsWithModifiers(Modifier.TRANSIENT);
        Gson gson = builder.create();

        Student student = new Student();
        student.setRollNo(1);
        student.setName("eric");
        student.setVerified(true);
        student.setId(1);
        student.className = "VI";

        String jsonString = gson.toJson(student);
        System.out.println(jsonString);
    }

}

class Student {
    private int rollNo;
    private String name;
    private boolean verified;
    //瞬态字段
    private transient int id;
    //静态字段
    public static String className;

    public int getRollNo() {
        return rollNo;
    }

    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setVerified(boolean verified) {
        this.verified = verified;
    }

    public boolean isVerified() {
        return verified;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

2.2 运行测试

3 @Expose注解排除字段的示例

Gson提供@Expose注解,用上@Expose注解,可以控制是否把对应的属性排除在序列化/反序列之外。调用GsonBuilder的excludeFieldsWithoutExposeAnnotation() 方法可以把没有添加@Expose注解的字段在序列化/反序列过程排除掉。

3.1 编写核心类

MainApp:

package com.yiidian.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]) {

        GsonBuilder builder = new GsonBuilder();
        //排除没有添加@Expose注解的字段
        builder.excludeFieldsWithoutExposeAnnotation();
        Gson gson = builder.create();

        Student student = new Student();
        student.setRollNo(1);
        student.setName("eric");
        student.setVerified(true);
        student.setId(1);
        student.className = "VI";

        String jsonString = gson.toJson(student);
        System.out.println(jsonString);
    }

}

class Student {
    @Expose
    private int rollNo;

    @Expose
    private String name;
    private boolean verified;
    private int id;
    public static String className;

    public int getRollNo() {
        return rollNo;
    }
    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setVerified(boolean verified) {
        this.verified = verified;
    }
    public boolean isVerified() {
        return verified;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
}

3.2 运行测试

热门文章

优秀文章