AndBase开发框架  1.6
 全部  命名空间 文件 函数 变量 枚举值 
Public 成员函数 | Private 属性 | 所有成员列表
com.google.gson.GsonBuilder类 参考

Public 成员函数

 GsonBuilder ()
 
GsonBuilder setVersion (double ignoreVersionsAfter)
 
GsonBuilder excludeFieldsWithModifiers (int...modifiers)
 
GsonBuilder generateNonExecutableJson ()
 
GsonBuilder excludeFieldsWithoutExposeAnnotation ()
 
GsonBuilder serializeNulls ()
 
GsonBuilder enableComplexMapKeySerialization ()
 
GsonBuilder disableInnerClassSerialization ()
 
GsonBuilder setLongSerializationPolicy (LongSerializationPolicy serializationPolicy)
 
GsonBuilder setFieldNamingPolicy (FieldNamingPolicy namingConvention)
 
GsonBuilder setFieldNamingStrategy (FieldNamingStrategy fieldNamingStrategy)
 
GsonBuilder setExclusionStrategies (ExclusionStrategy...strategies)
 
GsonBuilder addSerializationExclusionStrategy (ExclusionStrategy strategy)
 
GsonBuilder addDeserializationExclusionStrategy (ExclusionStrategy strategy)
 
GsonBuilder setPrettyPrinting ()
 
GsonBuilder disableHtmlEscaping ()
 
GsonBuilder setDateFormat (String pattern)
 
GsonBuilder setDateFormat (int style)
 
GsonBuilder setDateFormat (int dateStyle, int timeStyle)
 

Private 属性

Excluder excluder = Excluder.DEFAULT
 
LongSerializationPolicy longSerializationPolicy = LongSerializationPolicy.DEFAULT
 
FieldNamingStrategy fieldNamingPolicy = FieldNamingPolicy.IDENTITY
 
final Map< Type,
InstanceCreator<?> > 
instanceCreators = new HashMap<Type, InstanceCreator<?>>()
 
final List< TypeAdapterFactoryfactories = new ArrayList<TypeAdapterFactory>()
 
final List< TypeAdapterFactoryhierarchyFactories = new ArrayList<TypeAdapterFactory>()
 
boolean serializeNulls
 
String datePattern
 
int dateStyle = DateFormat.DEFAULT
 
int timeStyle = DateFormat.DEFAULT
 
boolean complexMapKeySerialization
 
boolean serializeSpecialFloatingPointValues
 
boolean escapeHtmlChars = true
 
boolean prettyPrinting
 
boolean generateNonExecutableJson
 

详细描述

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()

.

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

to construct a Gson instance:

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

NOTES:

作者
Inderjeet Singh
Joel Leitch
Jesse Wilson

构造及析构函数说明

com.google.gson.GsonBuilder.GsonBuilder ( )
inline

Creates a GsonBuilder instance that can be used to build Gson with various configuration settings. GsonBuilder follows the builder pattern, and it is typically used by first invoking various configuration methods to set desired options, and finally calling create().

成员函数说明

GsonBuilder com.google.gson.GsonBuilder.addDeserializationExclusionStrategy ( ExclusionStrategy  strategy)
inline

Configures Gson to apply the passed in exclusion strategy during deserialization. If this method is invoked numerous times with different exclusion strategy objects then the exclusion strategies that were added will be applied as a disjunction rule. This means that if one of the added exclusion strategies suggests that a field (or class) should be skipped then that field (or object) is skipped during its deserialization.

参数
strategyan exclusion strategy to apply during deserialization.
返回
a reference to this object to fulfill the "Builder" pattern
自从
1.7
GsonBuilder com.google.gson.GsonBuilder.addSerializationExclusionStrategy ( ExclusionStrategy  strategy)
inline

Configures Gson to apply the passed in exclusion strategy during serialization. If this method is invoked numerous times with different exclusion strategy objects then the exclusion strategies that were added will be applied as a disjunction rule. This means that if one of the added exclusion strategies suggests that a field (or class) should be skipped then that field (or object) is skipped during its serialization.

参数
strategyan exclusion strategy to apply during serialization.
返回
a reference to this object to fulfill the "Builder" pattern
自从
1.7
GsonBuilder com.google.gson.GsonBuilder.disableHtmlEscaping ( )
inline

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

返回
a reference to this object to fulfill the "Builder" pattern
自从
1.3
GsonBuilder com.google.gson.GsonBuilder.disableInnerClassSerialization ( )
inline

Configures Gson to exclude inner classes during serialization.

返回
a reference to this object to fulfill the "Builder" pattern
自从
1.3
GsonBuilder com.google.gson.GsonBuilder.enableComplexMapKeySerialization ( )
inline

Enabling this feature will only change the serialized form if the map key is a complex type (i.e. non-primitive) in its serialized JSON form. The default implementation of map serialization uses

toString()

on the key; however, when this is called then one of the following cases apply:

Maps as JSON objects

For this case, assume that a type adapter is registered to serialize and deserialize some

Point

class, which contains an x and y coordinate, to/from the JSON Primitive string value

"(x,y)"

. The Java map would then be serialized as a JsonObject.

Below is an example:

Gson gson = new GsonBuilder()
.register(Point.class, new MyPointTypeAdapter())
.enableComplexMapKeySerialization()
.create();
Map<Point, String> original = new LinkedHashMap<Point, String>();
original.put(new Point(5, 6), "a");
original.put(new Point(8, 8), "b");
System.out.println(gson.toJson(original, type));

The above code prints this JSON object:

{
"(5,6)": "a",
"(8,8)": "b"
}

Maps as JSON arrays

For this case, assume that a type adapter was NOT registered for some

Point

class, but rather the default Gson serialization is applied. In this case, some

new Point(2,3)

would serialize as

{"x":2,"y":5}

.

Given the assumption above, a

Map<Point, String>

will be serialize as an array of arrays (can be viewed as an entry set of pairs).

Below is an example of serializing complex types as JSON arrays:

Gson gson = new GsonBuilder()
.enableComplexMapKeySerialization()
.create();
Map<Point, String> original = new LinkedHashMap<Point, String>();
original.put(new Point(5, 6), "a");
original.put(new Point(8, 8), "b");
System.out.println(gson.toJson(original, type));
The JSON output would look as follows:

[
[
{
"x": 5,
"y": 6
},
"a"
],
[
{
"x": 8,
"y": 8
},
"b"
]
]
返回
a reference to this object to fulfill the "Builder" pattern
自从
1.7
GsonBuilder com.google.gson.GsonBuilder.excludeFieldsWithModifiers ( int...  modifiers)
inline

Configures Gson to excludes all class fields that have the specified modifiers. By default, Gson will exclude all fields marked transient or static. This method will override that behavior.

参数
modifiersthe 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.
返回
a reference to this object to fulfill the "Builder" pattern
GsonBuilder com.google.gson.GsonBuilder.excludeFieldsWithoutExposeAnnotation ( )
inline

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

返回
a reference to this object to fulfill the "Builder" pattern
GsonBuilder com.google.gson.GsonBuilder.generateNonExecutableJson ( )
inline

Makes the output JSON non-executable in Javascript by prefixing the generated JSON with some special text. This prevents attacks from third-party sites through script sourcing. See Gson Issue 42 for details.

返回
a reference to this object to fulfill the "Builder" pattern
自从
1.3
GsonBuilder com.google.gson.GsonBuilder.serializeNulls ( )
inline

Configure Gson to serialize null fields. By default, Gson omits all fields that are null during serialization.

返回
a reference to this object to fulfill the "Builder" pattern
自从
1.2
GsonBuilder com.google.gson.GsonBuilder.setDateFormat ( String  pattern)
inline

Configures Gson to serialize

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.

The date format will be used to serialize and deserialize java.util.Date, java.sql.Timestamp and java.sql.Date.

Note that this pattern must abide by the convention provided by

SimpleDateFormat

class. See the documentation in java.text.SimpleDateFormat for more information on valid date and time patterns.

参数
patternthe pattern that dates will be serialized/deserialized to/from
返回
a reference to this object to fulfill the "Builder" pattern
自从
1.2
GsonBuilder com.google.gson.GsonBuilder.setDateFormat ( int  style)
inline

Configures Gson to to serialize

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 java.text.DateFormat for more information on the valid style constants.

参数
stylethe predefined date style that date objects will be serialized/deserialized to/from
返回
a reference to this object to fulfill the "Builder" pattern
自从
1.2
GsonBuilder com.google.gson.GsonBuilder.setDateFormat ( int  dateStyle,
int  timeStyle 
)
inline

Configures Gson to to serialize

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 java.text.DateFormat for more information on the valid style constants.

参数
dateStylethe predefined date style that date objects will be serialized/deserialized to/from
timeStylethe predefined style for the time portion of the date objects
返回
a reference to this object to fulfill the "Builder" pattern
自从
1.2
GsonBuilder com.google.gson.GsonBuilder.setExclusionStrategies ( ExclusionStrategy...  strategies)
inline

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

strategies

will be applied as a disjunction rule. This means that if one of the

strategies

suggests that a field (or class) should be skipped then that field (or object) is skipped during serializaiton/deserialization.

参数
strategiesthe set of strategy object to apply during object (de)serialization.
返回
a reference to this object to fulfill the "Builder" pattern
自从
1.4
GsonBuilder com.google.gson.GsonBuilder.setFieldNamingPolicy ( FieldNamingPolicy  namingConvention)
inline

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

参数
namingConventionthe JSON field naming convention to use for serialization and deserialization.
返回
a reference to this object to fulfill the "Builder" pattern
GsonBuilder com.google.gson.GsonBuilder.setFieldNamingStrategy ( FieldNamingStrategy  fieldNamingStrategy)
inline

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

参数
fieldNamingStrategythe actual naming strategy to apply to the fields
返回
a reference to this object to fulfill the "Builder" pattern
自从
1.3
GsonBuilder com.google.gson.GsonBuilder.setLongSerializationPolicy ( LongSerializationPolicy  serializationPolicy)
inline

Configures Gson to apply a specific serialization policy for

Long

and

long

objects.

参数
serializationPolicythe particular policy to use for serializing longs.
返回
a reference to this object to fulfill the "Builder" pattern
自从
1.3
GsonBuilder com.google.gson.GsonBuilder.setPrettyPrinting ( )
inline

Configures Gson to output Json that fits in a page for pretty printing. This option only affects Json serialization.

返回
a reference to this object to fulfill the "Builder" pattern
GsonBuilder com.google.gson.GsonBuilder.setVersion ( double  ignoreVersionsAfter)
inline

Configures Gson to enable versioning support.

参数
ignoreVersionsAfterany field or type marked with a version higher than this value are ignored during serialization or deserialization.
返回
a reference to this object to fulfill the "Builder" pattern

类成员变量说明

boolean com.google.gson.GsonBuilder.complexMapKeySerialization
private
String com.google.gson.GsonBuilder.datePattern
private
int com.google.gson.GsonBuilder.dateStyle = DateFormat.DEFAULT
private
boolean com.google.gson.GsonBuilder.escapeHtmlChars = true
private
Excluder com.google.gson.GsonBuilder.excluder = Excluder.DEFAULT
private
final List<TypeAdapterFactory> com.google.gson.GsonBuilder.factories = new ArrayList<TypeAdapterFactory>()
private
FieldNamingStrategy com.google.gson.GsonBuilder.fieldNamingPolicy = FieldNamingPolicy.IDENTITY
private
boolean com.google.gson.GsonBuilder.generateNonExecutableJson
private
final List<TypeAdapterFactory> com.google.gson.GsonBuilder.hierarchyFactories = new ArrayList<TypeAdapterFactory>()
private

tree-style hierarchy factories. These come after factories for backwards compatibility.

final Map<Type, InstanceCreator<?> > com.google.gson.GsonBuilder.instanceCreators = new HashMap<Type, InstanceCreator<?>>()
private
LongSerializationPolicy com.google.gson.GsonBuilder.longSerializationPolicy = LongSerializationPolicy.DEFAULT
private
boolean com.google.gson.GsonBuilder.prettyPrinting
private
boolean com.google.gson.GsonBuilder.serializeNulls
private
boolean com.google.gson.GsonBuilder.serializeSpecialFloatingPointValues
private
int com.google.gson.GsonBuilder.timeStyle = DateFormat.DEFAULT
private

该类的文档由以下文件生成: