Search in sources :

Example 6 with TypeSerializer

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

the class CollectionSerializer method serializeContentsUsing.

public void serializeContentsUsing(Collection<?> value, JsonGenerator g, SerializerProvider provider, JsonSerializer<Object> ser) throws IOException {
    Iterator<?> it = value.iterator();
    if (it.hasNext()) {
        TypeSerializer typeSer = _valueTypeSerializer;
        int i = 0;
        do {
            Object elem = it.next();
            try {
                if (elem == null) {
                    provider.defaultSerializeNull(g);
                } else {
                    if (typeSer == null) {
                        ser.serialize(elem, g, provider);
                    } else {
                        ser.serializeWithType(elem, g, provider, typeSer);
                    }
                }
                ++i;
            } catch (Exception e) {
                wrapAndThrow(provider, e, value, i);
            }
        } while (it.hasNext());
    }
}
Also used : TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer) IOException(java.io.IOException)

Example 7 with TypeSerializer

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

the class CollectionSerializer method serializeContents.

@Override
public void serializeContents(Collection<?> value, JsonGenerator g, SerializerProvider provider) throws IOException {
    g.setCurrentValue(value);
    if (_elementSerializer != null) {
        serializeContentsUsing(value, g, provider, _elementSerializer);
        return;
    }
    Iterator<?> it = value.iterator();
    if (!it.hasNext()) {
        return;
    }
    PropertySerializerMap serializers = _dynamicSerializers;
    final TypeSerializer typeSer = _valueTypeSerializer;
    int i = 0;
    try {
        do {
            Object elem = it.next();
            if (elem == null) {
                provider.defaultSerializeNull(g);
            } else {
                Class<?> cc = elem.getClass();
                JsonSerializer<Object> serializer = serializers.serializerFor(cc);
                if (serializer == null) {
                    if (_elementType.hasGenericTypes()) {
                        serializer = _findAndAddDynamic(serializers, provider.constructSpecializedType(_elementType, cc), provider);
                    } else {
                        serializer = _findAndAddDynamic(serializers, cc, provider);
                    }
                    serializers = _dynamicSerializers;
                }
                if (typeSer == null) {
                    serializer.serialize(elem, g, provider);
                } else {
                    serializer.serializeWithType(elem, g, provider, typeSer);
                }
            }
            ++i;
        } while (it.hasNext());
    } catch (Exception e) {
        wrapAndThrow(provider, e, value, i);
    }
}
Also used : PropertySerializerMap(com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap) TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer) IOException(java.io.IOException)

Example 8 with TypeSerializer

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

the class BasicSerializerFactory method buildContainerSerializer.

/*
    /**********************************************************
    /* Factory methods, container types:
    /**********************************************************
     */
/**
     * @since 2.1
     */
protected JsonSerializer<?> buildContainerSerializer(SerializerProvider prov, JavaType type, BeanDescription beanDesc, boolean staticTyping) throws JsonMappingException {
    final SerializationConfig config = prov.getConfig();
    /* [databind#23], 15-Mar-2013, tatu: must force static handling of root value type,
         *   with just one important exception: if value type is "untyped", let's
         *   leave it as is; no clean way to make it work.
         */
    if (!staticTyping && type.useStaticType()) {
        if (!type.isContainerType() || !type.getContentType().isJavaLangObject()) {
            staticTyping = true;
        }
    }
    // Let's see what we can learn about element/content/value type, type serializer for it:
    JavaType elementType = type.getContentType();
    TypeSerializer elementTypeSerializer = createTypeSerializer(config, elementType);
    // if elements have type serializer, can not force static typing:
    if (elementTypeSerializer != null) {
        staticTyping = false;
    }
    JsonSerializer<Object> elementValueSerializer = _findContentSerializer(prov, beanDesc.getClassInfo());
    if (type.isMapLikeType()) {
        // implements java.util.Map
        MapLikeType mlt = (MapLikeType) type;
        /* 29-Sep-2012, tatu: This is actually too early to (try to) find
             *  key serializer from property annotations, and can lead to caching
             *  issues (see [databind#75]). Instead, must be done from 'createContextual()' call.
             *  But we do need to check class annotations.
             */
        JsonSerializer<Object> keySerializer = _findKeySerializer(prov, beanDesc.getClassInfo());
        if (mlt.isTrueMapType()) {
            return buildMapSerializer(prov, (MapType) mlt, beanDesc, staticTyping, keySerializer, elementTypeSerializer, elementValueSerializer);
        }
        // With Map-like, just 2 options: (1) Custom, (2) Annotations
        JsonSerializer<?> ser = null;
        MapLikeType mlType = (MapLikeType) type;
        for (Serializers serializers : customSerializers()) {
            // (1) Custom
            ser = serializers.findMapLikeSerializer(config, mlType, beanDesc, keySerializer, elementTypeSerializer, elementValueSerializer);
            if (ser != null) {
                break;
            }
        }
        if (ser == null) {
            // (2) Annotations-based ones:
            ser = findSerializerByAnnotations(prov, type, beanDesc);
        }
        if (ser != null) {
            if (_factoryConfig.hasSerializerModifiers()) {
                for (BeanSerializerModifier mod : _factoryConfig.serializerModifiers()) {
                    ser = mod.modifyMapLikeSerializer(config, mlType, beanDesc, ser);
                }
            }
        }
        return ser;
    }
    if (type.isCollectionLikeType()) {
        CollectionLikeType clt = (CollectionLikeType) type;
        if (clt.isTrueCollectionType()) {
            return buildCollectionSerializer(prov, (CollectionType) clt, beanDesc, staticTyping, elementTypeSerializer, elementValueSerializer);
        }
        // With Map-like, just 2 options: (1) Custom, (2) Annotations
        JsonSerializer<?> ser = null;
        CollectionLikeType clType = (CollectionLikeType) type;
        for (Serializers serializers : customSerializers()) {
            // (1) Custom
            ser = serializers.findCollectionLikeSerializer(config, clType, beanDesc, elementTypeSerializer, elementValueSerializer);
            if (ser != null) {
                break;
            }
        }
        if (ser == null) {
            // (2) Annotations-based ones:
            ser = findSerializerByAnnotations(prov, type, beanDesc);
        }
        if (ser != null) {
            if (_factoryConfig.hasSerializerModifiers()) {
                for (BeanSerializerModifier mod : _factoryConfig.serializerModifiers()) {
                    ser = mod.modifyCollectionLikeSerializer(config, clType, beanDesc, ser);
                }
            }
        }
        return ser;
    }
    if (type.isArrayType()) {
        return buildArraySerializer(prov, (ArrayType) type, beanDesc, staticTyping, elementTypeSerializer, elementValueSerializer);
    }
    return null;
}
Also used : TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer)

Example 9 with TypeSerializer

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

the class IndexedListSerializer method serializeTypedContents.

public void serializeTypedContents(List<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    final int len = value.size();
    if (len == 0) {
        return;
    }
    int i = 0;
    try {
        final TypeSerializer typeSer = _valueTypeSerializer;
        PropertySerializerMap serializers = _dynamicSerializers;
        for (; i < len; ++i) {
            Object elem = value.get(i);
            if (elem == null) {
                provider.defaultSerializeNull(jgen);
            } else {
                Class<?> cc = elem.getClass();
                JsonSerializer<Object> serializer = serializers.serializerFor(cc);
                if (serializer == null) {
                    // To fix [JACKSON-508]
                    if (_elementType.hasGenericTypes()) {
                        serializer = _findAndAddDynamic(serializers, provider.constructSpecializedType(_elementType, cc), provider);
                    } else {
                        serializer = _findAndAddDynamic(serializers, cc, provider);
                    }
                    serializers = _dynamicSerializers;
                }
                serializer.serializeWithType(elem, jgen, provider, typeSer);
            }
        }
    } catch (Exception e) {
        // [JACKSON-55] Need to add reference information
        wrapAndThrow(provider, e, value, i);
    }
}
Also used : TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer) IOException(java.io.IOException)

Example 10 with TypeSerializer

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

the class IteratorSerializer method _serializeDynamicContents.

protected void _serializeDynamicContents(Iterator<?> value, JsonGenerator g, SerializerProvider provider) throws IOException {
    JsonSerializer<Object> serializer = _elementSerializer;
    final TypeSerializer typeSer = _valueTypeSerializer;
    PropertySerializerMap serializers = _dynamicSerializers;
    do {
        Object elem = value.next();
        if (elem == null) {
            provider.defaultSerializeNull(g);
            continue;
        }
        Class<?> cc = elem.getClass();
        serializers.serializerFor(cc);
        if (serializer == null) {
            if (_elementType.hasGenericTypes()) {
                serializer = _findAndAddDynamic(serializers, provider.constructSpecializedType(_elementType, cc), provider);
            } else {
                serializer = _findAndAddDynamic(serializers, cc, provider);
            }
            serializers = _dynamicSerializers;
        }
        if (typeSer == null) {
            serializer.serialize(elem, g, provider);
        } else {
            serializer.serializeWithType(elem, g, provider, typeSer);
        }
    } while (value.hasNext());
}
Also used : TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer)

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