示例#1
0
  private void addTypeAdaptersForDate(
      String datePattern, int dateStyle, int timeStyle, List<TypeAdapterFactory> factories) {
    DefaultDateTypeAdapter dateTypeAdapter;
    if (datePattern != null && !"".equals(datePattern.trim())) {
      dateTypeAdapter = new DefaultDateTypeAdapter(datePattern);
    } else if (dateStyle != DateFormat.DEFAULT && timeStyle != DateFormat.DEFAULT) {
      dateTypeAdapter = new DefaultDateTypeAdapter(dateStyle, timeStyle);
    } else {
      return;
    }

    factories.add(TreeTypeAdapter.newFactory(TypeToken.get(Date.class), dateTypeAdapter));
    factories.add(TreeTypeAdapter.newFactory(TypeToken.get(Timestamp.class), dateTypeAdapter));
    factories.add(TreeTypeAdapter.newFactory(TypeToken.get(java.sql.Date.class), dateTypeAdapter));
  }
示例#2
0
 /**
  * Configures Gson for custom serialization or deserialization for an inheritance type hierarchy.
  * This method combines the registration of a {@link TypeAdapter}, {@link JsonSerializer} and a
  * {@link JsonDeserializer}. If a type adapter was previously registered for the specified type
  * hierarchy, it is overridden. If a type adapter is registered for a specific type in the type
  * hierarchy, it will be invoked instead of the one registered for the type hierarchy.
  *
  * @param baseType the class definition for the type adapter being registered for the base class
  *     or interface
  * @param typeAdapter This object must implement at least one of {@link TypeAdapter}, {@link
  *     JsonSerializer} or {@link JsonDeserializer} interfaces.
  * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
  * @since 1.7
  */
 @SuppressWarnings({"unchecked", "rawtypes"})
 public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
   $Gson$Preconditions.checkArgument(
       typeAdapter instanceof JsonSerializer<?>
           || typeAdapter instanceof JsonDeserializer<?>
           || typeAdapter instanceof TypeAdapter<?>);
   if (typeAdapter instanceof JsonDeserializer || typeAdapter instanceof JsonSerializer) {
     hierarchyFactories.add(0, TreeTypeAdapter.newTypeHierarchyFactory(baseType, typeAdapter));
   }
   if (typeAdapter instanceof TypeAdapter<?>) {
     factories.add(TypeAdapters.newTypeHierarchyFactory(baseType, (TypeAdapter) typeAdapter));
   }
   return this;
 }
示例#3
0
 /**
  * Configures Gson for custom serialization or deserialization. This method combines the
  * registration of an {@link TypeAdapter}, {@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 a type adapter was
  * previously registered for the specified {@code type}, it is overwritten.
  *
  * <p>This registers the type specified and no other types: you must manually register related
  * types! For example, applications registering {@code boolean.class} should also register {@code
  * Boolean.class}.
  *
  * @param type the type definition for the type adapter being registered
  * @param typeAdapter This object must implement at least one of the {@link TypeAdapter}, {@link
  *     InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
  * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
  */
 @SuppressWarnings({"unchecked", "rawtypes"})
 public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
   $Gson$Preconditions.checkArgument(
       typeAdapter instanceof JsonSerializer<?>
           || typeAdapter instanceof JsonDeserializer<?>
           || typeAdapter instanceof InstanceCreator<?>
           || typeAdapter instanceof TypeAdapter<?>);
   if (typeAdapter instanceof InstanceCreator<?>) {
     instanceCreators.put(type, (InstanceCreator) typeAdapter);
   }
   if (typeAdapter instanceof JsonSerializer<?> || typeAdapter instanceof JsonDeserializer<?>) {
     TypeToken<?> typeToken = TypeToken.get(type);
     factories.add(TreeTypeAdapter.newFactoryWithMatchRawType(typeToken, typeAdapter));
   }
   if (typeAdapter instanceof TypeAdapter<?>) {
     factories.add(TypeAdapters.newFactory(TypeToken.get(type), (TypeAdapter) typeAdapter));
   }
   return this;
 }