下面列出了com.fasterxml.jackson.annotation.JsonProperty#value ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets json names from module fields annotations.
*
* @param type The Java module type.
* @return List of json property names.
*/
private static List<ModuleProperty> getFieldAnnotations(Class type) {
List<ModuleProperty> modelProperties = new ArrayList<ModuleProperty>();
Field[] fields = type.getDeclaredFields();
for(Field field : fields) {
Annotation[] annotations = field.getDeclaredAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof JsonProperty){
JsonProperty property = (JsonProperty) annotation;
ModuleProperty moduleProperty = new ModuleProperty();
moduleProperty.name = field.getName();
moduleProperty.jsonName = property.value();
moduleProperty.type = field.getType();
moduleProperty.isNumeric = isTypeNumeric(field.getType());
modelProperties.add(moduleProperty);
}
}
}
return modelProperties;
}
private static String propertyName(Method getterMethod) {
JsonProperty jsonProperty = getterMethod.getAnnotation(JsonProperty.class);
if (jsonProperty != null
&& !jsonProperty.value().isEmpty()) {
return jsonProperty.value();
} else {
String name = getterMethod.getName();
if (name.startsWith("get")
&& name.length() > 3) {
return name.substring(3, 4).toLowerCase(Locale.ENGLISH) + name.substring(4);
} else if (name.startsWith("is")
&& name.length() > 2
&& getterMethod.getReturnType().equals(boolean.class)) {
return name.substring(2, 3).toLowerCase(Locale.ENGLISH) + name.substring(3);
} else {
return null;
}
}
}
private void addJacksonAnnotationsToConstructorParameter(Field field, AnnotatedParameter parameter, String name) {
if (field != null) {
for (Annotation a : field.getAnnotations()) {
if (a.annotationType().getName().startsWith("com.fasterxml")) {
if (a.annotationType() != JsonProperty.class) {
parameter.addOrOverride(a);
} else {
JsonProperty jp = (JsonProperty) a;
if (!jp.value().equals("")) {
name = jp.value();
}
}
}
}
}
JsonProperty jsonProperty =
ProxyAnnotation.of(JsonProperty.class, Collections.singletonMap("value", name));
parameter.addOrOverride(jsonProperty);
}
/**
* Look-up an alternative name as per the following order of priority.
* <ol>
* <li>{@link JsonProperty} annotation on the field itself</li>
* <li>{@link JsonProperty} annotation on the field's getter method</li>
* </ol>
*
* @param field field to look-up alternative property name for
* @return alternative property name (or {@code null})
*/
protected String getPropertyNameOverrideBasedOnJsonPropertyAnnotation(FieldScope field) {
JsonProperty annotation = field.getAnnotationConsideringFieldAndGetter(JsonProperty.class);
if (annotation != null) {
String nameOverride = annotation.value();
// check for invalid overrides
if (nameOverride != null && !nameOverride.isEmpty() && !nameOverride.equals(field.getDeclaredName())) {
return nameOverride;
}
}
return null;
}
@Override
public PropertyName findNameForDeserialization(Annotated annotatedEntity) {
// This logic relies on the fact that Immutables generates setters annotated with @JsonProperty.
// It thus becomes obsolete whenever we move away from Immutables and the deserialization target no longer
// carries those annotations.
JsonProperty propertyAnnotation = _findAnnotation(annotatedEntity, JsonProperty.class);
if (propertyAnnotation != null) {
String jsonFieldName = propertyAnnotation.value();
Preconditions.checkArgument(
KEBAB_CASE_PATTERN.matcher(jsonFieldName).matches(),
"Conjure grammar requires kebab-case field names: %s",
jsonFieldName);
}
if (annotatedEntity instanceof AnnotatedMethod) {
AnnotatedMethod maybeSetter = (AnnotatedMethod) annotatedEntity;
if (maybeSetter.getName().startsWith("set")) {
// As a pre-caution, require that all setters have a JsonProperty annotation.
Preconditions.checkArgument(
_findAnnotation(annotatedEntity, JsonProperty.class) != null,
"All setter ({@code set*}) deserialization targets require @JsonProperty annotations: %s",
maybeSetter.getName());
}
}
return null; // delegate to the next introspector in an AnnotationIntrospectorPair.
}
/**
* Given a field of a POJO class and JsonNode corresponds to the same POJO class,
* check field's {@link JsonProperty} has flattening dots in it if so
* flatten the nested child JsonNode corresponds to the field in the given JsonNode.
*
* @param classField the field in a POJO class
* @param jsonNode the json node corresponds to POJO class that field belongs to
*/
@SuppressWarnings("unchecked")
private static void handleFlatteningForField(Field classField, JsonNode jsonNode) {
final JsonProperty jsonProperty = classField.getAnnotation(JsonProperty.class);
if (jsonProperty != null) {
final String jsonPropValue = jsonProperty.value();
if (containsFlatteningDots(jsonPropValue)) {
JsonNode childJsonNode = findNestedNode(jsonNode, jsonPropValue);
((ObjectNode) jsonNode).put(jsonPropValue, childJsonNode);
}
}
}
public static String getPropertyName(AccessibleObject property) {
JsonProperty jsonProperty = Annotations.get(property, JsonProperty.class);
if (jsonProperty != null) {
return jsonProperty.value();
} else {
if (property instanceof Field) {
return ((Field) property).getName();
} else if (property instanceof Method) {
return ((Method) property).getName();
}
throw new ElepyConfigException("Failed to get the name from AccessibleObject");
}
}
protected String getPropertyName(Element field) {
JsonProperty annotation = field.getAnnotation(JsonProperty.class);
if (isNull(annotation) || JsonProperty.USE_DEFAULT_NAME.equals(annotation.value())) {
return field.getSimpleName().toString();
} else {
return annotation.value();
}
}
/**
* @param field
* @return the field provided in the {@link JsonProperty} as long as the provided name is not JsonProperty.USE_DEFAULT_NAME otherwise return the field simple name
*/
private String getPropertyName(Element field) {
JsonProperty annotation = field.getAnnotation(JsonProperty.class);
if (isNull(annotation) || JsonProperty.USE_DEFAULT_NAME.equals(annotation.value())) {
return field.getSimpleName().toString();
} else {
return annotation.value();
}
}
public String getParameterName() {
String parameterName = parameter.getSimpleName().toString();
JsonProperty jsonProperty = parameter.getAnnotation(JsonProperty.class);
if (nonNull(jsonProperty)) {
String value = jsonProperty.value();
parameterName = value.isEmpty() ? parameterName : value;
}
return parameterName;
}
@SuppressWarnings("deprecation")
@Override
public String findEnumValue(Enum<?> value) {
try {
JsonProperty annotation = value.getClass().getField(value.name()).getAnnotation(JsonProperty.class);
if (null == annotation || StringUtils.isEmpty(annotation.value())) {
return super.findEnumValue(value);
}
return annotation.value();
} catch (NoSuchFieldException e) {
return super.findEnumValue(value);
}
}
/**
* Given a field of a POJO class and JsonNode corresponds to the same POJO class,
* check field's {@link JsonProperty} has flattening dots in it if so
* flatten the nested child JsonNode corresponds to the field in the given JsonNode.
*
* @param classField the field in a POJO class
* @param jsonNode the json node corresponds to POJO class that field belongs to
*/
@SuppressWarnings("unchecked")
private static void handleFlatteningForField(Field classField, JsonNode jsonNode) {
final JsonProperty jsonProperty = classField.getAnnotation(JsonProperty.class);
if (jsonProperty != null) {
final String jsonPropValue = jsonProperty.value();
if (containsFlatteningDots(jsonPropValue)) {
JsonNode childJsonNode = findNestedNode(jsonNode, jsonPropValue);
((ObjectNode) jsonNode).put(jsonPropValue, childJsonNode);
}
}
}
@Override
public ObjectIdInfo findObjectIdInfo(Annotated annotated) {
Class<?> rawClass = annotated.getType().getRawClass();
for (Type<?> type : model.getTypes()) {
if (type.getClassType() == rawClass && type.getSingleKeyAttribute() != null) {
Attribute<?, ?> attribute = type.getSingleKeyAttribute();
String name = removePrefix(attribute.getPropertyName());
if (useTableNames) {
name = attribute.getName();
}
// if the name is overridden use that
Class<?> superClass = rawClass.getSuperclass();
while (superClass != Object.class && superClass != null) {
try {
Field field = superClass.getDeclaredField(attribute.getPropertyName());
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
if (jsonProperty != null) {
name = jsonProperty.value();
break;
}
} catch (NoSuchFieldException ignored) {
}
superClass = superClass.getSuperclass();
}
return new ObjectIdInfo(new PropertyName(name), rawClass,
ObjectIdGenerators.PropertyGenerator.class,
EntityStoreResolver.class);
}
}
return super.findObjectIdInfo(annotated);
}
private String fetchParameterName(Parameter parameter) {
final JsonProperty jsonAnnotation = parameter.getAnnotation(JsonProperty.class);
final ParameterName parameterNameAnnotation = parameter.getAnnotation(ParameterName.class);
if (parameterNameAnnotation != null) {
return parameterNameAnnotation.value();
}
if (jsonAnnotation != null) {
return jsonAnnotation.value();
}
return parameter.getName();
}
private String renderName(PropertyDescriptor descriptor) {
if (descriptor.getReadMethod().isAnnotationPresent(JsonProperty.class)) {
JsonProperty annotation = descriptor.getReadMethod().getAnnotation(JsonProperty.class);
if (!JsonProperty.USE_DEFAULT_NAME.equalsIgnoreCase(annotation.value())) {
return annotation.value();
}
}
return descriptor.getName();
}
private String getFieldName(AccessibleProperty property) {
JsonProperty jsonProperty = getAnnotation(property, JsonProperty.class);
if(jsonProperty != null && jsonProperty.value().length() > 0) {
return jsonProperty.value();
} else {
return property.name;
}
}
@Override
public String findImplicitPropertyName(AnnotatedMember member) {
JsonProperty property = member.getAnnotation(JsonProperty.class);
if (property == null) {
if (member instanceof AnnotatedMethod) {
AnnotatedMethod method = (AnnotatedMethod) member;
String fieldName = BeanUtil.okNameForGetter(method);
return getJacksonPropertyName(member.getDeclaringClass(), fieldName);
}
} else if (!property.value().equals("")) {
return property.value();
}
return null;
}
private String getJacksonPropertyName(Class<?> declaringClass, String fieldName) {
if (fieldName != null) {
try {
Field field = declaringClass.getDeclaredField(fieldName);
if (field != null) {
JsonProperty fieldProperty = field.getAnnotation(JsonProperty.class);
if (fieldProperty != null && !fieldProperty.value().equals("")) {
return fieldProperty.value();
}
}
} catch (NoSuchFieldException ignored) {
}
}
return null;
}
@Override
public String getFieldName(PersistentProperty<?> property) {
JsonProperty jsonPropertyAnnotation = property.findAnnotation(JsonProperty.class);
if (jsonPropertyAnnotation != null) {
return jsonPropertyAnnotation.value();
}
else {
return backingStrategy.getFieldName(property);
}
}
protected String _findExplicitName(AnnotatedElement m) {
JsonProperty ann = _find(m, JsonProperty.class);
return (ann == null) ? null : ann.value();
}