Top Description Fields Constructors Methods
com.google.gson

public final Class GsonBuilder

extends Object
Class Inheritance
Imports
com.google.errorprone.annotations.CanIgnoreReturnValue, .InlineMe, com.google.gson.annotations.Since, .Until, com.google.gson.internal.ConstructorConstructor, .Excluder, com.google.gson.internal.bind.ArrayTypeAdapter, .CollectionTypeAdapterFactory, .DefaultDateTypeAdapter, .JsonAdapterAnnotationTypeAdapterFactory, .MapTypeAdapterFactory, .NumberTypeAdapter, .ObjectTypeAdapter, .ReflectiveTypeAdapterFactory, .TreeTypeAdapter, .TypeAdapters, com.google.gson.internal.sql.SqlTypesSupport, com.google.gson.reflect.TypeToken, com.google.gson.stream.JsonReader, .JsonWriter, java.lang.reflect.Type, java.text.DateFormat, .SimpleDateFormat, java.util.ArrayDeque, .ArrayList, .Arrays, .Collection, .Collections, .Date, .HashMap, .List, .Map, .Objects, java.util.concurrent.atomic.AtomicLong, .AtomicLongArray

Use this builder to construct a Gson instance when you need to set configuration options other than the default. For Gson with default configuration, it is simpler to use new Gson(). GsonBuilder is best used by creating it, and then invoking its various configuration methods, and finally calling create.

The following example shows how to use the GsonBuilder to construct a Gson instance:

Gson gson = new GsonBuilder()
    .registerTypeAdapter(Id.class, new IdTypeAdapter())
    .enableComplexMapKeySerialization()
    .serializeNulls()
    .setDateFormat(DateFormat.LONG, DateFormat.LONG)
    .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
    .setPrettyPrinting()
    .setVersion(1.0)
    .create();

Notes:

Authors
Inderjeet Singh, Joel Leitch, Jesse Wilson

Field Summary

Modifier and TypeField and Description
pack-priv boolean
pack-priv String
pack-priv int
pack-priv static final GsonBuilder
DEFAULT

Default instance of the builder, to be used only by the default Gson#Gson() constructor.

private static final boolean
pack-priv static final ConstructorConstructor
private static final String
private static final boolean
private static final FieldNamingStrategy
private static final FormattingStyle
pack-priv static final JsonAdapterAnnotationTypeAdapterFactory
private static final boolean
private static final ToNumberStrategy
private static final ToNumberStrategy
private static final boolean
private static final boolean
private static final Strictness
pack-priv static final List<TypeAdapterFactory>
private static final boolean
pack-priv boolean
pack-priv Excluder
pack-priv final List<TypeAdapterFactory>
pack-priv FieldNamingStrategy
pack-priv FormattingStyle
pack-priv boolean
pack-priv final List<TypeAdapterFactory>
hierarchyFactories

tree-style hierarchy factories.

pack-priv final Map<Type, InstanceCreator<?>>
pack-priv LongSerializationPolicy
pack-priv ToNumberStrategy
pack-priv ToNumberStrategy
pack-priv final ArrayDeque<ReflectionAccessFilter>
pack-priv boolean
pack-priv boolean
pack-priv Strictness
pack-priv int
pack-priv boolean

Constructor Summary

AccessConstructor and Description
public
GsonBuilder()

Creates a GsonBuilder instance that can be used to build Gson with various configuration settings.

pack-priv
GsonBuilder(Gson
the gson instance whose configuration should be applied to a new GsonBuilder.
gson
)

Constructs a GsonBuilder instance from a Gson instance.

Method Summary

Modifier and TypeMethod and Description
private void
public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
addDeserializationExclusionStrategy
(ExclusionStrategy
an exclusion strategy to apply during deserialization.
strategy
)

Configures Gson to apply the passed in exclusion strategy during deserialization.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
addReflectionAccessFilter
(ReflectionAccessFilter
filter to add
filter
)

Adds a reflection access filter.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
addSerializationExclusionStrategy
(ExclusionStrategy
an exclusion strategy to apply during serialization.
strategy
)

Configures Gson to apply the passed in exclusion strategy during serialization.

private void
private static int
public Gson

Returns:

an instance of Gson configured with the options currently set in this builder
create
()

Creates a Gson instance based on the current configuration.

pack-priv List<TypeAdapterFactory>
public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
disableHtmlEscaping
()

By default, Gson escapes HTML characters such as < > etc. Use this option to configure Gson to pass-through HTML characters as is.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
disableInnerClassSerialization
()

Configures Gson to exclude inner classes (= non-static nested classes) during serialization and deserialization.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
disableJdkUnsafe
()

Disables usage of JDK's sun.misc.Unsafe.

private TypeAdapter<Number>
public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
enableComplexMapKeySerialization
()

Configures Gson to serialize Map objects with complex keys as JSON arrays.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
excludeFieldsWithModifiers
(int...
the field modifiers. You must use the modifiers specified in the java.lang.reflect.Modifier class. For example, java.lang.reflect.Modifier#TRANSIENT, java.lang.reflect.Modifier#STATIC.
modifiers
)

Configures Gson to excludes all class fields that have the specified modifiers.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
excludeFieldsWithoutExposeAnnotation
()

Configures Gson to exclude all fields from consideration for serialization and deserialization that do not have the com.google.gson.annotations.Expose annotation.

private TypeAdapter<Number>
public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
generateNonExecutableJson
()

Makes the output JSON non-executable in Javascript by prefixing the generated JSON with some special text.

private static boolean
hasNonOverridableAdapter(Type type)

Whether the type has a built-in adapter which cannot be overridden.

pack-priv static <E> List<E>
public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
registerTypeAdapter
(Type
the type definition for the type adapter being registered
type
,
Object
This object must implement at least one of the TypeAdapter, InstanceCreator, JsonSerializer, and a JsonDeserializer interfaces.
typeAdapter
)

Configures Gson for custom serialization or deserialization.

public GsonBuilder
registerTypeAdapterFactory(TypeAdapterFactory factory)

Registers a factory for type adapters.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
registerTypeHierarchyAdapter
(Class<?>
the class definition for the type adapter being registered for the base class or interface
baseType
,
Object
This object must implement at least one of TypeAdapter, JsonSerializer or JsonDeserializer interfaces.
typeAdapter
)

Configures Gson for custom serialization or deserialization for an inheritance type hierarchy.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
serializeNulls
()

Configures Gson to serialize null fields.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
serializeSpecialFloatingPointValues
()

Section 6 of JSON specification disallows special double values (NaN, Infinity, -Infinity).

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setDateFormat
(String
the pattern that dates will be serialized/deserialized to/from; can be null to reset the pattern
pattern
)

Configures Gson to serialize Date objects according to the pattern provided.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setDateFormat
(int
the predefined date style that date objects will be serialized/deserialized to/from
dateStyle
)
Deprecated Counterintuitively, despite this method taking only a 'date style' Gson will use a format which includes both date and time, with the 'time style' being the last value set by setDateFormat(int, int).

Configures Gson to serialize Date objects according to the date style value provided.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setDateFormat
(int
the predefined date style that date objects will be serialized/deserialized to/from
dateStyle
,
int
the predefined style for the time portion of the date objects
timeStyle
)

Configures Gson to serialize Date objects according to the style value provided.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setExclusionStrategies
(ExclusionStrategy...
the set of strategy object to apply during object (de)serialization.
strategies
)

Configures Gson to apply a set of exclusion strategies during both serialization and deserialization.

public GsonBuilder
setFieldNamingPolicy(FieldNamingPolicy namingConvention)

Configures Gson to apply a specific naming policy to an object's fields during serialization and deserialization.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setFieldNamingStrategy
(FieldNamingStrategy
the naming strategy to apply to the fields
fieldNamingStrategy
)

Configures Gson to apply a specific naming strategy to an object's fields during serialization and deserialization.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setFormattingStyle
(FormattingStyle
the formatting style to use.
formattingStyle
)

Configures Gson to output JSON that uses a certain kind of formatting style (for example newline and indent).

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern.
setLenient
()
Deprecated This method is equivalent to calling setStrictness(Strictness) with Strictness#LENIENT: setStrictness(Strictness.LENIENT)

Sets the strictness of this builder to Strictness#LENIENT.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setLongSerializationPolicy
(LongSerializationPolicy
the particular policy to use for serializing longs.
serializationPolicy
)

Configures Gson to apply a specific serialization policy for Long and long objects.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setNumberToNumberStrategy
(ToNumberStrategy
the actual number-to-number strategy
numberToNumberStrategy
)

Configures Gson to apply a specific number strategy during deserialization of Number.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setObjectToNumberStrategy
(ToNumberStrategy
the actual object-to-number strategy
objectToNumberStrategy
)

Configures Gson to apply a specific number strategy during deserialization of Object.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setPrettyPrinting
()

Configures Gson to output JSON that fits in a page for pretty printing.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern.
setStrictness
(Strictness
the new strictness mode. May not be null.
strictness
)

Sets the strictness of this builder to the provided parameter.

public GsonBuilder

Returns:

a reference to this GsonBuilder object to fulfill the "Builder" pattern
setVersion
(double
the version number to use.
version
)

Configures Gson to enable versioning support.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait