Search in sources :

Example 1 with PropertySerializerMap

use of com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap in project jackson-databind by FasterXML.

the class ObjectArraySerializer method serializeContents.

@Override
public void serializeContents(Object[] value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    final int len = value.length;
    if (len == 0) {
        return;
    }
    if (_elementSerializer != null) {
        serializeContentsUsing(value, gen, provider, _elementSerializer);
        return;
    }
    if (_valueTypeSerializer != null) {
        serializeTypedContents(value, gen, provider);
        return;
    }
    int i = 0;
    Object elem = null;
    try {
        PropertySerializerMap serializers = _dynamicSerializers;
        for (; i < len; ++i) {
            elem = value[i];
            if (elem == null) {
                provider.defaultSerializeNull(gen);
                continue;
            }
            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);
                }
            }
            serializer.serialize(elem, gen, provider);
        }
    } catch (Exception e) {
        wrapAndThrow(provider, e, elem, i);
    }
}
Also used : PropertySerializerMap(com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap) IOException(java.io.IOException)

Example 2 with PropertySerializerMap

use of com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap 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 PropertySerializerMap

use of com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap 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 4 with PropertySerializerMap

use of com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap in project jackson-databind by FasterXML.

the class BeanPropertyWriter method serializeAsField.

/*
    /***********************************************************
    /* PropertyWriter methods (serialization)
    /***********************************************************
     */
/**
     * Method called to access property that this bean stands for, from within
     * given bean, and to serialize it as a JSON Object field using appropriate
     * serializer.
     */
@Override
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
    // inlined 'get()'
    final Object value = (_accessorMethod == null) ? _field.get(bean) : _accessorMethod.invoke(bean, (Object[]) null);
    // Null handling is bit different, check that first
    if (value == null) {
        if (_nullSerializer != null) {
            gen.writeFieldName(_name);
            _nullSerializer.serialize(null, gen, prov);
        }
        return;
    }
    // then find serializer to use
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
        Class<?> cls = value.getClass();
        PropertySerializerMap m = _dynamicSerializers;
        ser = m.serializerFor(cls);
        if (ser == null) {
            ser = _findAndAddDynamic(m, cls, prov);
        }
    }
    // and then see if we must suppress certain values (default, empty)
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            return;
        }
    }
    // For non-nulls: simple check for direct cycles
    if (value == bean) {
        // three choices: exception; handled by call; or pass-through
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    gen.writeFieldName(_name);
    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}
Also used : PropertySerializerMap(com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap)

Example 5 with PropertySerializerMap

use of com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap in project jackson-databind by FasterXML.

the class VirtualBeanPropertyWriter method serializeAsElement.

// This one's fine as-is from base class
//public void serializeAsOmittedField(Object bean, JsonGenerator jgen, SerializerProvider prov) throws Exception
@Override
public void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
    // NOTE: mostly copied from base class, but off-lined get() access
    final Object value = value(bean, gen, prov);
    if (value == null) {
        if (_nullSerializer != null) {
            _nullSerializer.serialize(null, gen, prov);
        } else {
            gen.writeNull();
        }
        return;
    }
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
        Class<?> cls = value.getClass();
        PropertySerializerMap map = _dynamicSerializers;
        ser = map.serializerFor(cls);
        if (ser == null) {
            ser = _findAndAddDynamic(map, cls, prov);
        }
    }
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                serializeAsPlaceholder(bean, gen, prov);
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            serializeAsPlaceholder(bean, gen, prov);
            return;
        }
    }
    if (value == bean) {
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}
Also used : PropertySerializerMap(com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap)

Aggregations

PropertySerializerMap (com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap)11 IOException (java.io.IOException)3 TypeSerializer (com.fasterxml.jackson.databind.jsontype.TypeSerializer)2