|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.gson.GsonBuilder
public final class GsonBuilder
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 is an example shows how to use the GsonBuilder
to construct a Gson
instance:
Gson gson = new GsonBuilder() .registerTypeAdapter(Id.class, new IdTypeAdapter()) .serializeNulls() .setDateFormat(DateFormat.LONG) .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setPrettyPrinting() .setVersion(1.0) .create();
NOTE: the order of invocation of configuration methods does not matter.
Constructor Summary | |
---|---|
GsonBuilder()
Creates a GsonBuilder instance that can be used to build Gson with various configuration settings. |
Method Summary | |
---|---|
Gson |
create()
Creates a Gson instance based on the current configuration. |
GsonBuilder |
excludeFieldsWithModifiers(int... modifiers)
Configures Gson to excludes all class fields that have the specified modifiers. |
GsonBuilder |
excludeFieldsWithoutExposeAnnotation()
Configures Gson to exclude all fields from consideration for serialization or deserialization that do not have the Expose annotation. |
GsonBuilder |
registerTypeAdapter(Type type,
Object typeAdapter)
Configures Gson for custom serialization or deserialization. |
GsonBuilder |
serializeNulls()
Configure Gson to serialize null fields. |
GsonBuilder |
setDateFormat(int style)
Configures Gson to to serialize Date objects according to the style value provided. |
GsonBuilder |
setDateFormat(int dateStyle,
int timeStyle)
Configures Gson to to serialize Date objects according to the style value provided. |
GsonBuilder |
setDateFormat(String pattern)
Configures Gson to serialize Date objects according to the pattern provided. |
GsonBuilder |
setFieldNamingPolicy(FieldNamingPolicy namingConvention)
Configures Gson to apply a specific naming policy to an object's field during serialization and deserialization. |
GsonBuilder |
setPrettyPrinting()
Configures Gson to output Json that fits in a page for pretty printing. |
GsonBuilder |
setVersion(double ignoreVersionsAfter)
Configures Gson to enable versioning support. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public GsonBuilder()
create()
.
Method Detail |
---|
public GsonBuilder setVersion(double ignoreVersionsAfter)
ignoreVersionsAfter
- any field or type marked with a version higher than this value
are ignored during serialization or deserialization.
GsonBuilder
object to fulfill the "Builder" patternpublic GsonBuilder excludeFieldsWithModifiers(int... modifiers)
modifiers
- the field modifiers. You must use the modifiers specified in the
Modifier
class. For example,
Modifier.TRANSIENT
,
Modifier.STATIC
.
GsonBuilder
object to fulfill the "Builder" patternpublic GsonBuilder excludeFieldsWithoutExposeAnnotation()
Expose
annotation.
GsonBuilder
object to fulfill the "Builder" patternpublic GsonBuilder serializeNulls()
GsonBuilder
object to fulfill the "Builder" patternpublic GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention)
namingConvention
- the JSON field naming convention to use for serialization and
deserialization.
GsonBuilder
object to fulfill the "Builder" patternpublic GsonBuilder setPrettyPrinting()
GsonBuilder
object to fulfill the "Builder" patternpublic GsonBuilder setDateFormat(String pattern)
Date
objects according to the pattern provided. You can
call this method or setDateFormat(int)
multiple times, but only the last invocation
will be used to decide the serialization format.
Note that this pattern must abide by the convention provided by SimpleDateFormat
class. See the documentation in SimpleDateFormat
for more information on
valid date and time patterns.
pattern
- the pattern that dates will be serialized/deserialized to/from
GsonBuilder
object to fulfill the "Builder" patternpublic GsonBuilder setDateFormat(int style)
Date
objects according to the style value provided.
You can call this method or setDateFormat(String)
multiple times, but only the last
invocation will be used to decide the serialization format.
Note that this style value should be one of the predefined constants in the
DateFormat
class. See the documentation in DateFormat
for more
information on the valid style constants.
style
- the predefined date style that date objects will be serialized/deserialized
to/from
GsonBuilder
object to fulfill the "Builder" patternpublic GsonBuilder setDateFormat(int dateStyle, int timeStyle)
Date
objects according to the style value provided.
You can call this method or setDateFormat(String)
multiple times, but only the last
invocation will be used to decide the serialization format.
Note that this style value should be one of the predefined constants in the
DateFormat
class. See the documentation in DateFormat
for more
information on the valid style constants.
dateStyle
- the predefined date style that date objects will be serialized/deserialized
to/fromtimeStyle
- the predefined style for the time portion of the date objects
GsonBuilder
object to fulfill the "Builder" patternpublic GsonBuilder registerTypeAdapter(Type type, Object typeAdapter)
InstanceCreator
, JsonSerializer
, and a
JsonDeserializer
. It is best used when a single object typeAdapter
implements
all the required interfaces for custom serialization with Gson. If an instance creator,
serializer or deserializer was previously registered for the specified type
, it is
overwritten.
type
- the type definition for the type adapter being registeredtypeAdapter
- This object must implement at least one of the InstanceCreator
,
JsonSerializer
, and a JsonDeserializer
interfaces.
GsonBuilder
object to fulfill the "Builder" patternpublic Gson create()
Gson
instance based on the current configuration. This method is free of
side-effects to this GsonBuilder
instance and hence can be called multiple times.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |