原文地址:http://www.studytrails.com/java/json/java-google-json-exclusion-strategy.jsp
在這一節中,我們將會看到如何選擇Java對象中的某些屬性轉換成json.Gson默認是會把java對象中所有的屬性都轉換成Json.然而,有時候我們想控制那些屬性轉,那些屬性不轉.這種控制的方法有幾種.甚至對於沒有源碼的類型都是可以的.不同的方式:
- 使用自定義的註解來忽略註解的屬性
- 通過實現ExclusionStrategy接口和實現shouldSkipFiled和shouldSkipClass方法
- 通過使用@Expose註解和在GsonBuilder使用excludeFiledsWithoutExposeAnnotation().這樣就會忽略所有沒有使用@Expose註解的屬性.
以下的demo展示這三種方式
package com.studytrails.json.gson;
import java.awt.Color;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class ExclusionExample {
public static void main(String[] args) {
// We create an instance of type CAT.
Cat cat = new Cat();
cat.setName("Cat");
cat.setAge(1);
cat.setColor(Color.BLACK);
cat.setCountry("US");
// we allow serializing null. therefore although the fields lazy is
// null, it will be serialized. We add a CustomExclusionStrategy that
// will exclude the Color class. We also allow only those fields that
// have been exposed using the @Expore annotation
Gson gson = new GsonBuilder().serializeNulls().setExclusionStrategies(new CustomExclusionStrategy(Color.class))
.excludeFieldsWithoutExposeAnnotation().create();
System.out.println(gson.toJson(cat));
// prints {"name":"Cat","lazy":null}
}
}
Cat類
package com.studytrails.json.gson;
import java.awt.Color;
import com.google.gson.annotations.Expose;
public class Cat {
@Expose
private String name;
private int age;
private Color color;
@Expose
@Country
private String country;
@Expose
private Boolean lazy = null;
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setColor(Color color) {
this.color = color;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public Color getColor() {
return color;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return country;
}
public void setLazy(Boolean lazy) {
this.lazy = lazy;
}
public Boolean getLazy() {
return lazy;
}
}
Exclusion Strategy
package com.studytrails.json.gson;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
/**
* This class defines custom exclusion policy. We want to ignore all fields that
* have been annotated with the Country annotation. Note that we can also ignore
* fields based on name or type. This same policy can be applied to any class.
* In this example we apply to the CAT class, but it is not limited to the cat
* class.
*
*/
public class CustomExclusionStrategy implements ExclusionStrategy {
private Class classToExclude;
public CustomExclusionStrategy(Class classToExclude) {
this.classToExclude = classToExclude;
}
// This method is called for all fields. if the method returns false the
// field is excluded from serialization
@Override
public boolean shouldSkipField(FieldAttributes f) {
if (f.getAnnotation(Country.class) == null)
return false;
return true;
}
// This method is called for all classes. If the method returns false the
// class is excluded.
@Override
public boolean shouldSkipClass(Class<?> clazz) {
if (clazz.equals(classToExclude))
return true;
return false;
}
}