use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project atlasmap by atlasmap.
the class ActionDetail method setActionSchema.
/**
* Sets the action schema from a field action model class.
* @param clazz field action model class
* @throws JsonMappingException unexpected error
*/
@JsonProperty("actionSchema")
public void setActionSchema(Class<? extends Action> clazz) throws JsonMappingException {
if (clazz == null) {
setActionSchema((ObjectSchema) null);
return;
}
setClassName(clazz.getName());
ObjectMapper mapper = new ObjectMapper().enable(MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES);
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
AtlasSchemaFactoryWrapper visitor = new AtlasSchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(clazz, visitor);
JsonSchema schema = visitor.finalSchema();
ObjectSchema objectSchema = schema.asObjectSchema();
// see: https://json-schema.org/understanding-json-schema/reference/generic.html#constant-values
String id = ActionResolver.getInstance().toId(clazz);
objectSchema.setId(id);
AtlasSchemaFactoryWrapper.ExtendedJsonSchema keyField = (AtlasSchemaFactoryWrapper.ExtendedJsonSchema) objectSchema.getProperties().get("@type");
keyField.getMetadata().put("const", id);
setActionSchema(objectSchema);
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project textdb by TextDB.
the class JsonSchemaHelper method getPropertyDefaultValues.
public static Map<String, Object> getPropertyDefaultValues(Class<? extends PredicateBase> predicateClass) {
HashMap<String, Object> defaultValueMap = new HashMap<>();
Constructor<?> constructor = getJsonCreatorConstructor(predicateClass);
// get all parameter types
Class<?>[] parameterTypes = constructor.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
// find the @JsonProperty annotation for each parameter
Annotation[] annotations = constructor.getParameterAnnotations()[i];
Optional<Annotation> findJsonProperty = Arrays.asList(annotations).stream().filter(annotation -> annotation.annotationType().equals(JsonProperty.class)).findAny();
if (!findJsonProperty.isPresent()) {
continue;
}
// convert the defaultValue from the string to the parameter's type
JsonProperty jsonProperty = (JsonProperty) findJsonProperty.get();
if (!jsonProperty.defaultValue().trim().isEmpty()) {
defaultValueMap.put(jsonProperty.value(), objectMapper.convertValue(jsonProperty.defaultValue(), parameterTypes[i]));
}
}
return defaultValueMap;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project textdb by TextDB.
the class JsonSchemaHelper method getRequiredProperties.
public static List<String> getRequiredProperties(Class<? extends PredicateBase> predicateClass) {
ArrayList<String> requiredProperties = new ArrayList<>();
Constructor<?> constructor = getJsonCreatorConstructor(predicateClass);
for (Annotation[] annotations : Arrays.asList(constructor.getParameterAnnotations())) {
// find the @JsonProperty annotation for each parameter
Optional<Annotation> findJsonProperty = Arrays.asList(annotations).stream().filter(annotation -> annotation.annotationType().equals(JsonProperty.class)).findAny();
if (!findJsonProperty.isPresent()) {
continue;
}
// add the required property to the list
JsonProperty jsonProperty = (JsonProperty) findJsonProperty.get();
if (jsonProperty.required()) {
requiredProperties.add(jsonProperty.value());
}
}
return requiredProperties;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project requery by requery.
the class EntityAnnotationIntrospector method findObjectIdInfo.
@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);
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty in project Gaffer by gchq.
the class JsonSerialisationUtil method getSerialisedFieldClasses.
/**
* Gets all the fields and their classes for a given class.
*
* @param className the class name to find the fields for.
* @return a map of field name to class name
*/
public static Map<String, String> getSerialisedFieldClasses(final String className) {
final Map<String, String> cachedResult = cache.get(className);
if (null != cachedResult) {
return cachedResult;
}
final Class<?> clazz;
try {
clazz = Class.forName(SimpleClassNameIdResolver.getClassName(className));
} catch (final Exception e) {
throw new IllegalArgumentException("Class name was not recognised: " + className, e);
}
final ObjectMapper mapper = new ObjectMapper();
final JavaType type = mapper.getTypeFactory().constructType(clazz);
final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
final Class<?> builder = introspection.findPOJOBuilder();
String buildMethodPrefix = "with";
if (null != builder) {
JsonPOJOBuilder anno = findAnnotation(builder, JsonPOJOBuilder.class);
if (null != anno) {
buildMethodPrefix = anno.withPrefix();
}
}
Constructor<?> creator = null;
for (final Constructor<?> constructor : type.getRawClass().getDeclaredConstructors()) {
final JsonCreator anno = constructor.getAnnotation(JsonCreator.class);
if (null != anno) {
creator = constructor;
break;
}
}
final List<BeanPropertyDefinition> properties = introspection.findProperties();
final Map<String, String> fieldMap = new HashMap<>();
for (final BeanPropertyDefinition property : properties) {
final String propName = property.getName();
final String propClass;
if ("class".equals(propName)) {
propClass = Class.class.getName();
} else {
Type genericType = null;
if (null != builder) {
final String methodName = buildMethodPrefix + propName;
Method matchedMethod = null;
for (final Method method : builder.getMethods()) {
if (methodName.equalsIgnoreCase(method.getName())) {
final Type[] params = method.getGenericParameterTypes();
if (null != params && 1 == params.length) {
final JsonSetter jsonSetter = method.getAnnotation(JsonSetter.class);
if (null != jsonSetter && propName.equals(jsonSetter.value())) {
matchedMethod = method;
break;
}
final JsonProperty jsonProperty = method.getAnnotation(JsonProperty.class);
if (null != jsonProperty && propName.equals(jsonProperty.value())) {
matchedMethod = method;
break;
}
if (null == matchedMethod) {
matchedMethod = method;
} else if (builder.equals(method.getReturnType())) {
// Checks for overridden methods
matchedMethod = method;
}
}
}
}
if (null != matchedMethod) {
genericType = matchedMethod.getGenericParameterTypes()[0];
}
}
if (null == genericType && null != creator) {
for (final Parameter parameter : creator.getParameters()) {
final JsonProperty anno = parameter.getAnnotation(JsonProperty.class);
if (null != anno && propName.equals(anno.value())) {
if (null != parameter.getParameterizedType()) {
genericType = parameter.getParameterizedType();
} else {
genericType = parameter.getType();
}
break;
}
}
}
if (null == genericType && null != property.getSetter() && null != property.getSetter().getGenericParameterTypes() && 1 == property.getSetter().getGenericParameterTypes().length) {
genericType = property.getSetter().getGenericParameterTypes()[0];
}
if (null != genericType && genericType instanceof Class && ((Class) genericType).isEnum()) {
genericType = String.class;
}
if (null == genericType) {
propClass = Object.class.getName();
} else {
propClass = getFieldTypeString(clazz, genericType);
}
}
fieldMap.put(propName, propClass);
}
final Map<String, Map<String, String>> newCache = new HashMap<>(cache);
newCache.put(className, Collections.unmodifiableMap(fieldMap));
cache = Collections.unmodifiableMap(newCache);
return fieldMap;
}
Aggregations