Gson 简明教程
Gson - Excluding fields from Serialization
默认情况下,GSON 会将瞬态和静态字段排除在序列化/反序列化过程中。让我们来看一下下面的示例。
By default, GSON excludes transient and static fields from the serialization/deserialization process. Let’s take a look at the following example.
Example
在 C:\>GSON_WORKSPACE 创建一个名为 GsonTester 的 Java 类文件。
Create a Java class file named GsonTester in C:\>GSON_WORKSPACE.
File − GsonTester.java
File − 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;
}
}
Verify the result
使用以下 javac 编译器编译类:
Compile the classes using javac compiler as follows −
C:\GSON_WORKSPACE>javac GsonTester.java
现在运行 GsonTester 以查看结果 −
Now run the GsonTester to see the result −
C:\GSON_WORKSPACE>java GsonTester
验证输出
Verify the output
{"rollNo":1,"name":"Mahesh Kumar","verified":true}
Using excludeFieldsWithModifiers
GsonBuilder 提供对使用 excludeFieldsWithModifiers() 方法排除具有特殊修饰符的字段的控制,以排除序列化/反序列化过程。请参阅以下示例。
GsonBuilder provides control over excluding fields with particular modifier using excludeFieldsWithModifiers() method from serialization/deserialization process. See the following example.
Example
在 C:\>GSON_WORKSPACE 中创建一个名为 GsonTester 的 Java 类文件。
Create a Java class file named GsonTester in C:\>GSON_WORKSPACE.
File − GsonTester.java
File − 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;
}
}
Verify the result
使用以下 javac 编译器编译类:
Compile the classes using javac compiler as follows −
C:\GSON_WORKSPACE>javac GsonTester.java
现在运行 GsonTester 以查看结果 −
Now run the GsonTester to see the result −
C:\GSON_WORKSPACE>java GsonTester
验证输出
Verify the output
{"rollNo":1,"name":"Mahesh Kumar","verified":true,"className":"VI"}
Using @Expose Annotation
Gson 提供 @Expose 注释来控制基于类的作用域的 Json 序列化/反序列化。考虑具有变量具有 @Expose 支持的以下类。此类中, name 和 rollno 变量公开序列化。然后,我们使用 GsonBuilder.excludeFieldsWithoutExposeAnnotation() 方法来指示仅公开变量将被序列化/反序列化。请参阅以下示例。
Gson provides @Expose annotation to control the Json serialization/deserialization of a class based on its scope. Consider the following class with a variable having @Expose support. In this class, name and rollno variables are to be exposed for serialization. Then we’ve used the GsonBuilder.excludeFieldsWithoutExposeAnnotation() method to indicate that only exposed variables are to be serialized/deserialized. See the following example.
Example
在 C:\>GSON_WORKSPACE 创建一个名为 GsonTester 的 Java 类文件。
Create a Java class file named GsonTester in C:\>GSON_WORKSPACE.
File − GsonTester.java
File − 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;
}
}