Search in sources :

Example 16 with BeanDescription

use of com.fasterxml.jackson.databind.BeanDescription 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 17 with BeanDescription

use of com.fasterxml.jackson.databind.BeanDescription 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 18 with BeanDescription

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

the class AdditionalPropertiesDeserializer 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) {
            for (Class<?> c : TypeToken.of(beanDesc.getBeanClass()).getTypes().classes().rawTypes()) {
                Field[] fields = c.getDeclaredFields();
                for (Field field : fields) {
                    if ("additionalProperties".equalsIgnoreCase(field.getName())) {
                        JsonProperty property = field.getAnnotation(JsonProperty.class);
                        if (property != null && property.value().isEmpty()) {
                            return new AdditionalPropertiesDeserializer(beanDesc.getBeanClass(), deserializer, mapper);
                        }
                    }
                }
            }
            return deserializer;
        }
    });
    return module;
}
Also used : BeanDeserializerModifier(com.fasterxml.jackson.databind.deser.BeanDeserializerModifier) Field(java.lang.reflect.Field) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) 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)

Example 19 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) {
                // will get called for complex types and it can analyze typeId discriminator.
                return new FlatteningDeserializer(beanDesc.getBeanClass(), deserializer, mapper);
            } else {
                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)

Example 20 with BeanDescription

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

the class AdditionalPropertiesSerializer method getModule.

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

        @Override
        public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
            for (Class<?> c : TypeToken.of(beanDesc.getBeanClass()).getTypes().classes().rawTypes()) {
                if (c.isAssignableFrom(Object.class)) {
                    continue;
                }
                Field[] fields = c.getDeclaredFields();
                for (Field field : fields) {
                    if ("additionalProperties".equalsIgnoreCase(field.getName())) {
                        JsonProperty property = field.getAnnotation(JsonProperty.class);
                        if (property != null && property.value().isEmpty()) {
                            return new AdditionalPropertiesSerializer(beanDesc.getBeanClass(), serializer, mapper);
                        }
                    }
                }
            }
            return serializer;
        }
    });
    return module;
}
Also used : Field(java.lang.reflect.Field) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier)

Aggregations

BeanDescription (com.fasterxml.jackson.databind.BeanDescription)47 JavaType (com.fasterxml.jackson.databind.JavaType)25 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)24 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)12 ArrayList (java.util.ArrayList)10 Annotation (java.lang.annotation.Annotation)9 DeserializationConfig (com.fasterxml.jackson.databind.DeserializationConfig)8 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)8 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)7 HashSet (java.util.HashSet)7 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)6 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)6 Type (java.lang.reflect.Type)6 LinkedHashMap (java.util.LinkedHashMap)6 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)5 BeanDeserializerModifier (com.fasterxml.jackson.databind.deser.BeanDeserializerModifier)5 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)4 BeanSerializerModifier (com.fasterxml.jackson.databind.ser.BeanSerializerModifier)4 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)3