Adds getters for config fields.

This adds simple getters for certain config fields that would be helpful in custom
TypeAdapters to deal with situations like this:
https://github.com/rharter/auto-value-gson/issues/18
This commit is contained in:
Ryan Harter 2016-04-25 10:26:41 -05:00 committed by jwilson
parent 18f1795d18
commit a851569ab9
2 changed files with 76 additions and 2 deletions

View File

@ -127,6 +127,8 @@ public final class Gson {
private final List<TypeAdapterFactory> 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.<TypeAdapterFactory>emptyList());
}
Gson(final Excluder excluder, final FieldNamingStrategy fieldNamingPolicy,
Gson(final Excluder excluder, final FieldNamingStrategy fieldNamingStrategy,
final Map<Type, InstanceCreator<?>> instanceCreators, boolean serializeNulls,
boolean complexMapKeySerialization, boolean generateNonExecutableGson, boolean htmlSafe,
boolean prettyPrinting, boolean lenient, boolean serializeSpecialFloatingPointValues,
LongSerializationPolicy longSerializationPolicy,
List<TypeAdapterFactory> 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<Number> doubleAdapter(boolean serializeSpecialFloatingPointValues) {
if (serializeSpecialFloatingPointValues) {
return TypeAdapters.DOUBLE;

View File

@ -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<Type, InstanceCreator<?>>(), true, false, true, false,
true, true, false, LongSerializationPolicy.DEFAULT,
new ArrayList<TypeAdapterFactory>());
assertEquals(CUSTOM_EXCLUDER, gson.excluder());
assertEquals(CUSTOM_FIELD_NAMING_STRATEGY, gson.fieldNamingStrategy());
assertEquals(true, gson.serializeNulls());
assertEquals(false, gson.htmlSafe());
}
}