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:
Date and its subclasses in Gson does not contain
time-zone information. So, if you are using date/time instances, use GsonBuilder
and its setDateFormat methods.
Strictness is set; some of the Gson methods behave
as if Strictness#LEGACY_STRICT was used whereas others behave as if Strictness#LENIENT was used. Prefer explicitly setting a strictness with setStrictness(Strictness) to avoid this legacy behavior.
| Modifier and Type | Field 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 |
| 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 | |
| private static final boolean | |
| pack-priv boolean | |
| pack-priv Excluder | |
| pack-priv final List | |
| pack-priv FieldNamingStrategy | |
| pack-priv FormattingStyle | |
| pack-priv boolean | |
| pack-priv final List | hierarchyFactories
tree-style hierarchy factories. |
| pack-priv final Map | |
| pack-priv LongSerializationPolicy | |
| pack-priv ToNumberStrategy | |
| pack-priv ToNumberStrategy | |
| pack-priv final ArrayDeque | |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv Strictness | |
| pack-priv int | |
| pack-priv boolean |
| Access | Constructor 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. |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternan exclusion strategy to apply during deserialization. strategy)Configures Gson to apply the passed in exclusion strategy during deserialization. |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternfilter to add filter)Adds a reflection access filter. |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternan 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 | |
| pack-priv List | createFactories(ConstructorConstructor constructorConstructor, JsonAdapterAnnotationTypeAdapterFactory jsonAdapterFactory)
|
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternBy 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 thisGsonBuilder object to fulfill the "Builder" patternConfigures Gson to exclude inner classes (= non- |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternDisables usage of JDK's |
| private TypeAdapter | |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternConfigures Gson to serialize |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe field modifiers. You must use the modifiers specified in the modifiers)java. class. For example, java., java..Configures Gson to excludes all class fields that have the specified modifiers. |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternConfigures Gson to exclude all fields from consideration for serialization and deserialization
that do not have the |
| private TypeAdapter | |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternMakes 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 | |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe type definition for the type adapter being registered type, Object This object must implement at least one of the typeAdapter)TypeAdapter, InstanceCreator, JsonSerializer, and a JsonDeserializer interfaces.Configures Gson for custom serialization or deserialization. |
| public GsonBuilder | |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe 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)TypeAdapter, JsonSerializer or JsonDeserializer interfaces.Configures Gson for custom serialization or deserialization for an inheritance type hierarchy. |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternConfigures Gson to serialize null fields. |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternSection 6 of JSON specification disallows special double values (NaN, Infinity, -Infinity). |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe pattern that dates will be serialized/deserialized to/from; can be pattern)null to reset the patternConfigures Gson to serialize |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe 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 |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe 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 |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe 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 thisGsonBuilder object to fulfill the "Builder" patternthe 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 thisGsonBuilder object to fulfill the "Builder" patternthe 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 thisGsonBuilder object to fulfill the "Builder" pattern.
Deprecated
This method is equivalent to calling
setStrictness(Strictness) with Strictness#LENIENT: setStrictness(Strictness.LENIENT)
Sets the strictness of this builder to |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe particular policy to use for serializing longs. serializationPolicy)Configures Gson to apply a specific serialization policy for |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe actual number-to-number strategy numberToNumberStrategy)Configures Gson to apply a specific number strategy during deserialization of |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe actual object-to-number strategy objectToNumberStrategy)Configures Gson to apply a specific number strategy during deserialization of |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternConfigures Gson to output JSON that fits in a page for pretty printing. |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" pattern.the new strictness mode. May not be strictness)null.Sets the strictness of this builder to the provided parameter. |
| public GsonBuilder | Returns: a reference to thisGsonBuilder object to fulfill the "Builder" patternthe version number to use. version)Configures Gson to enable versioning support. |