Search in sources :

Example 6 with PropertySerializerMap

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

the class ObjectFieldPropertyWriter method serializeAsField.

/*
    /**********************************************************
    /* Overrides
    /**********************************************************
     */
@Override
public final void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
    if (broken) {
        fallbackWriter.serializeAsField(bean, gen, prov);
        return;
    }
    Object value;
    try {
        value = _propertyAccessor.objectField(bean, _propertyIndex);
    } catch (Throwable t) {
        _handleProblem(bean, gen, prov, t, false);
        return;
    }
    // Null (etc) handling; copied from super-class impl
    if (value == null) {
        if (_nullSerializer != null) {
            gen.writeFieldName(_fastName);
            _nullSerializer.serialize(null, gen, prov);
        } else if (!_suppressNulls) {
            gen.writeFieldName(_fastName);
            prov.defaultSerializeNull(gen);
        }
        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)) {
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            return;
        }
    }
    if (value == bean) {
        // three choices: exception; handled by call; or pass-through
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    gen.writeFieldName(_fastName);
    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 7 with PropertySerializerMap

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

the class ObjectFieldPropertyWriter method serializeAsElement.

@Override
public final void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
    if (broken) {
        fallbackWriter.serializeAsElement(bean, gen, prov);
        return;
    }
    Object value;
    try {
        value = _propertyAccessor.objectField(bean, _propertyIndex);
    } catch (Throwable t) {
        _handleProblem(bean, gen, prov, t, true);
        return;
    }
    if (value == null) {
        if (_nullSerializer != null) {
            _nullSerializer.serialize(null, gen, prov);
        } else if (_suppressNulls) {
            serializeAsPlaceholder(bean, gen, prov);
        } else {
            prov.defaultSerializeNull(gen);
        }
        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) {
        // three choices: exception; handled by call; or pass-through
        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)

Example 8 with PropertySerializerMap

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

the class ObjectMethodPropertyWriter method serializeAsElement.

@Override
public final void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
    if (broken) {
        fallbackWriter.serializeAsElement(bean, gen, prov);
        return;
    }
    Object value;
    try {
        value = _propertyAccessor.objectGetter(bean, _propertyIndex);
    } catch (Throwable t) {
        _handleProblem(bean, gen, prov, t, true);
        return;
    }
    // Null (etc) handling; copied from super-class impl
    if (value == null) {
        if (_nullSerializer != null) {
            _nullSerializer.serialize(null, gen, prov);
        } else if (_suppressNulls) {
            serializeAsPlaceholder(bean, gen, prov);
        } else {
            prov.defaultSerializeNull(gen);
        }
        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) {
        // three choices: exception; handled by call; or pass-through
        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)

Example 9 with PropertySerializerMap

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

the class ObjectMethodPropertyWriter method serializeAsField.

/*
    /**********************************************************
    /* Overrides
    /**********************************************************
     */
@Override
public final void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
    if (broken) {
        fallbackWriter.serializeAsField(bean, gen, prov);
        return;
    }
    Object value;
    try {
        value = _propertyAccessor.objectGetter(bean, _propertyIndex);
    } catch (Throwable t) {
        _handleProblem(bean, gen, prov, t, false);
        return;
    }
    // Null (etc) handling; copied from super-class impl
    if (value == null) {
        if (_nullSerializer != null) {
            gen.writeFieldName(_fastName);
            _nullSerializer.serialize(null, gen, prov);
        } else if (!_suppressNulls) {
            gen.writeFieldName(_fastName);
            prov.defaultSerializeNull(gen);
        }
        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)) {
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            return;
        }
    }
    if (value == bean) {
        // three choices: exception; handled by call; or pass-through
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }
    gen.writeFieldName(_fastName);
    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 10 with PropertySerializerMap

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

the class BeanPropertyWriter method serializeAsElement.

/**
     * Alternative to {@link #serializeAsField} that is used when a POJO is
     * serialized as JSON Array; the difference is that no field names are
     * written.
     * 
     * @since 2.3
     */
@Override
public void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
    // inlined 'get()'
    final Object value = (_accessorMethod == null) ? _field.get(bean) : _accessorMethod.invoke(bean, (Object[]) null);
    if (value == null) {
        // nulls need specialized handling
        if (_nullSerializer != null) {
            _nullSerializer.serialize(null, gen, prov);
        } else {
            // can NOT suppress entries in tabular output
            gen.writeNull();
        }
        return;
    }
    // otherwise find serializer to use
    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);
        }
    }
    // and then see if we must suppress certain values (default, empty)
    if (_suppressableValue != null) {
        if (MARKER_FOR_EMPTY == _suppressableValue) {
            if (ser.isEmpty(prov, value)) {
                // can NOT suppress entries in
                // tabular output
                serializeAsPlaceholder(bean, gen, prov);
                return;
            }
        } else if (_suppressableValue.equals(value)) {
            // can NOT suppress
            // entries in tabular
            // output
            serializeAsPlaceholder(bean, gen, prov);
            return;
        }
    }
    // For non-nulls: simple check for direct cycles
    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