Example usage for com.fasterxml.jackson.databind.introspect BasicBeanDescription findProperties

List of usage examples for com.fasterxml.jackson.databind.introspect BasicBeanDescription findProperties

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.introspect BasicBeanDescription findProperties.

Prototype

public List<BeanPropertyDefinition> findProperties() 

Source Link

Usage

From source file:org.jongo.marshall.jackson.JacksonObjectIdUpdater.java

public Object getId(Object pojo) {
    BasicBeanDescription beanDescription = beanDescription(pojo.getClass());
    for (BeanPropertyDefinition def : beanDescription.findProperties()) {
        if (isIdProperty(def)) {
            AnnotatedMember accessor = def.getAccessor();
            accessor.fixAccess();//from   ww w .  jav a2 s  . com
            Object id = accessor.getValue(pojo);
            if (id instanceof String && isObjectId(def)) {
                return new ObjectId(id.toString());
            } else {
                return id;
            }
        }
    }
    return null;
}

From source file:fr.norad.jaxrs.doc.parser.ModelJacksonParser.java

@Override
public List<PropertyAccessor> findProperties(Class<?> modelClass) {
    List<PropertyAccessor> properties = new ArrayList<>();

    BasicBeanDescription beanDesc = fakeSerializer.getDescription(modelClass);
    List<BeanPropertyDefinition> findProperties = beanDesc.findProperties();
    for (BeanPropertyDefinition beanPropertyDefinition : findProperties) {
        if (modelClass.isEnum() && "declaringClass".equals(beanPropertyDefinition.getName())) {
            continue;
        }/*ww  w. j  av a 2s. co  m*/
        AnnotatedMethod getterJackson = beanPropertyDefinition.getGetter();
        AnnotatedMethod setterJackson = beanPropertyDefinition.getSetter();
        AnnotatedField fieldJackson = null;
        try {
            fieldJackson = beanPropertyDefinition.getField();
        } catch (Exception e) {
            log.warning("Name conflict on fields in bean : " + beanPropertyDefinition + " during doc generation"
                    + e.getMessage());
        }

        Method getter = getterJackson == null ? null : getterJackson.getAnnotated();
        Method setter = setterJackson == null ? null : setterJackson.getAnnotated();
        Field field = fieldJackson == null ? null : fieldJackson.getAnnotated();
        if (getter == null && setter == null && field == null) {
            log.warning(
                    "Cannot find valid property element for : " + beanPropertyDefinition + " on " + modelClass);
            continue;
        }

        PropertyAccessor property = new PropertyAccessor();
        property.setField(field);
        property.setGetter(getter);
        property.setSetter(setter);
        property.setName(beanPropertyDefinition.getName());

        properties.add(property);
    }
    return properties;
}

From source file:de.fraunhofer.iosb.ilt.sta.serialize.EntitySerializer.java

@Override
public void serialize(Entity entity, JsonGenerator gen, SerializerProvider serializers)
        throws IOException, JsonProcessingException {
    gen.writeStartObject();// w ww  . j  a  v a2  s.c o  m
    try {
        BasicBeanDescription beanDescription = serializers.getConfig()
                .introspect(serializers.constructType(entity.getClass()));
        List<BeanPropertyDefinition> properties = beanDescription.findProperties();
        for (BeanPropertyDefinition property : properties) {
            // 0. check if it should be serialized
            if (selectedProperties != null) {
                if (!selectedProperties.contains(property.getName())) {
                    continue;
                }
            }
            // 1. is it a NavigableElement?
            if (NavigableElement.class.isAssignableFrom(property.getAccessor().getRawType())) {
                Object rawValue = property.getAccessor().getValue(entity);
                if (rawValue != null) {
                    NavigableElement value = (NavigableElement) rawValue;
                    // If navigation link set, output navigation link.
                    if (value.getNavigationLink() != null && !value.getNavigationLink().isEmpty()) {
                        gen.writeFieldName(property.getName() + "@iot.navigationLink");
                        gen.writeString(value.getNavigationLink());
                    }
                    // If object should not be exported, skip any further processing.
                    if (!value.isExportObject()) {
                        continue;
                    }
                }
            }
            // 2. check if property has CustomSerialization annotation -> use custom serializer
            Annotation annotation = property.getAccessor().getAnnotation(CustomSerialization.class);
            if (annotation != null) {
                serializeFieldCustomized(entity, gen, property, properties, (CustomSerialization) annotation);
            } else {
                serializeField(entity, gen, serializers, beanDescription, property);
            }
            // 3. check if property is EntitySet than eventually write count
            if (EntitySet.class.isAssignableFrom(property.getAccessor().getRawType())) {
                Object rawValue = property.getAccessor().getValue(entity);
                if (rawValue != null) {
                    EntitySet set = (EntitySet) rawValue;
                    long count = set.getCount();
                    if (count >= 0) {
                        gen.writeNumberField(property.getName() + "@iot.count", count);
                    }
                    String nextLink = set.getNextLink();
                    if (nextLink != null) {
                        gen.writeStringField(property.getName() + "@iot.nextLink", nextLink);
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("could not serialize Entity", e);
        throw new IOException("could not serialize Entity", e);
    } finally {
        gen.writeEndObject();
    }
}

From source file:org.springframework.rest.documentation.boot.SwaggerDocumentationEndpoint.java

private void addModelForType(String type, Map<String, ClassDescriptor> responseClasses,
        Documentation documentation) {/*from   ww  w . j a v a2  s.  c o m*/
    String name = getSwaggerDataType(type);
    if (documentation.getModels() == null || !documentation.getModels().containsKey(name)) {
        DocumentationSchema schema = new DocumentationSchema();

        ClassDescriptor classDescriptor = responseClasses.get(type);
        if (classDescriptor != null) {
            schema.setDescription(classDescriptor.getName());
        }

        try {
            Class<?> clazz = Class.forName(type);

            if (clazz.isEnum()) {
                Object[] enumConstants = clazz.getEnumConstants();
                List<String> values = new ArrayList<String>();
                for (Object enumConstant : enumConstants) {
                    values.add(enumConstant.toString());
                }
                schema.setAllowableValues(new DocumentationAllowableListValues(values));
            } else {
                BasicClassIntrospector introspector = new BasicClassIntrospector();

                Map<String, DocumentationSchema> properties = new HashMap<String, DocumentationSchema>();

                BasicBeanDescription descriptor = introspector.forSerialization(
                        this.objectMapper.getSerializationConfig(),
                        TypeFactory.defaultInstance().constructType(clazz),
                        this.objectMapper.getSerializationConfig());

                for (BeanPropertyDefinition property : descriptor.findProperties()) {
                    String propertyName = property.getName();
                    DocumentationSchema propertySchema = new DocumentationSchema();
                    MethodDescriptor methodDescriptor = classDescriptor
                            .getMethodDescriptor((Method) property.getAccessor().getAnnotated());
                    if (methodDescriptor != null) {
                        Class<?> propertyClass = Class.forName(methodDescriptor.getReturnType());

                        propertySchema.setDescription(methodDescriptor.getSummary());

                        if (propertyClass.isEnum()) {
                            Object[] enumConstants = propertyClass.getEnumConstants();
                            List<String> values = new ArrayList<String>();
                            for (Object enumConstant : enumConstants) {
                                values.add(enumConstant.toString());
                            }
                            propertySchema.setType("string");
                            propertySchema.setAllowableValues(new DocumentationAllowableListValues(values));
                        } else {
                            propertySchema.setType(getSwaggerDataType(methodDescriptor.getReturnType()));
                            addModelForType(methodDescriptor.getReturnType(), responseClasses, documentation);
                        }
                    }

                    properties.put(propertyName, propertySchema);
                }

                schema.setProperties(properties);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        documentation.addModel(name, schema);
    }
}

From source file:org.midonet.cluster.rest_api.serialization.MidonetObjectMapper.java

static JsonError getError(DeserializationConfig config, JavaType valueType, String fieldName) {
    try {//from   w  ww .  j av  a2s .c o m
        BasicBeanDescription beanDesc = config.introspect(valueType);
        for (BeanPropertyDefinition property : beanDesc.findProperties()) {
            if (property.getName().equals(fieldName) && null != property.getField()
                    && null != property.getField().getAnnotated()) {
                return property.getField().getAnnotated().getAnnotation(JsonError.class);
            }
        }
        return null;
    } catch (Exception e) {
        return null;
    }
}