java类com.google.gson.annotations.JsonAdapter的实例源码

JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:odoo-work 阅读 24 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked") // Casts guarded by conditionals.
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> fieldType, JsonAdapter annotation) {
  Class<?> value = annotation.value();
  if (TypeAdapter.class.isAssignableFrom(value)) {
        Class<TypeAdapter<?>> typeAdapter = (Class<TypeAdapter<?>>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapter)).construct();
  }
  if (TypeAdapterFactory.class.isAssignableFrom(value)) {
        Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapterFactory))
        .construct()
        .create(gson, fieldType);
  }

  throw new IllegalArgumentException(
      "@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:odoo-follow-up 阅读 21 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked") // Casts guarded by conditionals.
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> fieldType, JsonAdapter annotation) {
  Class<?> value = annotation.value();
  if (TypeAdapter.class.isAssignableFrom(value)) {
        Class<TypeAdapter<?>> typeAdapter = (Class<TypeAdapter<?>>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapter)).construct();
  }
  if (TypeAdapterFactory.class.isAssignableFrom(value)) {
        Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapterFactory))
        .construct()
        .create(gson, fieldType);
  }

  throw new IllegalArgumentException(
      "@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:MyJojoXUtils 阅读 27 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked") // Casts guarded by conditionals.
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> fieldType, JsonAdapter annotation) {
  Class<?> value = annotation.value();
  TypeAdapter<?> typeAdapter;
  if (TypeAdapter.class.isAssignableFrom(value)) {
    Class<TypeAdapter<?>> typeAdapterClass = (Class<TypeAdapter<?>>) value;
    typeAdapter = constructorConstructor.get(TypeToken.get(typeAdapterClass)).construct();
  } else if (TypeAdapterFactory.class.isAssignableFrom(value)) {
    Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
    typeAdapter = constructorConstructor.get(TypeToken.get(typeAdapterFactory))
        .construct()
        .create(gson, fieldType);
  } else {
    throw new IllegalArgumentException(
        "@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
  }
  if (typeAdapter != null) {
    typeAdapter = typeAdapter.nullSafe();
  }
  return typeAdapter;
}
WorkflowConfigMapper.java 文件源码 项目:workflowTools 阅读 18 收藏 0 点赞 0 评论 0
private void serializeConfigurableProperties(JsonObject configJsonObject, Object configInstance, JsonSerializationContext jsonSerializationContext) {
    List<Field> configurableFields = Arrays.stream(configInstance.getClass().getFields())
            .filter(field -> field.getAnnotation(ConfigurableProperty.class) != null).collect(Collectors.toList());

    configurableFields.forEach(field -> {
        String fieldName = determineNameToUseForField(field);
        Object fieldValue = ReflectionUtils.getValue(field, configInstance);
        if (fieldValue != null) {
            JsonElement fieldJsonObject;
            if (field.getAnnotation(JsonAdapter.class) != null) {
                try {
                    JsonSerializer customSerializer = (JsonSerializer) field.getAnnotation(JsonAdapter.class).value().newInstance();
                    fieldJsonObject = customSerializer.serialize(fieldValue, field.getType(), jsonSerializationContext);
                } catch (InstantiationException | IllegalAccessException e) {
                    throw new RuntimeReflectiveOperationException(e);
                }
            } else {
                fieldJsonObject = jsonSerializationContext.serialize(fieldValue);
            }
            configJsonObject.add(fieldName, fieldJsonObject);
        }
    });
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:StaticMC 阅读 28 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked") // Casts guarded by conditionals.
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> fieldType, JsonAdapter annotation) {
  Class<?> value = annotation.value();
  if (TypeAdapter.class.isAssignableFrom(value)) {
        Class<TypeAdapter<?>> typeAdapter = (Class<TypeAdapter<?>>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapter)).construct();
  }
  if (TypeAdapterFactory.class.isAssignableFrom(value)) {
        Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapterFactory))
        .construct()
        .create(gson, fieldType);
  }

  throw new IllegalArgumentException(
      "@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:odoo-work 阅读 22 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  JsonAdapter annotation = targetType.getRawType().getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
ReflectiveTypeAdapterFactory.java 文件源码 项目:odoo-work 阅读 22 收藏 0 点赞 0 评论 0
private TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
  JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
  if (annotation != null) {
    TypeAdapter<?> adapter = getTypeAdapter(constructorConstructor, gson, fieldType, annotation);
    if (adapter != null) return adapter;
  }
  return gson.getAdapter(fieldType);
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:boohee_v5.6 阅读 24 收藏 0 点赞 0 评论 0
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
    JsonAdapter annotation = (JsonAdapter) targetType.getRawType().getAnnotation(JsonAdapter
            .class);
    if (annotation == null) {
        return null;
    }
    return getTypeAdapter(this.constructorConstructor, gson, targetType, annotation);
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:boohee_v5.6 阅读 29 收藏 0 点赞 0 评论 0
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson
        gson, TypeToken<?> fieldType, JsonAdapter annotation) {
    Class<?> value = annotation.value();
    if (TypeAdapter.class.isAssignableFrom(value)) {
        return (TypeAdapter) constructorConstructor.get(TypeToken.get((Class) value))
                .construct();
    }
    if (TypeAdapterFactory.class.isAssignableFrom(value)) {
        return ((TypeAdapterFactory) constructorConstructor.get(TypeToken.get((Class) value))
                .construct()).create(gson, fieldType);
    }
    throw new IllegalArgumentException("@JsonAdapter value must be TypeAdapter or " +
            "TypeAdapterFactory reference.");
}
ReflectiveTypeAdapterFactory.java 文件源码 项目:boohee_v5.6 阅读 26 收藏 0 点赞 0 评论 0
private TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
    JsonAdapter annotation = (JsonAdapter) field.getAnnotation(JsonAdapter.class);
    if (annotation != null) {
        TypeAdapter<?> adapter = JsonAdapterAnnotationTypeAdapterFactory.getTypeAdapter(this
                .constructorConstructor, gson, fieldType, annotation);
        if (adapter != null) {
            return adapter;
        }
    }
    return gson.getAdapter((TypeToken) fieldType);
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:odoo-follow-up 阅读 23 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  JsonAdapter annotation = targetType.getRawType().getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
ReflectiveTypeAdapterFactory.java 文件源码 项目:odoo-follow-up 阅读 23 收藏 0 点赞 0 评论 0
private TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
  JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
  if (annotation != null) {
    TypeAdapter<?> adapter = getTypeAdapter(constructorConstructor, gson, fieldType, annotation);
    if (adapter != null) return adapter;
  }
  return gson.getAdapter(fieldType);
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:MyJojoXUtils 阅读 21 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  JsonAdapter annotation = targetType.getRawType().getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
ReflectiveTypeAdapterFactory.java 文件源码 项目:MyJojoXUtils 阅读 26 收藏 0 点赞 0 评论 0
TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
  JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
  if (annotation != null) {
    TypeAdapter<?> adapter = getTypeAdapter(constructorConstructor, gson, fieldType, annotation);
    if (adapter != null) return adapter;
  }
  return gson.getAdapter(fieldType);
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:SteamLib 阅读 28 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  Class<? super T> rawType = targetType.getRawType();
  JsonAdapter annotation = rawType.getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:SteamLib 阅读 21 收藏 0 点赞 0 评论 0
@SuppressWarnings({ "unchecked", "rawtypes" }) // Casts guarded by conditionals.
TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> type, JsonAdapter annotation) {
  Object instance = constructorConstructor.get(TypeToken.get(annotation.value())).construct();

  TypeAdapter<?> typeAdapter;
  if (instance instanceof TypeAdapter) {
    typeAdapter = (TypeAdapter<?>) instance;
  } else if (instance instanceof TypeAdapterFactory) {
    typeAdapter = ((TypeAdapterFactory) instance).create(gson, type);
  } else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer) {
    JsonSerializer<?> serializer = instance instanceof JsonSerializer
        ? (JsonSerializer) instance
        : null;
    JsonDeserializer<?> deserializer = instance instanceof JsonDeserializer
        ? (JsonDeserializer) instance
        : null;
    typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null);
  } else {
    throw new IllegalArgumentException(
        "@JsonAdapter value must be TypeAdapter, TypeAdapterFactory, "
            + "JsonSerializer or JsonDeserializer reference.");
  }

  if (typeAdapter != null) {
    typeAdapter = typeAdapter.nullSafe();
  }

  return typeAdapter;
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:1797-2017 阅读 33 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  Class<? super T> rawType = targetType.getRawType();
  JsonAdapter annotation = rawType.getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:1797-2017 阅读 27 收藏 0 点赞 0 评论 0
@SuppressWarnings({ "unchecked", "rawtypes" }) // Casts guarded by conditionals.
TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> type, JsonAdapter annotation) {
  Object instance = constructorConstructor.get(TypeToken.get(annotation.value())).construct();

  TypeAdapter<?> typeAdapter;
  if (instance instanceof TypeAdapter) {
    typeAdapter = (TypeAdapter<?>) instance;
  } else if (instance instanceof TypeAdapterFactory) {
    typeAdapter = ((TypeAdapterFactory) instance).create(gson, type);
  } else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer) {
    JsonSerializer<?> serializer = instance instanceof JsonSerializer
        ? (JsonSerializer) instance
        : null;
    JsonDeserializer<?> deserializer = instance instanceof JsonDeserializer
        ? (JsonDeserializer) instance
        : null;
    typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null);
  } else {
    throw new IllegalArgumentException(
        "@JsonAdapter value must be TypeAdapter, TypeAdapterFactory, "
            + "JsonSerializer or JsonDeserializer reference.");
  }

  if (typeAdapter != null && annotation.nullSafe()) {
    typeAdapter = typeAdapter.nullSafe();
  }

  return typeAdapter;
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:1797-2017 阅读 21 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  Class<? super T> rawType = targetType.getRawType();
  JsonAdapter annotation = rawType.getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:1797-2017 阅读 22 收藏 0 点赞 0 评论 0
@SuppressWarnings({ "unchecked", "rawtypes" }) // Casts guarded by conditionals.
TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> type, JsonAdapter annotation) {
  Object instance = constructorConstructor.get(TypeToken.get(annotation.value())).construct();

  TypeAdapter<?> typeAdapter;
  if (instance instanceof TypeAdapter) {
    typeAdapter = (TypeAdapter<?>) instance;
  } else if (instance instanceof TypeAdapterFactory) {
    typeAdapter = ((TypeAdapterFactory) instance).create(gson, type);
  } else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer) {
    JsonSerializer<?> serializer = instance instanceof JsonSerializer
        ? (JsonSerializer) instance
        : null;
    JsonDeserializer<?> deserializer = instance instanceof JsonDeserializer
        ? (JsonDeserializer) instance
        : null;
    typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null);
  } else {
    throw new IllegalArgumentException(
        "@JsonAdapter value must be TypeAdapter, TypeAdapterFactory, "
            + "JsonSerializer or JsonDeserializer reference.");
  }

  if (typeAdapter != null && annotation.nullSafe()) {
    typeAdapter = typeAdapter.nullSafe();
  }

  return typeAdapter;
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:urmusic-desktop 阅读 24 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  Class<? super T> rawType = targetType.getRawType();
  JsonAdapter annotation = rawType.getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:urmusic-desktop 阅读 30 收藏 0 点赞 0 评论 0
@SuppressWarnings({ "unchecked", "rawtypes" }) // Casts guarded by conditionals.
TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> type, JsonAdapter annotation) {
  Object instance = constructorConstructor.get(TypeToken.get(annotation.value())).construct();

  TypeAdapter<?> typeAdapter;
  if (instance instanceof TypeAdapter) {
    typeAdapter = (TypeAdapter<?>) instance;
  } else if (instance instanceof TypeAdapterFactory) {
    typeAdapter = ((TypeAdapterFactory) instance).create(gson, type);
  } else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer) {
    JsonSerializer<?> serializer = instance instanceof JsonSerializer
        ? (JsonSerializer) instance
        : null;
    JsonDeserializer<?> deserializer = instance instanceof JsonDeserializer
        ? (JsonDeserializer) instance
        : null;
    typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null);
  } else {
    throw new IllegalArgumentException(
        "@JsonAdapter value must be TypeAdapter, TypeAdapterFactory, "
            + "JsonSerializer or JsonDeserializer reference.");
  }

  if (typeAdapter != null && annotation.nullSafe()) {
    typeAdapter = typeAdapter.nullSafe();
  }

  return typeAdapter;
}
WorkflowConfigMapper.java 文件源码 项目:workflowTools 阅读 17 收藏 0 点赞 0 评论 0
@Override
public WorkflowConfig deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    Class typeClass = (Class) type;
    List<Field> additionalConfigFields = Arrays.stream(typeClass.getFields())
            .filter(field -> field.getAnnotation(SectionConfig.class) != null).collect(Collectors.toList());

    WorkflowConfig config = (WorkflowConfig) ReflectionUtils.newInstance(typeClass);

    List<Field> configurableFields = Arrays.stream(typeClass.getFields())
            .filter(field -> field.getAnnotation(ConfigurableProperty.class) != null).collect(Collectors.toList());

    JsonObject configJsonObject = jsonElement.getAsJsonObject();
    configurableFields.forEach(field -> {
        String fieldName = determineNameToUseForField(field);
        JsonElement fieldJsonObject = configJsonObject.get(fieldName);
        if (fieldJsonObject != null) {
            Object fieldValue;
            if (field.getAnnotation(JsonAdapter.class) != null) {
                JsonDeserializer customDeserializer = (JsonDeserializer) ReflectionUtils.newInstance(field.getAnnotation(JsonAdapter.class).value());
                fieldValue = customDeserializer.deserialize(fieldJsonObject, field.getType(), jsonDeserializationContext);
            } else {
                fieldValue = jsonDeserializationContext.deserialize(fieldJsonObject, field.getType());
            }
            setFieldValue(field, config, fieldValue);
        }
    });

    additionalConfigFields.forEach(field -> {
        Object fieldConfig = jsonDeserializationContext.deserialize(jsonElement, field.getType());
        setFieldValue(field, config, fieldConfig);
    });
    return config;
}
JsonAdapterAnnotationTypeAdapterFactory.java 文件源码 项目:StaticMC 阅读 26 收藏 0 点赞 0 评论 0
@SuppressWarnings({"rawtypes", "unchecked"})
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  JsonAdapter annotation = targetType.getRawType().getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
ReflectiveTypeAdapterFactory.java 文件源码 项目:StaticMC 阅读 24 收藏 0 点赞 0 评论 0
private TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
  JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
  if (annotation != null) {
    TypeAdapter<?> adapter = getTypeAdapter(constructorConstructor, gson, fieldType, annotation);
    if (adapter != null) return adapter;
  }
  return gson.getAdapter(fieldType);
}


问题


面经


文章

微信
公众号

扫码关注公众号