Example usage for com.fasterxml.jackson.databind SerializerProvider constructType

List of usage examples for com.fasterxml.jackson.databind SerializerProvider constructType

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind SerializerProvider constructType.

Prototype

public JavaType constructType(Type paramType) 

Source Link

Usage

From source file:org.apache.ode.jacob.soup.jackson.ChannelProxySerializer.java

private void serializeContents(ChannelProxy value, JsonGenerator jgen, SerializerProvider provider)
        throws JsonGenerationException, IOException {
    CommChannel commChannel = ChannelFactory.getBackend((Channel) value);
    ClassNameIdResolver idResolver = new ClassNameIdResolver(provider.constructType(commChannel.getType()),
            provider.getTypeFactory());/*from w ww. j  ava 2  s.  c o m*/
    Integer cid = (Integer) commChannel.getId();
    jgen.writeStringField("channelType", idResolver.idFromBaseType());
    jgen.writeNumberField("channelId", cid);

    // save channel id for garbage collection
    executionQueueImplSerializer.markChannelUsed(cid);
}

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 w  w.j ava  2  s .  c  om*/
    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:de.fraunhofer.iosb.ilt.sta.serialize.EntitySerializer.java

protected void serializeFieldTyped(Entity entity, JsonGenerator gen, SerializerProvider serializers,
        BeanDescription beanDescription, BeanPropertyDefinition beanPropertyDefinition,
        TypeSerializer typeSerializer) throws Exception {
    try {//from  w  w w  .j av  a  2s.c  o  m
        if (typeSerializer == null) {
            typeSerializer = serializers.findTypeSerializer(
                    serializers.constructType(beanPropertyDefinition.getAccessor().getRawType()));
        }
        if (typeSerializer == null) {
            // if not static type if available use dynamic type if available
            Object propertyValue = beanPropertyDefinition.getAccessor().getValue(entity);
            if (propertyValue != null) {
                typeSerializer = serializers
                        .findTypeSerializer(serializers.constructType(propertyValue.getClass()));
            }
        }

        BeanPropertyWriter bpw = new BeanPropertyWriter(beanPropertyDefinition,
                beanPropertyDefinition.getAccessor(), beanDescription.getClassAnnotations(),
                beanPropertyDefinition.getAccessor().getType(), null, // will be searched automatically
                typeSerializer, // will not be searched automatically
                beanPropertyDefinition.getAccessor().getType(),
                suppressNulls(serializers.getConfig().getDefaultPropertyInclusion()),
                suppressableValue(serializers.getConfig().getDefaultPropertyInclusion()));
        bpw.serializeAsField(entity, gen, serializers);
    } catch (JsonMappingException ex) {
        Logger.getLogger(EntitySerializer.class.getName()).log(Level.SEVERE, null, ex);
    }
}