Search in sources :

Example 1 with TypeSerializer

use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.

the class MapSerializer method serializeFieldsUsing.

/**
     * Method called to serialize fields, when the value type is statically known,
     * so that value serializer is passed and does not need to be fetched from
     * provider.
     */
public void serializeFieldsUsing(Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, JsonSerializer<Object> ser) throws IOException {
    final JsonSerializer<Object> keySerializer = _keySerializer;
    final Set<String> ignored = _ignoredEntries;
    final TypeSerializer typeSer = _valueTypeSerializer;
    for (Map.Entry<?, ?> entry : value.entrySet()) {
        Object keyElem = entry.getKey();
        if (ignored != null && ignored.contains(keyElem))
            continue;
        if (keyElem == null) {
            provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
        } else {
            keySerializer.serialize(keyElem, gen, provider);
        }
        final Object valueElem = entry.getValue();
        if (valueElem == null) {
            provider.defaultSerializeNull(gen);
        } else {
            try {
                if (typeSer == null) {
                    ser.serialize(valueElem, gen, provider);
                } else {
                    ser.serializeWithType(valueElem, gen, provider, typeSer);
                }
            } catch (Exception e) {
                wrapAndThrow(provider, e, value, String.valueOf(keyElem));
            }
        }
    }
}
Also used : TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer) PropertySerializerMap(com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap) IOException(java.io.IOException)

Example 2 with TypeSerializer

use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.

the class ObjectArraySerializer method serializeTypedContents.

public void serializeTypedContents(Object[] value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    final int len = value.length;
    final TypeSerializer typeSer = _valueTypeSerializer;
    int i = 0;
    Object elem = null;
    try {
        PropertySerializerMap serializers = _dynamicSerializers;
        for (; i < len; ++i) {
            elem = value[i];
            if (elem == null) {
                provider.defaultSerializeNull(jgen);
                continue;
            }
            Class<?> cc = elem.getClass();
            JsonSerializer<Object> serializer = serializers.serializerFor(cc);
            if (serializer == null) {
                serializer = _findAndAddDynamic(serializers, cc, provider);
            }
            serializer.serializeWithType(elem, jgen, provider, typeSer);
        }
    } catch (Exception e) {
        wrapAndThrow(provider, e, elem, i);
    }
}
Also used : PropertySerializerMap(com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap) TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer) IOException(java.io.IOException)

Example 3 with TypeSerializer

use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.

the class ObjectArraySerializer method createContextual.

/*
    /**********************************************************
    /* Post-processing
    /**********************************************************
     */
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers, BeanProperty property) throws JsonMappingException {
    TypeSerializer vts = _valueTypeSerializer;
    if (vts != null) {
        vts = vts.forProperty(property);
    }
    JsonSerializer<?> ser = null;
    Boolean unwrapSingle = null;
    // First: if we have a property, may have property-annotation overrides
    if (property != null) {
        AnnotatedMember m = property.getMember();
        final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
        if (m != null) {
            Object serDef = intr.findContentSerializer(m);
            if (serDef != null) {
                ser = serializers.serializerInstance(m, serDef);
            }
        }
    }
    JsonFormat.Value format = findFormatOverrides(serializers, property, handledType());
    if (format != null) {
        unwrapSingle = format.getFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
    }
    if (ser == null) {
        ser = _elementSerializer;
    }
    // [databind#124]: May have a content converter
    ser = findContextualConvertingSerializer(serializers, property, ser);
    if (ser == null) {
        //   we can consider it a static case as well.
        if (_elementType != null) {
            if (_staticTyping && !_elementType.isJavaLangObject()) {
                ser = serializers.findValueSerializer(_elementType, property);
            }
        }
    }
    return withResolved(property, vts, ser, unwrapSingle);
}
Also used : JsonFormat(com.fasterxml.jackson.annotation.JsonFormat) TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember)

Example 4 with TypeSerializer

use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.

the class ReferenceTypeSerializer method createContextual.

/*
    /**********************************************************
    /* Contextualization (support for property annotations)
    /**********************************************************
     */
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property) throws JsonMappingException {
    TypeSerializer typeSer = _valueTypeSerializer;
    if (typeSer != null) {
        typeSer = typeSer.forProperty(property);
    }
    // First: do we have an annotation override from property?
    JsonSerializer<?> ser = findAnnotatedContentSerializer(provider, property);
    if (ser == null) {
        // If not, use whatever was configured by type
        ser = _valueSerializer;
        if (ser == null) {
            // A few conditions needed to be able to fetch serializer here:
            if (_useStatic(provider, property, _referredType)) {
                ser = _findSerializer(provider, _referredType, property);
            }
        } else {
            ser = provider.handlePrimaryContextualization(ser, property);
        }
    }
    // First, resolve wrt property, resolved serializers
    ReferenceTypeSerializer<?> refSer;
    if ((_property == property) && (_valueTypeSerializer == typeSer) && (_valueSerializer == ser)) {
        refSer = this;
    } else {
        refSer = withResolved(property, typeSer, ser, _unwrapper);
    }
    // and then see if we have property-inclusion overrides
    if (property != null) {
        JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), handledType());
        if (inclV != null) {
            JsonInclude.Include incl = inclV.getContentInclusion();
            if (incl != JsonInclude.Include.USE_DEFAULTS) {
                Object valueToSuppress;
                boolean suppressNulls;
                switch(incl) {
                    case NON_DEFAULT:
                        valueToSuppress = BeanUtil.getDefaultValue(_referredType);
                        suppressNulls = true;
                        if (valueToSuppress != null) {
                            if (valueToSuppress.getClass().isArray()) {
                                valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
                            }
                        }
                        break;
                    case NON_ABSENT:
                        suppressNulls = true;
                        valueToSuppress = _referredType.isReferenceType() ? MARKER_FOR_EMPTY : null;
                        break;
                    case NON_EMPTY:
                        suppressNulls = true;
                        valueToSuppress = MARKER_FOR_EMPTY;
                        break;
                    case CUSTOM:
                        valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter());
                        if (valueToSuppress == null) {
                            // is this legal?
                            suppressNulls = true;
                        } else {
                            suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
                        }
                        break;
                    case NON_NULL:
                        valueToSuppress = null;
                        suppressNulls = true;
                        break;
                    // default
                    case ALWAYS:
                    default:
                        valueToSuppress = null;
                        suppressNulls = false;
                        break;
                }
                if ((_suppressableValue != valueToSuppress) || (_suppressNulls != suppressNulls)) {
                    refSer = refSer.withContentInclusion(valueToSuppress, suppressNulls);
                }
            }
        }
    }
    return refSer;
}
Also used : TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude)

Example 5 with TypeSerializer

use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.

the class AsArraySerializerBase method createContextual.

/*
    /**********************************************************
    /* Post-processing
    /**********************************************************
     */
/**
     * This method is needed to resolve contextual annotations like
     * per-property overrides, as well as do recursive call
     * to <code>createContextual</code> of content serializer, if
     * known statically.
     */
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers, BeanProperty property) throws JsonMappingException {
    TypeSerializer typeSer = _valueTypeSerializer;
    if (typeSer != null) {
        typeSer = typeSer.forProperty(property);
    }
    JsonSerializer<?> ser = null;
    Boolean unwrapSingle = null;
    if (property != null) {
        final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
        AnnotatedMember m = property.getMember();
        if (m != null) {
            Object serDef = intr.findContentSerializer(m);
            if (serDef != null) {
                ser = serializers.serializerInstance(m, serDef);
            }
        }
    }
    JsonFormat.Value format = findFormatOverrides(serializers, property, handledType());
    if (format != null) {
        unwrapSingle = format.getFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
    }
    if (ser == null) {
        ser = _elementSerializer;
    }
    // 18-Feb-2013, tatu: May have a content converter:
    ser = findContextualConvertingSerializer(serializers, property, ser);
    if (ser == null) {
        //   we can consider it a static case as well.
        if (_elementType != null) {
            if (_staticTyping && !_elementType.isJavaLangObject()) {
                ser = serializers.findValueSerializer(_elementType, property);
            }
        }
    }
    if ((ser != _elementSerializer) || (property != _property) || (_valueTypeSerializer != typeSer) || (_unwrapSingle != unwrapSingle)) {
        return withResolved(property, typeSer, ser, unwrapSingle);
    }
    return this;
}
Also used : JsonFormat(com.fasterxml.jackson.annotation.JsonFormat) TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember)

Aggregations

TypeSerializer (com.fasterxml.jackson.databind.jsontype.TypeSerializer)24 IOException (java.io.IOException)8 PropertySerializerMap (com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap)3 JsonFormat (com.fasterxml.jackson.annotation.JsonFormat)2 AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)2 FilteredBeanPropertyWriter (com.fasterxml.jackson.databind.ser.impl.FilteredBeanPropertyWriter)2 TypeWrappedSerializer (com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer)2 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1