gson使用教程-翻译8

原文地址: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;
    }
}

你可能感兴趣的:(java,gson)