diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 073091ef..9fbf44a5 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -127,6 +127,8 @@ public final class Gson { private final List factories; private final ConstructorConstructor constructorConstructor; + private final Excluder excluder; + private final FieldNamingStrategy fieldNamingStrategy; private final boolean serializeNulls; private final boolean htmlSafe; private final boolean generateNonExecutableJson; @@ -175,13 +177,15 @@ public final class Gson { LongSerializationPolicy.DEFAULT, Collections.emptyList()); } - Gson(final Excluder excluder, final FieldNamingStrategy fieldNamingPolicy, + Gson(final Excluder excluder, final FieldNamingStrategy fieldNamingStrategy, final Map> instanceCreators, boolean serializeNulls, boolean complexMapKeySerialization, boolean generateNonExecutableGson, boolean htmlSafe, boolean prettyPrinting, boolean lenient, boolean serializeSpecialFloatingPointValues, LongSerializationPolicy longSerializationPolicy, List typeAdapterFactories) { this.constructorConstructor = new ConstructorConstructor(instanceCreators); + this.excluder = excluder; + this.fieldNamingStrategy = fieldNamingStrategy; this.serializeNulls = serializeNulls; this.generateNonExecutableJson = generateNonExecutableGson; this.htmlSafe = htmlSafe; @@ -244,11 +248,27 @@ public final class Gson { factories.add(new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor)); factories.add(TypeAdapters.ENUM_FACTORY); factories.add(new ReflectiveTypeAdapterFactory( - constructorConstructor, fieldNamingPolicy, excluder)); + constructorConstructor, fieldNamingStrategy, excluder)); this.factories = Collections.unmodifiableList(factories); } + public Excluder excluder() { + return excluder; + } + + public FieldNamingStrategy fieldNamingStrategy() { + return fieldNamingStrategy; + } + + public boolean serializeNulls() { + return serializeNulls; + } + + public boolean htmlSafe() { + return htmlSafe; + } + private TypeAdapter doubleAdapter(boolean serializeSpecialFloatingPointValues) { if (serializeSpecialFloatingPointValues) { return TypeAdapters.DOUBLE; diff --git a/gson/src/test/java/com/google/gson/GsonTest.java b/gson/src/test/java/com/google/gson/GsonTest.java new file mode 100644 index 00000000..fb0c0032 --- /dev/null +++ b/gson/src/test/java/com/google/gson/GsonTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2016 The Gson Authors + * + * 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; + +import com.google.gson.internal.Excluder; +import java.lang.reflect.Field; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; +import junit.framework.TestCase; + +/** + * Unit tests for {@link Gson}. + * + * @author Ryan Harter + */ +public final class GsonTest extends TestCase { + + private static final Excluder CUSTOM_EXCLUDER = Excluder.DEFAULT + .excludeFieldsWithoutExposeAnnotation() + .disableInnerClassSerialization(); + + private static final FieldNamingStrategy CUSTOM_FIELD_NAMING_STRATEGY = new FieldNamingStrategy() { + @Override public String translateName(Field f) { + return "foo"; + } + }; + + public void testOverridesDefaultExcluder() { + Gson gson = new Gson(CUSTOM_EXCLUDER, CUSTOM_FIELD_NAMING_STRATEGY, + new HashMap>(), true, false, true, false, + true, true, false, LongSerializationPolicy.DEFAULT, + new ArrayList()); + + assertEquals(CUSTOM_EXCLUDER, gson.excluder()); + assertEquals(CUSTOM_FIELD_NAMING_STRATEGY, gson.fieldNamingStrategy()); + assertEquals(true, gson.serializeNulls()); + assertEquals(false, gson.htmlSafe()); + } +}