2008-09-01 05:13:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Google Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.google.gson;
|
|
|
|
|
|
|
|
/**
|
2009-10-07 11:23:14 +02:00
|
|
|
* A strategy pattern (see "Design Patterns" written by GoF for some literature on this pattern)
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* <p>The following example show an implementation of an {@code ExclusionStrategy} where a specific
|
|
|
|
* type will be excluded from the output.
|
|
|
|
*
|
|
|
|
* <p><pre class="code">
|
|
|
|
* private static class UserDefinedExclusionStrategy implements ExclusionStrategy {
|
|
|
|
* private final Class<?> excludedThisClass;
|
|
|
|
*
|
|
|
|
* UserDefinedExclusionStrategy(Class<?> excludedThisClass) {
|
|
|
|
* this.excludedThisClass = excludedThisClass;
|
|
|
|
* }
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
2009-10-07 11:23:14 +02:00
|
|
|
* public boolean shouldSkipClass(Class<?> clazz) {
|
|
|
|
* return excludedThisClass.equals(clazz);
|
|
|
|
* }
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
2009-10-07 11:23:14 +02:00
|
|
|
* public boolean shouldSkipField(FieldAttributes f) {
|
|
|
|
* return excludedThisClass.equals(f.getDeclaredClass());
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ExclusionStrategy excludeStrings = new UserDefinedExclusionStrategy(String.class);
|
|
|
|
* Gson gson = new GsonBuilder()
|
|
|
|
* .setExclusionStrategies(excludeStrings)
|
|
|
|
* .create();
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
2008-09-01 05:13:32 +02:00
|
|
|
* @author Joel Leitch
|
2009-10-07 11:23:14 +02:00
|
|
|
*
|
|
|
|
* @see GsonBuilder#setExclusionStrategies(ExclusionStrategy...)
|
|
|
|
*
|
|
|
|
* @since 1.4
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
|
|
|
interface ExclusionStrategy {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param f the field object that is under test
|
2009-10-07 11:23:14 +02:00
|
|
|
* @return true if the field should be ignored; otherwise false
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2009-10-07 11:23:14 +02:00
|
|
|
public boolean shouldSkipField(FieldAttributes f);
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param clazz the class object that is under test
|
2009-10-07 11:23:14 +02:00
|
|
|
* @return true if the class should be ignored; otherwise false
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
|
|
|
public boolean shouldSkipClass(Class<?> clazz);
|
|
|
|
}
|