Java Code Examples for com.fasterxml.jackson.annotation.JsonProperty#value()

The following examples show how to use com.fasterxml.jackson.annotation.JsonProperty#value() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JacksonLombokAnnotationIntrospector.java    From jackson-lombok with MIT License 6 votes vote down vote up
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);
}
 
Example 2
Source File: ModuleInfo.java    From SugarOnRest with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: Property.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
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;
        }
    }
}
 
Example 4
Source File: KebabCaseEnforcingAnnotationInspector.java    From conjure with Apache License 2.0 5 votes vote down vote up
@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.
}
 
Example 5
Source File: JsonPropertyPreservingFieldNamingStrategy.java    From omh-dsu-ri with Apache License 2.0 5 votes vote down vote up
@Override
public String getFieldName(PersistentProperty<?> property) {

    JsonProperty jsonPropertyAnnotation = property.findAnnotation(JsonProperty.class);

    if (jsonPropertyAnnotation != null) {
        return jsonPropertyAnnotation.value();
    }
    else {
        return backingStrategy.getFieldName(property);
    }
}
 
Example 6
Source File: JacksonLombokAnnotationIntrospector.java    From jackson-lombok with MIT License 5 votes vote down vote up
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;
}
 
Example 7
Source File: JacksonLombokAnnotationIntrospector.java    From jackson-lombok with MIT License 5 votes vote down vote up
@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;
}
 
Example 8
Source File: JsonViewSerializer.java    From json-view with GNU General Public License v3.0 5 votes vote down vote up
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;
  }
}
 
Example 9
Source File: CatnapProperty.java    From catnap with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 10
Source File: LocalisationHelper.java    From phoenicis with GNU Lesser General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 11
Source File: EntityAnnotationIntrospector.java    From requery with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 12
Source File: FlatteningDeserializer.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 13
Source File: JsonPropertyIntrospector.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 14
Source File: ParameterDeserializerBuilder.java    From domino-jackson with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: AptDeserializerBuilder.java    From domino-jackson with Apache License 2.0 5 votes vote down vote up
/**
 * @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();
    }
}
 
Example 16
Source File: AccessorsFilter.java    From domino-jackson with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 17
Source File: ReflectionUtils.java    From elepy with Apache License 2.0 5 votes vote down vote up
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");
    }
}
 
Example 18
Source File: FlatteningDeserializer.java    From botbuilder-java with MIT License 5 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 19
Source File: JacksonModule.java    From jsonschema-generator with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: AnnotationBasedIntrospector.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
protected String _findExplicitName(AnnotatedElement m) {
    JsonProperty ann = _find(m, JsonProperty.class);
    return (ann == null) ? null : ann.value();
}