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;
|
|
|
|
|
|
|
|
/**
|
2023-11-06 20:59:01 +01:00
|
|
|
* A strategy (or policy) definition that is used to decide whether or not a field or class should
|
|
|
|
* be serialized or deserialized as part of the JSON output/input.
|
2009-10-07 11:23:14 +02:00
|
|
|
*
|
2009-10-08 22:51:40 +02:00
|
|
|
* <p>The following are a few examples that shows how you can use this exclusion mechanism.
|
2009-10-07 11:23:14 +02:00
|
|
|
*
|
2009-10-09 17:26:34 +02:00
|
|
|
* <p><strong>Exclude fields and objects based on a particular class type:</strong>
|
2023-11-06 20:59:01 +01:00
|
|
|
*
|
2009-10-08 22:51:40 +02:00
|
|
|
* <pre class="code">
|
|
|
|
* private static class SpecificClassExclusionStrategy implements ExclusionStrategy {
|
2009-10-07 11:23:14 +02:00
|
|
|
* private final Class<?> excludedThisClass;
|
|
|
|
*
|
2009-10-09 17:26:34 +02:00
|
|
|
* public SpecificClassExclusionStrategy(Class<?> excludedThisClass) {
|
2009-10-07 11:23:14 +02:00
|
|
|
* 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());
|
|
|
|
* }
|
|
|
|
* }
|
2009-10-08 22:51:40 +02:00
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <p><strong>Excludes fields and objects based on a particular annotation:</strong>
|
2023-11-06 20:59:01 +01:00
|
|
|
*
|
2009-10-08 22:51:40 +02:00
|
|
|
* <pre class="code">
|
2020-02-17 23:46:15 +01:00
|
|
|
* public @interface FooAnnotation {
|
2009-10-08 22:51:40 +02:00
|
|
|
* // some implementation here
|
|
|
|
* }
|
|
|
|
*
|
2020-02-17 23:46:15 +01:00
|
|
|
* // Excludes any field (or class) that is tagged with an "@FooAnnotation"
|
2009-10-08 22:51:40 +02:00
|
|
|
* 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;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </pre>
|
2009-10-07 11:23:14 +02:00
|
|
|
*
|
2023-11-06 20:59:01 +01:00
|
|
|
* <p>Now if you want to configure {@code Gson} to use a user defined exclusion strategy, then the
|
|
|
|
* {@code GsonBuilder} is required. The following is an example of how you can use the {@code
|
|
|
|
* GsonBuilder} to configure Gson to use one of the above samples:
|
|
|
|
*
|
2009-10-09 17:26:34 +02:00
|
|
|
* <pre class="code">
|
2009-10-07 11:23:14 +02:00
|
|
|
* ExclusionStrategy excludeStrings = new UserDefinedExclusionStrategy(String.class);
|
|
|
|
* Gson gson = new GsonBuilder()
|
|
|
|
* .setExclusionStrategies(excludeStrings)
|
|
|
|
* .create();
|
|
|
|
* </pre>
|
2011-04-05 01:16:44 +02:00
|
|
|
*
|
2011-02-04 04:09:41 +01:00
|
|
|
* <p>For certain model classes, you may only want to serialize a field, but exclude it for
|
2023-11-06 20:59:01 +01:00
|
|
|
* deserialization. To do that, you can write an {@code ExclusionStrategy} as per normal; however,
|
|
|
|
* you would register it with the {@link
|
|
|
|
* GsonBuilder#addDeserializationExclusionStrategy(ExclusionStrategy)} method. For example:
|
|
|
|
*
|
2011-04-05 01:16:44 +02:00
|
|
|
* <pre class="code">
|
|
|
|
* ExclusionStrategy excludeStrings = new UserDefinedExclusionStrategy(String.class);
|
|
|
|
* Gson gson = new GsonBuilder()
|
2011-04-13 00:03:15 +02:00
|
|
|
* .addDeserializationExclusionStrategy(excludeStrings)
|
2011-04-05 01:16:44 +02:00
|
|
|
* .create();
|
|
|
|
* </pre>
|
2009-10-07 11:23:14 +02:00
|
|
|
*
|
|
|
|
* @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...)
|
2011-04-13 00:03:15 +02:00
|
|
|
* @see GsonBuilder#addDeserializationExclusionStrategy(ExclusionStrategy)
|
|
|
|
* @see GsonBuilder#addSerializationExclusionStrategy(ExclusionStrategy)
|
2009-10-07 11:23:14 +02:00
|
|
|
* @since 1.4
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2009-10-08 23:52:56 +02:00
|
|
|
public interface ExclusionStrategy {
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
/**
|
2023-02-17 18:58:16 +01:00
|
|
|
* Decides if a field should be skipped during serialization or deserialization.
|
|
|
|
*
|
2008-09-01 05:13:32 +02:00
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
2023-02-17 18:58:16 +01:00
|
|
|
* Decides if a class should be serialized or deserialized
|
|
|
|
*
|
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);
|
|
|
|
}
|