Java Code Examples for com.fasterxml.jackson.databind.JsonSerializer#isEmpty()

The following examples show how to use com.fasterxml.jackson.databind.JsonSerializer#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnwrappingBeanPropertyWriter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov)
        throws Exception
{
    final Object value = get(bean);
    if (value == null) {
        // Hmmh. I assume we MUST pretty much suppress nulls, since we
        // can't really unwrap them...
        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;
        }
    }
    // For non-nulls, first: simple check for direct cycles
    if (value == bean) {
        if (_handleSelfReference(bean, gen, prov, ser)) {
            return;
        }
    }

    // note: must verify we are using unwrapping serializer; if not, will write field name
    if (!ser.isUnwrappingSerializer()) {
        gen.writeFieldName(_name);
    }

    if (_typeSerializer == null) {
        ser.serialize(value, gen, prov);
    } else {
        ser.serializeWithType(value, gen, prov, _typeSerializer);
    }
}
 
Example 2
Source File: ObjectMethodPropertyWriter.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@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.defaultSerializeNullValue(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);
    }
}
 
Example 3
Source File: ObjectMethodPropertyWriter.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@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.defaultSerializeNullValue(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);
    }
}
 
Example 4
Source File: ObjectFieldPropertyWriter.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@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.defaultSerializeNullValue(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);
    }
}
 
Example 5
Source File: ObjectFieldPropertyWriter.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@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.defaultSerializeNullValue(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);
    }
}