Gson从序列化中排除字段
默认情况下,GSON排除序列化/反序列化过程中的瞬态和静态字段。 我们来看看下面的例子。
示例
创建一个名为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("Maxsu"); 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; } }
执行上面示例代码,得到以下结果 -
{"rollNo":1,"name":"Maxsu","verified":true}
使用excludeFieldsWithModifiers()方法
GsonBuilder使用序列化/反序列化过程中的excludeFieldsWithModifiers()
方法提供对使用特定修饰符排除字段的控制。 看下面的例子。
示例
创建一个名为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("Maxsu"); 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; } }
执行上面示例代码,得到以下结果 -
{"rollNo":1,"name":"Maxsu","verified":true,"className":"VI"}
使用@Expose注解
Gson提供@Expose
注解来根据其范围控制类的Json序列化/反序列化。 考虑以下具有@Expose
支持的变量的类。 在这个类中,name
和rollno
变量将被暴露以进行序列化。 然后,我们使用GsonBuilder.excludeFieldsWithoutExposeAnnotation()
方法来指示只有暴露的变量要被序列化/反序列化。 看下面的例子。
示例
创建一个名为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("Maxsu"); 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; } }
执行上面示例代码,得到以下结果 -
{"rollNo":1,"name":"Maxsu"}
上一篇:Gson版本支持
下一篇:没有了