Incorporated comments from r710

This commit is contained in:
Inderjeet Singh 2011-04-04 22:48:34 +00:00
parent 56b7ab1b71
commit 542a17c3bc
3 changed files with 35 additions and 55 deletions

View File

@ -353,26 +353,37 @@ public final class GsonBuilder {
} }
/** /**
* Configures Gson to apply a set of exclusion strategies during either serialization or * Configures Gson to apply a set of exclusion strategies during either serialization.
* deserialization. Each of the {@code strategies} will be applied as a disjunction rule. * Each of the {@code strategies} will be applied as a disjunction rule.
* This means that if one of the {@code strategies} suggests that a field (or class) should be * This means that if one of the {@code strategies} suggests that a field (or class) should be
* skipped then that field (or object) is skipped during either serialization or deserialization * skipped then that field (or object) is skipped during either serialization
* depending on the {@code mode} that is passed into this method. * depending on the {@code mode} that is passed into this method.
* *
* @param strategies the set of strategy object to apply during the {@code mode}. * @param strategies the set of strategy object to apply during the {@code mode}.
* @param mode the mode of Gson (either serialization or deserialization) as to when the
* {@code strategies} should be applied.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7 * @since 1.7
*/ */
public GsonBuilder setExclusionStrategies(Mode mode, ExclusionStrategy... strategies) { public GsonBuilder addSerializationExclusionStrategies(ExclusionStrategy... strategies) {
Set<ExclusionStrategy> strategySet = serializeExclusionStrategies.addAll(Arrays.asList(strategies));
($Preconditions.checkNotNull(mode) == Mode.SERIALIZE)
? serializeExclusionStrategies : deserializeExclusionStrategies;
strategySet.addAll(Arrays.asList(strategies));
return this; return this;
} }
/**
* Configures Gson to apply a set of exclusion strategies during deserialization.
* Each of the {@code strategies} will be applied as a disjunction rule.
* This means that if one of the {@code strategies} suggests that a field (or class) should be
* skipped then that field (or object) is skipped during deserialization
* depending on the {@code mode} that is passed into this method.
*
* @param strategies the set of strategy object to apply during the {@code mode}.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7
*/
public GsonBuilder addDeserializationExclusionStrategies(ExclusionStrategy... strategies) {
deserializeExclusionStrategies.addAll(Arrays.asList(strategies));
return this;
}
/** /**
* Configures Gson to output Json that fits in a page for pretty printing. This option only * Configures Gson to output Json that fits in a page for pretty printing. This option only
* affects Json serialization. * affects Json serialization.

View File

@ -1,30 +0,0 @@
/*
* Copyright (C) 2010 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;
/**
* Defines the current context of Gson so that common code for serializing and deserializing can
* distinguish between the two modes.
*
* @author Joel Leitch
*
* @since 1.7
*/
public enum Mode {
SERIALIZE,
DESERIALIZE;
}

View File

@ -16,20 +16,19 @@
package com.google.gson.functional; package com.google.gson.functional;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import junit.framework.TestCase;
import com.google.gson.ExclusionStrategy; import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes; import com.google.gson.FieldAttributes;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import com.google.gson.Mode;
import junit.framework.TestCase;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* Performs some functional tests when Gson is instantiated with some common user defined * Performs some functional tests when Gson is instantiated with some common user defined
@ -48,7 +47,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
} }
public void testExclusionStrategySerialization() throws Exception { public void testExclusionStrategySerialization() throws Exception {
Gson gson = createGson(new MyExclusionStrategy(String.class), null); Gson gson = createGson(new MyExclusionStrategy(String.class), true);
String json = gson.toJson(src); String json = gson.toJson(src);
assertFalse(json.contains("\"stringField\"")); assertFalse(json.contains("\"stringField\""));
assertFalse(json.contains("\"annotatedField\"")); assertFalse(json.contains("\"annotatedField\""));
@ -56,7 +55,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
} }
public void testExclusionStrategyDeserialization() throws Exception { public void testExclusionStrategyDeserialization() throws Exception {
Gson gson = createGson(new MyExclusionStrategy(String.class), null); Gson gson = createGson(new MyExclusionStrategy(String.class), false);
JsonObject json = new JsonObject(); JsonObject json = new JsonObject();
json.add("annotatedField", new JsonPrimitive(src.annotatedField + 5)); json.add("annotatedField", new JsonPrimitive(src.annotatedField + 5));
json.add("stringField", new JsonPrimitive(src.stringField + "blah,blah")); json.add("stringField", new JsonPrimitive(src.stringField + "blah,blah"));
@ -75,7 +74,7 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
src.annotatedField + 5, src.stringField + "blah,blah", src.annotatedField + 5, src.stringField + "blah,blah",
src.longField + 655L); src.longField + 655L);
Gson gson = createGson(new MyExclusionStrategy(String.class), Mode.DESERIALIZE); Gson gson = createGson(new MyExclusionStrategy(String.class), false);
JsonObject json = gson.toJsonTree(testObj).getAsJsonObject(); JsonObject json = gson.toJsonTree(testObj).getAsJsonObject();
assertEquals(testObj.annotatedField, json.get("annotatedField").getAsInt()); assertEquals(testObj.annotatedField, json.get("annotatedField").getAsInt());
assertEquals(testObj.stringField, json.get("stringField").getAsString()); assertEquals(testObj.stringField, json.get("stringField").getAsString());
@ -89,12 +88,12 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
assertEquals(src.stringField, target.stringField); assertEquals(src.stringField, target.stringField);
} }
private static Gson createGson(ExclusionStrategy exclusionStrategy, Mode mode) { private static Gson createGson(ExclusionStrategy exclusionStrategy, boolean serialization) {
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
if (mode == null) { if (serialization) {
gsonBuilder.setExclusionStrategies(exclusionStrategy); gsonBuilder.addSerializationExclusionStrategies(exclusionStrategy);
} else { } else {
gsonBuilder.setExclusionStrategies(mode, exclusionStrategy); gsonBuilder.addDeserializationExclusionStrategies(exclusionStrategy);
} }
return gsonBuilder return gsonBuilder
.serializeNulls() .serializeNulls()