Search in sources :

Example 11 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project java-chassis by ServiceComb.

the class ProducerArgumentsMapperCreator method processBeanParameter.

@Override
protected void processBeanParameter(int producerParamIdx, Parameter producerParameter) {
    ProducerBeanParamMapper mapper = new ProducerBeanParamMapper(providerMethod.getParameters()[producerParamIdx].getName(), producerParameter.getType());
    JavaType producerType = TypeFactory.defaultInstance().constructType(producerParameter.getParameterizedType());
    for (BeanPropertyDefinition propertyDefinition : serializationConfig.introspect(producerType).findProperties()) {
        String parameterName = collectParameterName(providerMethod, propertyDefinition);
        Integer swaggerIdx = findSwaggerParameterIndex(parameterName);
        if (swaggerIdx == null) {
            throw new IllegalStateException(String.format("failed to find producer parameter in contract, method=%s:%s, bean parameter name=%s.", providerMethod.getDeclaringClass().getName(), providerMethod.getName(), parameterName));
        }
        swaggerParameterTypes.put(parameterName, propertyDefinition.getPrimaryType());
        mapper.addField(parameterName, LambdaMetafactoryUtils.createObjectSetter(propertyDefinition));
        processedSwaggerParamters.add(parameterName);
    }
    mappers.add(mapper);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)

Example 12 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project alfresco-remote-api by Alfresco.

the class SerializerOfExecutionResult method serialize.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void serialize(ExecutionResult value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
    SerializationConfig config = provider.getConfig();
    Object rootObj = value.getRoot();
    if (rootObj == null) {
        provider.getDefaultNullValueSerializer().serialize(null, jgen, provider);
    } else {
        Class<?> cls = rootObj.getClass();
        // create an untyped map, add the contents of the root + the embeds.
        Map toBeSerialized = new HashMap();
        BeanPropertiesFilter filter = value.getFilter();
        if (filter == null)
            filter = BeanPropertiesFilter.ALLOW_ALL;
        if (Map.class.isAssignableFrom(cls)) {
            // Its a map so
            Map rootAsaMap = (Map) rootObj;
            toBeSerialized.putAll(rootAsaMap);
        } else {
            JavaType classType = config.constructType(cls);
            BeanDescription beanDesc = provider.getConfig().introspect(classType);
            List<BeanPropertyDefinition> props = beanDesc.findProperties();
            for (BeanPropertyDefinition beanProperty : props) {
                if (beanProperty.couldSerialize() && filter.isAllowed(beanProperty.getName())) {
                    Object propertyValue = ResourceInspectorUtil.invokeMethod(beanProperty.getGetter().getAnnotated(), rootObj);
                    if (propertyValue != null) {
                        if ((propertyValue instanceof String)) {
                            if (((String) propertyValue).trim().length() > 0) {
                                toBeSerialized.put(beanProperty.getName(), propertyValue);
                            }
                        } else {
                            toBeSerialized.put(beanProperty.getName(), propertyValue);
                        }
                    }
                }
            }
        }
        // Add embedded
        for (Entry<String, Object> embedded : value.getEmbedded().entrySet()) {
            if (filter == null || filter.isAllowed(embedded.getKey())) {
                toBeSerialized.put(embedded.getKey(), embedded.getValue());
            }
        }
        // if its an embedded entity then render the properties (not as an "entry:")
        if (value.isAnEmbeddedEntity()) {
            jgen.writeObject(toBeSerialized);
        } else {
            jgen.writeStartObject();
            jgen.writeObjectField("entry", toBeSerialized);
            if (value.getRelated() != null && !value.getRelated().isEmpty()) {
                jgen.writeObjectField("relations", value.getRelated());
            }
            jgen.writeEndObject();
        }
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) HashMap(java.util.HashMap) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project endpoints-java by cloudendpoints.

the class JacksonResourceSchemaProvider method getResourceSchema.

@Override
public ResourceSchema getResourceSchema(TypeToken<?> type, ApiConfig config) {
    ResourceSchema schema = super.getResourceSchema(type, config);
    if (schema != null) {
        return schema;
    }
    ObjectMapper objectMapper = ObjectMapperUtil.createStandardObjectMapper(config.getSerializationConfig());
    JavaType javaType = objectMapper.getTypeFactory().constructType(type.getRawType());
    BeanDescription beanDescription = objectMapper.getSerializationConfig().introspect(javaType);
    ResourceSchema.Builder schemaBuilder = ResourceSchema.builderForType(type.getRawType());
    Set<String> genericDataFieldNames = getGenericDataFieldNames(type);
    for (BeanPropertyDefinition definition : beanDescription.findProperties()) {
        TypeToken<?> propertyType = getPropertyType(type, toMethod(definition.getGetter()), toMethod(definition.getSetter()), definition.getField(), config);
        String name = definition.getName();
        if (genericDataFieldNames == null || genericDataFieldNames.contains(name)) {
            if (hasUnresolvedType(propertyType)) {
                logger.atWarning().log("skipping field '%s' of type '%s' because it is unresolved.", name, propertyType);
                continue;
            }
            if (propertyType != null) {
                ResourcePropertySchema propertySchema = ResourcePropertySchema.of(propertyType);
                propertySchema.setDescription(definition.getMetadata().getDescription());
                schemaBuilder.addProperty(name, propertySchema);
            } else {
                logger.atWarning().log("No type found for property '%s' on class '%s'.", name, type);
            }
        } else {
            logger.atFine().log("skipping field '%s' because it's not a Java client model field.", name);
        }
    }
    return schemaBuilder.build();
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ResourceSchema(com.google.api.server.spi.config.ResourceSchema) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ResourcePropertySchema(com.google.api.server.spi.config.ResourcePropertySchema) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 14 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project incubator-servicecomb-java-chassis by apache.

the class TestLambdaMetafactoryUtils method should_support_primitive_type.

@Test
public void should_support_primitive_type() {
    Child child = new Child();
    ObjectMapper mapper = JsonUtils.OBJ_MAPPER;
    BeanDescription beanDescription = mapper.getSerializationConfig().introspect(mapper.constructType(Child.class));
    List<BeanPropertyDefinition> properties = beanDescription.findProperties();
    assertThat(properties).hasSize(2);
    for (int idx = 0; idx < properties.size(); idx++) {
        BeanPropertyDefinition property = properties.get(idx);
        Setter<Object, Object> setter = createObjectSetter(property.getSetter().getAnnotated());
        setter.set(child, idx);
        Getter<Object, Object> getter = createObjectGetter(property.getGetter().getAnnotated());
        Object value = getter.get(child);
        assertThat(value).isEqualTo(idx);
    }
}
Also used : BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 15 with BeanPropertyDefinition

use of com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition in project incubator-servicecomb-java-chassis by apache.

the class AbstractOperationGenerator method extractAggregatedParameterGenerators.

protected void extractAggregatedParameterGenerators(Map<String, List<Annotation>> methodAnnotationMap, java.lang.reflect.Parameter methodParameter) {
    JavaType javaType = TypeFactory.defaultInstance().constructType(methodParameter.getParameterizedType());
    BeanDescription beanDescription = Json.mapper().getSerializationConfig().introspect(javaType);
    for (BeanPropertyDefinition propertyDefinition : beanDescription.findProperties()) {
        if (!propertyDefinition.couldSerialize()) {
            continue;
        }
        Annotation[] annotations = collectAnnotations(propertyDefinition);
        ParameterGenerator propertyParameterGenerator = new ParameterGenerator(method, methodAnnotationMap, propertyDefinition.getName(), annotations, propertyDefinition.getPrimaryType());
        parameterGenerators.add(propertyParameterGenerator);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator) Annotation(java.lang.annotation.Annotation)

Aggregations

BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)32 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)24 JavaType (com.fasterxml.jackson.databind.JavaType)21 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 ArrayList (java.util.ArrayList)10 Annotation (java.lang.annotation.Annotation)8 Type (java.lang.reflect.Type)6 HashSet (java.util.HashSet)6 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)5 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)3 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)3 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)3 AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)3 AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)3 POJOPropertyBuilder (com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 JsonIdentityInfo (com.fasterxml.jackson.annotation.JsonIdentityInfo)2