Search in sources :

Example 1 with BeanDescription

use of com.fasterxml.jackson.databind.BeanDescription in project jackson-databind by FasterXML.

the class TypeNameIdResolver method idFromClass.

protected String idFromClass(Class<?> clazz) {
    if (clazz == null) {
        return null;
    }
    Class<?> cls = _typeFactory.constructType(clazz).getRawClass();
    final String key = cls.getName();
    String name;
    synchronized (_typeToId) {
        name = _typeToId.get(key);
        if (name == null) {
            // can either throw an exception, or use default name...
            if (_config.isAnnotationProcessingEnabled()) {
                BeanDescription beanDesc = _config.introspectClassAnnotations(cls);
                name = _config.getAnnotationIntrospector().findTypeName(beanDesc.getClassInfo());
            }
            if (name == null) {
                // And if still not found, let's choose default?
                name = _defaultTypeId(cls);
            }
            _typeToId.put(key, name);
        }
    }
    return name;
}
Also used : BeanDescription(com.fasterxml.jackson.databind.BeanDescription)

Example 2 with BeanDescription

use of com.fasterxml.jackson.databind.BeanDescription in project torodb by torodb.

the class AbstractBackendSerializer method acceptJsonFormatVisitor.

@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type) throws JsonMappingException {
    if (visitor == null) {
        return;
    }
    JsonObjectFormatVisitor v = visitor.expectObjectFormat(type);
    SerializerProvider prov = visitor.getProvider();
    final SerializationConfig config = prov.getConfig();
    BeanDescription beanDesc = config.introspect(type);
    JsonSubTypes jsonSubTypes;
    if (v != null) {
        for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
            if (propDef.isExplicitlyIncluded()) {
                jsonSubTypes = propDef.getPrimaryMember().getAnnotation(JsonSubTypes.class);
                if (jsonSubTypes != null) {
                    for (JsonSubTypes.Type jsonSubType : jsonSubTypes.value()) {
                        JavaType subType = TypeFactory.defaultInstance().constructType(jsonSubType.value());
                        depositSchemaProperty(v, jsonSubType.name(), subType);
                    }
                } else {
                    depositSchemaProperty(v, propDef.getName(), propDef.getPrimaryMember().getType(beanDesc.bindingsForBeanType()));
                }
            }
        }
    }
}
Also used : JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) JavaType(com.fasterxml.jackson.databind.JavaType) JsonObjectFormatVisitor(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider)

Example 3 with BeanDescription

use of com.fasterxml.jackson.databind.BeanDescription in project swagger-core by swagger-api.

the class DefaultParameterExtension method handleAdditionalAnnotation.

/**
     * Adds additional annotation processing support 
     * 
     * @param parameters
     * @param annotation
     * @param type
     * @param typesToSkip
     */
private void handleAdditionalAnnotation(List<Parameter> parameters, Annotation annotation, final Type type, Set<Type> typesToSkip) {
    if (CLASS_BEAN_PARAM != null && CLASS_BEAN_PARAM.isAssignableFrom(annotation.getClass())) {
        // Use Jackson's logic for processing Beans
        final BeanDescription beanDesc = mapper.getSerializationConfig().introspect(constructType(type));
        final List<BeanPropertyDefinition> properties = beanDesc.findProperties();
        for (final BeanPropertyDefinition propDef : properties) {
            final AnnotatedField field = propDef.getField();
            final AnnotatedMethod setter = propDef.getSetter();
            final AnnotatedMethod getter = propDef.getGetter();
            final List<Annotation> paramAnnotations = new ArrayList<Annotation>();
            final Iterator<SwaggerExtension> extensions = SwaggerExtensions.chain();
            Type paramType = null;
            // Gather the field's details
            if (field != null) {
                paramType = field.getRawType();
                for (final Annotation fieldAnnotation : field.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            // Gather the setter's details but only the ones we need
            if (setter != null) {
                // Do not set the param class/type from the setter if the values are already identified
                if (paramType == null) {
                    paramType = setter.getRawParameterTypes() != null ? setter.getRawParameterTypes()[0] : null;
                }
                for (final Annotation fieldAnnotation : setter.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            // Gather the getter's details but only the ones we need
            if (getter != null) {
                // Do not set the param class/type from the getter if the values are already identified
                if (paramType == null) {
                    paramType = getter.getRawReturnType();
                }
                for (final Annotation fieldAnnotation : getter.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            if (paramType == null) {
                continue;
            }
            // Re-process all Bean fields and let the default swagger-jaxrs/swagger-jersey-jaxrs processors do their thing
            List<Parameter> extracted = extensions.next().extractParameters(paramAnnotations, paramType, typesToSkip, extensions);
            // since downstream processors won't know how to introspect @BeanParam, process here
            for (Parameter param : extracted) {
                if (ParameterProcessor.applyAnnotations(null, param, paramType, paramAnnotations) != null) {
                    parameters.add(param);
                }
            }
        }
    }
}
Also used : AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) Type(java.lang.reflect.Type) AbstractSwaggerExtension(io.swagger.jaxrs.ext.AbstractSwaggerExtension) SwaggerExtension(io.swagger.jaxrs.ext.SwaggerExtension) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) HeaderParameter(io.swagger.models.parameters.HeaderParameter) CookieParameter(io.swagger.models.parameters.CookieParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField)

Example 4 with BeanDescription

use of com.fasterxml.jackson.databind.BeanDescription in project Gaffer by gchq.

the class GraphConfigurationService method getSerialisedFields.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override
public Set<String> getSerialisedFields(final String className) {
    final Class<?> clazz;
    try {
        clazz = Class.forName(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 List<BeanPropertyDefinition> properties = introspection.findProperties();
    final Set<String> fields = new HashSet<>();
    for (final BeanPropertyDefinition property : properties) {
        fields.add(property.getName());
    }
    return fields;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 5 with BeanDescription

use of com.fasterxml.jackson.databind.BeanDescription in project autorest-clientruntime-for-java by Azure.

the class FlatteningDeserializer method getModule.

/**
 * Gets a module wrapping this serializer as an adapter for the Jackson
 * ObjectMapper.
 *
 * @param mapper the object mapper for default deserializations
 * @return a simple module to be plugged onto Jackson ObjectMapper.
 */
public static SimpleModule getModule(final ObjectMapper mapper) {
    SimpleModule module = new SimpleModule();
    module.setDeserializerModifier(new BeanDeserializerModifier() {

        @Override
        public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
            if (beanDesc.getBeanClass().getAnnotation(JsonFlatten.class) != null) {
                return new FlatteningDeserializer(beanDesc.getBeanClass(), deserializer, mapper);
            }
            return deserializer;
        }
    });
    return module;
}
Also used : BeanDeserializerModifier(com.fasterxml.jackson.databind.deser.BeanDeserializerModifier) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) DeserializationConfig(com.fasterxml.jackson.databind.DeserializationConfig) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Aggregations

BeanDescription (com.fasterxml.jackson.databind.BeanDescription)40 JavaType (com.fasterxml.jackson.databind.JavaType)22 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)19 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)10 ArrayList (java.util.ArrayList)9 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)8 Annotation (java.lang.annotation.Annotation)8 DeserializationConfig (com.fasterxml.jackson.databind.DeserializationConfig)7 HashSet (java.util.HashSet)7 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)6 Type (java.lang.reflect.Type)6 LinkedHashMap (java.util.LinkedHashMap)6 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)5 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)5 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)4 BeanDeserializerModifier (com.fasterxml.jackson.databind.deser.BeanDeserializerModifier)4 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)3 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)3 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)3