📄 gsonbuilder.java
字号:
/* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gson;import java.lang.reflect.Type;import java.text.DateFormat;import java.util.Date;import java.util.LinkedList;import java.util.List;import com.google.gson.DefaultTypeAdapters.DefaultDateTypeAdapter;/** * <p>Use this builder to construct a {@link Gson} instance when you need to set configuration * options other than the default. For {@link Gson} with default configuration, it is simpler to * use {@code new Gson()}. {@code GsonBuilder} is best used by creating it, and then invoking its * various configuration methods, and finally calling create.</p> * * <p>The following is an example shows how to use the {@code GsonBuilder} to construct a Gson * instance: * * <pre> * Gson gson = new GsonBuilder() * .registerTypeAdapter(Id.class, new IdTypeAdapter()) * .serializeNulls() * .setDateFormat(DateFormat.LONG) * .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) * .setPrettyPrinting() * .setVersion(1.0) * .create(); * </pre></p> * * <p>NOTE: the order of invocation of configuration methods does not matter.</p> * * @author Inderjeet Singh * @author Joel Leitch */public final class GsonBuilder { private static final AnonymousAndLocalClassExclusionStrategy anonAndLocalClassExclusionStrategy = new AnonymousAndLocalClassExclusionStrategy(); private static final InnerClassExclusionStrategy innerClassExclusionStrategy = new InnerClassExclusionStrategy(); private static final ExposeAnnotationBasedExclusionStrategy exposeAnnotationExclusionStrategy = new ExposeAnnotationBasedExclusionStrategy(); private double ignoreVersionsAfter; private ModifierBasedExclusionStrategy modifierBasedExclusionStrategy; private boolean serializeInnerClasses; private boolean excludeFieldsWithoutExposeAnnotation; private LongSerializationPolicy longSerializationPolicy; private FieldNamingStrategy fieldNamingPolicy; private final ParameterizedTypeHandlerMap<InstanceCreator<?>> instanceCreators; private final ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers; private final ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers; private boolean serializeNulls; private String datePattern; private int dateStyle; private int timeStyle; private boolean serializeSpecialFloatingPointValues; private boolean escapeHtmlChars; private boolean prettyPrinting; private boolean generateNonExecutableJson; /** * 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 * {@link #create()}. */ public GsonBuilder() { // setup default values ignoreVersionsAfter = VersionConstants.IGNORE_VERSIONS; serializeInnerClasses = true; prettyPrinting = false; escapeHtmlChars = true; modifierBasedExclusionStrategy = Gson.DEFAULT_MODIFIER_BASED_EXCLUSION_STRATEGY; excludeFieldsWithoutExposeAnnotation = false; longSerializationPolicy = LongSerializationPolicy.DEFAULT; fieldNamingPolicy = Gson.DEFAULT_NAMING_POLICY; instanceCreators = new ParameterizedTypeHandlerMap<InstanceCreator<?>>(); serializers = new ParameterizedTypeHandlerMap<JsonSerializer<?>>(); deserializers = new ParameterizedTypeHandlerMap<JsonDeserializer<?>>(); serializeNulls = false; dateStyle = DateFormat.DEFAULT; timeStyle = DateFormat.DEFAULT; serializeSpecialFloatingPointValues = false; generateNonExecutableJson = false; } /** * Configures Gson to enable versioning support. * * @param ignoreVersionsAfter any field or type marked with a version higher than this value * are ignored during serialization or deserialization. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder setVersion(double ignoreVersionsAfter) { this.ignoreVersionsAfter = ignoreVersionsAfter; return this; } /** * 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. * * @param modifiers the field modifiers. You must use the modifiers specified in the * {@link java.lang.reflect.Modifier} class. For example, * {@link java.lang.reflect.Modifier#TRANSIENT}, * {@link java.lang.reflect.Modifier#STATIC}. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder excludeFieldsWithModifiers(int... modifiers) { boolean skipSynthetics = true; modifierBasedExclusionStrategy = new ModifierBasedExclusionStrategy(skipSynthetics, modifiers); return this; } /** * 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 * <a href="http://code.google.com/p/google-gson/issues/detail?id=42">Gson Issue 42</a> * for details. * * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */ public GsonBuilder generateNonExecutableJson() { this.generateNonExecutableJson = true; return this; } /** * Configures Gson to exclude all fields from consideration for serialization or deserialization * that do not have the {@link com.google.gson.annotations.Expose} annotation. * * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder excludeFieldsWithoutExposeAnnotation() { excludeFieldsWithoutExposeAnnotation = true; return this; } /** * Configure Gson to serialize null fields. By default, Gson omits all fields that are null * during serialization. * * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.2 */ public GsonBuilder serializeNulls() { this.serializeNulls = true; return this; } /** * Configures Gson to exclude inner classes during serialization. * * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */ public GsonBuilder disableInnerClassSerialization() { serializeInnerClasses = false; return this; } /** * Configures Gson to apply a specific serialization policy for {@code Long} and {@code long} * objects. * * @param serializationPolicy the particular policy to use for serializing longs. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */ public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) { this.longSerializationPolicy = serializationPolicy; return this; } /** * Configures Gson to apply a specific naming policy to an object's field during serialization * and deserialization. * * @param namingConvention the JSON field naming convention to use for serialization and * deserialization. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) { return setFieldNamingStrategy(namingConvention.getFieldNamingPolicy()); } /** * Configures Gson to apply a specific naming policy strategy to an object's field during * serialization and deserialization. * * @param fieldNamingStrategy the actual naming strategy to apply to the fields * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */ public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) { this.fieldNamingPolicy = new SerializedNameAnnotationInterceptingNamingPolicy(fieldNamingStrategy); return this; } /** * Configures Gson to output Json that fits in a page for pretty printing. This option only * affects Json serialization. * * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder setPrettyPrinting() { prettyPrinting = true; return this; } /** * By default, Gson escapes HTML characters such as < > etc. Use this option to configure * Gson to pass-through HTML characters as is. * * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -