| AndBase开发框架 1.6

- 首页
- 相关页面
- 命名空间
- 类
- 文件

- 类列表
- 类索引
- 类继承关系
- 类成员

全部 类 命名空间 文件 函数 变量 枚举值 页
Public 成员函数 | 所有成员列表
com.google.gson.ExclusionStrategy接口 参考

## Public 成员函数
boolean | shouldSkipField (FieldAttributes f)
boolean | shouldSkipClass (Class<?> clazz)

## 详细描述

A strategy (or policy) definition that is used to decide whether or not a field or top-level class should be serialized or deserialized as part of the JSON output/input. For serialization, if the shouldSkipClass(Class) method returns false then that class or field type will not be part of the JSON output. For deserialization, if shouldSkipClass(Class) returns false, then it will not be set as part of the Java object structure.

The following are a few examples that shows how you can use this exclusion mechanism.

Exclude fields and objects based on a particular class type:

    private static class SpecificClassExclusionStrategy implements ExclusionStrategy {
      private final Class<?> excludedThisClass;

      public SpecificClassExclusionStrategy(Class<?> excludedThisClass) {
        this.excludedThisClass = excludedThisClass;
      }

      public boolean shouldSkipClass(Class<?> clazz) {
        return excludedThisClass.equals(clazz);
      }

      public boolean shouldSkipField(FieldAttributes f) {
        return excludedThisClass.equals(f.getDeclaredClass());
      }
    }

Excludes fields and objects based on a particular annotation:

    public &#64interface FooAnnotation {
      // some implementation here
    }

    // Excludes any field (or class) that is tagged with an "&#64FooAnnotation"
    private static class FooAnnotationExclusionStrategy implements ExclusionStrategy {
      public boolean shouldSkipClass(Class<?> clazz) {
        return clazz.getAnnotation(FooAnnotation.class) != null;
      }

      public boolean shouldSkipField(FieldAttributes f) {
        return f.getAnnotation(FooAnnotation.class) != null;
      }
    }

Now if you want to configure

Gson

to use a user defined exclusion strategy, then the

GsonBuilder

is required. The following is an example of how you can use the

GsonBuilder

to configure Gson to use one of the above sample:

    ExclusionStrategy excludeStrings = new UserDefinedExclusionStrategy(String.class);
    Gson gson = new GsonBuilder()
        .setExclusionStrategies(excludeStrings)
        .create();

For certain model classes, you may only want to serialize a field, but exclude it for deserialization. To do that, you can write an

ExclusionStrategy

as per normal; however, you would register it with the GsonBuilder#addDeserializationExclusionStrategy(ExclusionStrategy) method. For example:

    ExclusionStrategy excludeStrings = new UserDefinedExclusionStrategy(String.class);
    Gson gson = new GsonBuilder()
        .addDeserializationExclusionStrategy(excludeStrings)
        .create();

作者
  Inderjeet Singh
  Joel Leitch

参见
  GsonBuilder::setExclusionStrategies(ExclusionStrategy...)
  GsonBuilder::addDeserializationExclusionStrategy(ExclusionStrategy)
  GsonBuilder::addSerializationExclusionStrategy(ExclusionStrategy)

自从
  1.4

## 成员函数说明

boolean com.google.gson.ExclusionStrategy.shouldSkipClass | ( | Class<?> | clazz | ) |

参数

  clazz | the class object that is under test

返回
  true if the class should be ignored; otherwise false

boolean com.google.gson.ExclusionStrategy.shouldSkipField | ( | FieldAttributes | f | ) |

参数

  f | the field object that is under test

返回
  true if the field should be ignored; otherwise false

该接口的文档由以下文件生成:

- src/com/google/gson/ExclusionStrategy.java