⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsonbuilder.java

📁 地图生成的一个文件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public GsonBuilder disableHtmlEscaping() {    this.escapeHtmlChars = false;    return this;  }  /**   * Configures Gson to serialize {@code Date} objects according to the pattern provided. You can   * call this method or {@link #setDateFormat(int)} multiple times, but only the last invocation   * will be used to decide the serialization format.   *   * <p>Note that this pattern must abide by the convention provided by {@code SimpleDateFormat}   * class. See the documentation in {@link java.text.SimpleDateFormat} for more information on   * valid date and time patterns.</p>   *   * @param pattern the pattern that dates will be serialized/deserialized to/from   * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern   * @since 1.2   */  public GsonBuilder setDateFormat(String pattern) {    // TODO(Joel): Make this fail fast if it is an invalid date format    this.datePattern = pattern;    return this;  }  /**   * Configures Gson to to serialize {@code Date} objects according to the style value provided.   * You can call this method or {@link #setDateFormat(String)} multiple times, but only the last   * invocation will be used to decide the serialization format.   *   * <p>Note that this style value should be one of the predefined constants in the   * {@code DateFormat} class. See the documentation in {@link java.text.DateFormat} for more   * information on the valid style constants.</p>   *   * @param style the predefined date style that date objects will be serialized/deserialized   * to/from   * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern   * @since 1.2   */  public GsonBuilder setDateFormat(int style) {    this.dateStyle = style;    this.datePattern = null;    return this;  }  /**   * Configures Gson to to serialize {@code Date} objects according to the style value provided.   * You can call this method or {@link #setDateFormat(String)} multiple times, but only the last   * invocation will be used to decide the serialization format.   *   * <p>Note that this style value should be one of the predefined constants in the   * {@code DateFormat} class. See the documentation in {@link java.text.DateFormat} for more   * information on the valid style constants.</p>   *   * @param dateStyle the predefined date style that date objects will be serialized/deserialized   * to/from   * @param timeStyle the predefined style for the time portion of the date objects   * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern   * @since 1.2   */  public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {    this.dateStyle = dateStyle;    this.timeStyle = timeStyle;    this.datePattern = null;    return this;  }  /**   * Configures Gson for custom serialization or deserialization. This method combines the   * registration of an {@link InstanceCreator}, {@link JsonSerializer}, and a   * {@link JsonDeserializer}. It is best used when a single object {@code typeAdapter} implements   * all the required interfaces for custom serialization with Gson. If an instance creator,   * serializer or deserializer was previously registered for the specified {@code type}, it is   * overwritten.   *   * @param type the type definition for the type adapter being registered   * @param typeAdapter This object must implement at least one of the {@link InstanceCreator},   * {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.   * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern   */  public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {    Preconditions.checkArgument(typeAdapter instanceof JsonSerializer        || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof InstanceCreator);    if (typeAdapter instanceof InstanceCreator) {      registerInstanceCreator(type, (InstanceCreator<?>) typeAdapter);    }    if (typeAdapter instanceof JsonSerializer) {      registerSerializer(type, (JsonSerializer<?>) typeAdapter);    }    if (typeAdapter instanceof JsonDeserializer) {      registerDeserializer(type, (JsonDeserializer<?>) typeAdapter);    }    return this;  }  /**   * Configures Gson to use a custom {@link InstanceCreator} for the specified type. If an instance   * creator was previously registered for the specified class, it is overwritten. Since this method   * takes a type instead of a Class object, it can be used to register a specific handler for a   * generic type corresponding to a raw type.   *   * @param <T> the type for which instance creator is being registered   * @param typeOfT The Type definition for T   * @param instanceCreator the instance creator for T   * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern   */  private <T> GsonBuilder registerInstanceCreator(Type typeOfT,      InstanceCreator<? extends T> instanceCreator) {    instanceCreators.register(typeOfT, instanceCreator);    return this;  }  /**   * Configures Gson to use a custom JSON serializer for the specified type. You should use this   * method if you want to register different serializers for different generic types corresponding   * to a raw type.   *   * @param <T> the type for which the serializer is being registered   * @param typeOfT The type definition for T   * @param serializer the custom serializer   * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern   */  private <T> GsonBuilder registerSerializer(Type typeOfT, final JsonSerializer<T> serializer) {    serializers.register(typeOfT, serializer);    return this;  }  /**   * Configures Gson to use a custom JSON deserializer for the specified type. You should use this   * method if you want to register different deserializers for different generic types   * corresponding to a raw type.   *   * @param <T> the type for which the deserializer is being registered   * @param typeOfT The type definition for T   * @param deserializer the custom deserializer   * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern   */  private <T> GsonBuilder registerDeserializer(Type typeOfT, JsonDeserializer<T> deserializer) {    deserializers.register(typeOfT, new JsonDeserializerExceptionWrapper<T>(deserializer));    return this;  }  /**   * Section 2.4 of <a href="http://www.ietf.org/rfc/rfc4627.txt">JSON specification</a> disallows   * special double values (NaN, Infinity, -Infinity). However,   * <a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf">Javascript   * specification</a> (see section 4.3.20, 4.3.22, 4.3.23) allows these values as valid Javascript   * values. Moreover, most JavaScript engines will accept these special values in JSON without   * problem. So, at a practical level, it makes sense to accept these values as valid JSON even   * though JSON specification disallows them.   *   * <p>Gson always accepts these special values during deserialization. However, it outputs   * strictly compliant JSON. Hence, if it encounters a float value {@link Float#NaN},   * {@link Float#POSITIVE_INFINITY}, {@link Float#NEGATIVE_INFINITY}, or a double value   * {@link Double#NaN}, {@link Double#POSITIVE_INFINITY}, {@link Double#NEGATIVE_INFINITY}, it   * will throw an {@link IllegalArgumentException}. This method provides a way to override the   * default behavior when you know that the JSON receiver will be able to handle these special   * values.   *   * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern   * @since 1.3   */  public GsonBuilder serializeSpecialFloatingPointValues() {    this.serializeSpecialFloatingPointValues = true;    return this;  }  /**   * Creates a {@link Gson} instance based on the current configuration. This method is free of   * side-effects to this {@code GsonBuilder} instance and hence can be called multiple times.   *   * @return an instance of Gson configured with the options currently set in this builder   */  public Gson create() {    List<ExclusionStrategy> strategies = new LinkedList<ExclusionStrategy>();    strategies.add(modifierBasedExclusionStrategy);    strategies.add(anonAndLocalClassExclusionStrategy);    if (!serializeInnerClasses) {      strategies.add(innerClassExclusionStrategy);    }    if (ignoreVersionsAfter != VersionConstants.IGNORE_VERSIONS) {      strategies.add(new VersionExclusionStrategy(ignoreVersionsAfter));    }    if (excludeFieldsWithoutExposeAnnotation) {      strategies.add(exposeAnnotationExclusionStrategy);    }    ExclusionStrategy exclusionStrategy = new DisjunctionExclusionStrategy(strategies);    ParameterizedTypeHandlerMap<JsonSerializer<?>> customSerializers = serializers.copyOf();    ParameterizedTypeHandlerMap<JsonDeserializer<?>> customDeserializers = deserializers.copyOf();    addTypeAdaptersForDate(datePattern, dateStyle, timeStyle, customSerializers,        customDeserializers);    customSerializers.registerIfAbsent(DefaultTypeAdapters.getDefaultSerializers(        serializeSpecialFloatingPointValues, longSerializationPolicy));    customDeserializers.registerIfAbsent(DefaultTypeAdapters.getDefaultDeserializers());    ParameterizedTypeHandlerMap<InstanceCreator<?>> customInstanceCreators =        instanceCreators.copyOf();    customInstanceCreators.registerIfAbsent(DefaultTypeAdapters.getDefaultInstanceCreators());        customSerializers.makeUnmodifiable();    customDeserializers.makeUnmodifiable();    instanceCreators.makeUnmodifiable();        MappedObjectConstructor objConstructor = new MappedObjectConstructor(customInstanceCreators);    JsonFormatter formatter =  prettyPrinting ?        new JsonPrintFormatter(escapeHtmlChars) : new JsonCompactFormatter(escapeHtmlChars);    Gson gson = new Gson(exclusionStrategy, fieldNamingPolicy, objConstructor,        formatter, serializeNulls, customSerializers, customDeserializers, generateNonExecutableJson);    return gson;  }  private static void addTypeAdaptersForDate(String datePattern, int dateStyle, int timeStyle,      ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,      ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers) {    if (!serializers.hasSpecificHandlerFor(Date.class)        && !deserializers.hasSpecificHandlerFor(Date.class)) {      // NOTE: if a date pattern exists, then that style takes priority      DefaultDateTypeAdapter dateTypeAdapter = null;      if (datePattern != null && !"".equals(datePattern.trim())) {        dateTypeAdapter = new DefaultDateTypeAdapter(datePattern);      } else if (dateStyle != DateFormat.DEFAULT && timeStyle != DateFormat.DEFAULT) {        dateTypeAdapter = new DefaultDateTypeAdapter(dateStyle, timeStyle);      }            if (dateTypeAdapter != null) {        serializers.register(Date.class, dateTypeAdapter);        deserializers.register(Date.class, dateTypeAdapter);      }    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -