]>
iEval git - unical.git/blob - ExclusionStrategy.java
08fafedfa9bf625efd575180fcc6728c401e70a6
2 * Copyright (C) 2008 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.google
.gson
;
20 * A strategy (or policy) definition that is used to decide whether or not a field or top-level
21 * class should be serialized or deserialized as part of the JSON output/input. For serialization,
22 * if the {@link #shouldSkipClass(Class)} method returns false then that class or field type
23 * will not be part of the JSON output. For deserialization, if {@link #shouldSkipClass(Class)}
24 * returns false, then it will not be set as part of the Java object structure.
26 * <p>The following are a few examples that shows how you can use this exclusion mechanism.
28 * <p><strong>Exclude fields and objects based on a particular class type:</strong>
30 * private static class SpecificClassExclusionStrategy implements ExclusionStrategy {
31 * private final Class<?> excludedThisClass;
33 * public SpecificClassExclusionStrategy(Class<?> excludedThisClass) {
34 * this.excludedThisClass = excludedThisClass;
37 * public boolean shouldSkipClass(Class<?> clazz) {
38 * return excludedThisClass.equals(clazz);
41 * public boolean shouldSkipField(FieldAttributes f) {
42 * return excludedThisClass.equals(f.getDeclaredClass());
47 * <p><strong>Excludes fields and objects based on a particular annotation:</strong>
49 * public @interface FooAnnotation {
50 * // some implementation here
53 * // Excludes any field (or class) that is tagged with an "@FooAnnotation"
54 * private static class FooAnnotationExclusionStrategy implements ExclusionStrategy {
55 * public boolean shouldSkipClass(Class<?> clazz) {
56 * return clazz.getAnnotation(FooAnnotation.class) != null;
59 * public boolean shouldSkipField(FieldAttributes f) {
60 * return f.getAnnotation(FooAnnotation.class) != null;
65 * <p>Now if you want to configure {@code Gson} to use a user defined exclusion strategy, then
66 * the {@code GsonBuilder} is required. The following is an example of how you can use the
67 * {@code GsonBuilder} to configure Gson to use one of the above sample:
69 * ExclusionStrategy excludeStrings = new UserDefinedExclusionStrategy(String.class);
70 * Gson gson = new GsonBuilder()
71 * .setExclusionStrategies(excludeStrings)
75 * <p>For certain model classes, you may only want to serialize a field, but exclude it for
76 * deserialization. To do that, you can write an {@code ExclusionStrategy} as per normal;
77 * however, you would register it with the
78 * {@link GsonBuilder#addDeserializationExclusionStrategy(ExclusionStrategy)} method.
81 * ExclusionStrategy excludeStrings = new UserDefinedExclusionStrategy(String.class);
82 * Gson gson = new GsonBuilder()
83 * .addDeserializationExclusionStrategy(excludeStrings)
87 * @author Inderjeet Singh
90 * @see GsonBuilder#setExclusionStrategies(ExclusionStrategy...)
91 * @see GsonBuilder#addDeserializationExclusionStrategy(ExclusionStrategy)
92 * @see GsonBuilder#addSerializationExclusionStrategy(ExclusionStrategy)
96 public interface ExclusionStrategy
{
99 * @param f the field object that is under test
100 * @return true if the field should be ignored; otherwise false
102 public boolean shouldSkipField(FieldAttributes f
);
105 * @param clazz the class object that is under test
106 * @return true if the class should be ignored; otherwise false
108 public boolean shouldSkipClass(Class
<?
> clazz
);
This page took 0.050944 seconds and 3 git commands to generate.