|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.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. Here is an example:
Gson gson = new GsonBuilder();
.setVersion(1.0)
.setPrettyPrinting()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.registerTypeAdapter(Id.class, new IdTypeAdapter())
.create();
| 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(java.lang.reflect.Type typeOfT,
java.lang.Object typeAdapter)
Configures Gson for custom serialization or deserialization. |
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(). Here is an example:
Gson gson = new GsonBuilder()
.setVersion(1.0)
.setPrettyPrinting()
.registerTypeAdapter(Id.class, new IdTypeAdapter())
.create();
The order of invocation of configuration methods does not matter.
| 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.
public GsonBuilder excludeFieldsWithModifiers(int... modifiers)
modifiers - the field modifiers. You must use the modifiers specified in the
Modifier class. For example,
Modifier.TRANSIENT,
Modifier.STATIC.
public GsonBuilder excludeFieldsWithoutExposeAnnotation()
Expose annotation.
public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention)
namingConvention - the JSON field naming convention to use for
serialization/deserializaiton
public GsonBuilder setPrettyPrinting()
public GsonBuilder registerTypeAdapter(java.lang.reflect.Type typeOfT,
java.lang.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 class, it is overwritten.
typeOfT - The class definition for the type T.typeAdapter - This object must implement at least one of the InstanceCreator,
JsonSerializer, and a JsonDeserializer interfaces.
public Gson create()
Gson instance based on the current configuration.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||