Private 属性 | |
| Excluder | excluder = Excluder.DEFAULT |
| LongSerializationPolicy | longSerializationPolicy = LongSerializationPolicy.DEFAULT |
| FieldNamingStrategy | fieldNamingPolicy = FieldNamingPolicy.IDENTITY |
| final Map< Type, InstanceCreator<?> > | instanceCreators = new HashMap<Type, InstanceCreator<?>>() |
| final List< TypeAdapterFactory > | factories = new ArrayList<TypeAdapterFactory>() |
| final List< TypeAdapterFactory > | hierarchyFactories = 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
.
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:
|
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().
|
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.
| strategy | an exclusion strategy to apply during deserialization. |
|
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.
| strategy | an exclusion strategy to apply during serialization. |
|
inline |
|
inline |
Configures Gson to exclude inner classes during serialization.
|
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
on the key; however, when this is called then one of the following cases apply:
For this case, assume that a type adapter is registered to serialize and deserialize some
class, which contains an x and y coordinate, to/from the JSON Primitive string value
. 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"}
For this case, assume that a type adapter was NOT registered for some
class, but rather the default Gson serialization is applied. In this case, some
would serialize as
.
Given the assumption above, a
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"]]
|
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.
| modifiers | 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. |
|
inline |
Configures Gson to exclude all fields from consideration for serialization or deserialization that do not have the com.google.gson.annotations.Expose annotation.
|
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.
|
inline |
|
inline |
Configures Gson to serialize
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
class. See the documentation in java.text.SimpleDateFormat for more information on valid date and time patterns.
| pattern | the pattern that dates will be serialized/deserialized to/from |
|
inline |
Configures Gson to to serialize
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
class. See the documentation in java.text.DateFormat for more information on the valid style constants.
| style | the predefined date style that date objects will be serialized/deserialized to/from |
|
inline |
Configures Gson to to serialize
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
class. See the documentation in java.text.DateFormat for more information on the valid style constants.
| dateStyle | the predefined date style that date objects will be serialized/deserialized to/from |
| timeStyle | the predefined style for the time portion of the date objects |
|
inline |
Configures Gson to apply a set of exclusion strategies during both serialization and deserialization. Each of the
will be applied as a disjunction rule. This means that if one of the
suggests that a field (or class) should be skipped then that field (or object) is skipped during serializaiton/deserialization.
| strategies | the set of strategy object to apply during object (de)serialization. |
|
inline |
Configures Gson to apply a specific naming policy to an object's field during serialization and deserialization.
| namingConvention | the JSON field naming convention to use for serialization and deserialization. |
|
inline |
Configures Gson to apply a specific naming policy strategy to an object's field during serialization and deserialization.
| fieldNamingStrategy | the actual naming strategy to apply to the fields |
|
inline |
Configures Gson to apply a specific serialization policy for
and
objects.
| serializationPolicy | the particular policy to use for serializing longs. |
|
inline |
Configures Gson to output Json that fits in a page for pretty printing. This option only affects Json serialization.
|
inline |
Configures Gson to enable versioning support.
| ignoreVersionsAfter | any field or type marked with a version higher than this value are ignored during serialization or deserialization. |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
tree-style hierarchy factories. These come after factories for backwards compatibility.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
1.8.8