Gson 从序列化中排除字段
gson 从序列化中排除字段
默认情况下,gson 从序列化/反序列化过程中排除瞬态和静态字段。我们来看看下面的例子。
1) 范例
在c:> gson_workspace中创建名为 gsontester 的java类文件。
文件:gsontester.java
import com.google.gson.gson; import com.google.gson.gsonbuilder; public class gsontester { public static void main(string args[]) { gsonbuilder builder = new gsonbuilder(); gson gson = builder.create(); student student = new student(); student.setrollno(1); student.setname("mahesh kumar"); 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) 验证结果
使用 javac 编译器编译类如下 -
c:\gson_workspace>javac gsontester.java
现在运行 gsontester 查看结果 -
c:\gson_workspace>java gsontester
验证输出
{"rollno":1,"name":"mahesh kumar","verified":true}
1. 使用 excludefieldswithmodifiers
gsonbuilder使用来自序列化/反序列化过程的excludefieldswithmodifiers()方法控制使用特定修饰符排除字段。请参阅以下示例。
1) 范例
在c:> gson_workspace中创建名为gsontester的java类文件。
文件:gsontester.java
import java.lang.reflect.modifier; import com.google.gson.gson; import com.google.gson.gsonbuilder; public class gsontester { 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("mahesh kumar"); 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) 验证结果
使用 javac 编译器编译类如下:
c:\gson_workspace>javac gsontester.java
现在运行 gsontester 查看结果:
c:\gson_workspace>java gsontester
验证输出
{"rollno":1,"name":"mahesh kumar","verified":true,"classname":"vi"}
2. 使用@expose annotation
gson提供 @expose 注释来根据其范围控制类的json序列化/反序列化。考虑以下具有 @expose 支持的变量的类。在此类中,将公开 name 和 rollno 变量以进行序列化。然后我们使用 gsonbuilder.excludefieldswithoutexposeannotation() 方法来指示只有序列化/反序列化的公开变量。请参阅以下示例。
1) 范例
在c:> gson_workspace中创建名为 gsontester 的java类文件。
文件:gsontester.java
import com.google.gson.gson; import com.google.gson.gsonbuilder; import com.google.gson.annotations.expose; public class gsontester { public static void main(string args[]) { gsonbuilder builder = new gsonbuilder(); builder.excludefieldswithoutexposeannotation(); gson gson = builder.create(); student student = new student(); student.setrollno(1); student.setname("mahesh kumar"); 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; } }
2) 验证结果
使用 javac 编译器编译类如下 -
c:\gson_workspace>javac gsontester.java
现在运行 gsontester 查看结果 -
c:\gson_workspace>java gsontester
验证输出
{"rollno":1,"name":"mahesh kumar"}