Example usage for com.fasterxml.jackson.databind.introspect BasicClassIntrospector BasicClassIntrospector

List of usage examples for com.fasterxml.jackson.databind.introspect BasicClassIntrospector BasicClassIntrospector

Introduction

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

Prototype

BasicClassIntrospector

Source Link

Usage

From source file:nl.talsmasoftware.enumerables.support.json.jackson2.EnumerableSerializer.java

private static List<BeanPropertyDefinition> serializationPropertiesFor(Class<?> simpleType,
        SerializationConfig config) {/*from  w w  w.j  a v  a 2  s. c o  m*/
    final String cacheKey = simpleType.getName();
    List<BeanPropertyDefinition> properties = CACHE.get(cacheKey);
    if (properties == null) {
        properties = new BasicClassIntrospector()
                .forSerialization(config, SimpleType.construct(simpleType), null).findProperties();
        CACHE.putIfAbsent(cacheKey, properties);
    }
    return properties;
}

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

private BasicBeanDescription beanDescription(Class<?> cls) {
    BasicClassIntrospector bci = new BasicClassIntrospector();
    return bci.forSerialization(mapper.getSerializationConfig(), mapper.constructType(cls),
            mapper.getSerializationConfig());
}

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  va 2  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);
    }
}