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();
}
}
}
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();
}
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;
}
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;
}
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;
}
Aggregations