Search in sources :

Example 36 with JsonProperty

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);
}
Also used : ObjectSchema(com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema) JsonSchema(com.fasterxml.jackson.module.jsonSchema.JsonSchema) JsonSchemaGenerator(com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty)

Example 37 with JsonProperty

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;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) java.util(java.util) OperatorArityConstants(edu.uci.ics.texera.dataflow.plangen.OperatorArityConstants) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonSchema(com.fasterxml.jackson.module.jsonSchema.JsonSchema) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) Constructor(java.lang.reflect.Constructor) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) Utils(edu.uci.ics.texera.api.utils.Utils) JsonSchemaGenerator(com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator) Annotation(java.lang.annotation.Annotation) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) AdvancedOption(edu.uci.ics.texera.dataflow.annotation.AdvancedOption) JsonNode(com.fasterxml.jackson.databind.JsonNode) DataConstants(edu.uci.ics.texera.api.constants.DataConstants) Path(java.nio.file.Path) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Annotation(java.lang.annotation.Annotation) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass)

Example 38 with JsonProperty

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;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) java.util(java.util) OperatorArityConstants(edu.uci.ics.texera.dataflow.plangen.OperatorArityConstants) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonSchema(com.fasterxml.jackson.module.jsonSchema.JsonSchema) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) Constructor(java.lang.reflect.Constructor) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) Utils(edu.uci.ics.texera.api.utils.Utils) JsonSchemaGenerator(com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator) Annotation(java.lang.annotation.Annotation) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) AdvancedOption(edu.uci.ics.texera.dataflow.annotation.AdvancedOption) JsonNode(com.fasterxml.jackson.databind.JsonNode) DataConstants(edu.uci.ics.texera.api.constants.DataConstants) Path(java.nio.file.Path) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Annotation(java.lang.annotation.Annotation)

Example 39 with JsonProperty

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);
}
Also used : Field(java.lang.reflect.Field) PropertyName(com.fasterxml.jackson.databind.PropertyName) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ObjectIdGenerators(com.fasterxml.jackson.annotation.ObjectIdGenerators) ObjectIdInfo(com.fasterxml.jackson.databind.introspect.ObjectIdInfo)

Example 40 with JsonProperty

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;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) HashMap(java.util.HashMap) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Method(java.lang.reflect.Method) JavaType(com.fasterxml.jackson.databind.JavaType) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) JsonPOJOBuilder(com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder) Parameter(java.lang.reflect.Parameter) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)45 Field (java.lang.reflect.Field)12 ArrayList (java.util.ArrayList)10 Annotation (java.lang.annotation.Annotation)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 Method (java.lang.reflect.Method)6 Type (java.lang.reflect.Type)6 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)4 JavaType (com.fasterxml.jackson.databind.JavaType)4 AnnotatedClass (com.fasterxml.jackson.databind.introspect.AnnotatedClass)4 JsonSchema (com.fasterxml.jackson.module.jsonSchema.JsonSchema)4 JsonSchemaGenerator (com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator)4 List (java.util.List)4 JsonIdentityInfo (com.fasterxml.jackson.annotation.JsonIdentityInfo)3