Java Code Examples for com.fasterxml.jackson.databind.util.BeanUtil#getDefaultValue()

The following examples show how to use com.fasterxml.jackson.databind.util.BeanUtil#getDefaultValue() . 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: ReferenceTypeSerializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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;
                case ALWAYS: // default
                default:
                    valueToSuppress = null;
                    suppressNulls = false;
                    break;
                }
                if ((_suppressableValue != valueToSuppress)
                        || (_suppressNulls != suppressNulls)) {
                    refSer = refSer.withContentInclusion(valueToSuppress, suppressNulls);
                }
            }
        }
    }
    return refSer;
}
 
Example 2
Source File: MapEntrySerializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider,
        BeanProperty property) throws JsonMappingException
{
    JsonSerializer<?> ser = null;
    JsonSerializer<?> keySer = null;
    final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    final AnnotatedMember propertyAcc = (property == null) ? null : property.getMember();

    // First: if we have a property, may have property-annotation overrides
    if (propertyAcc != null && intr != null) {
        Object serDef = intr.findKeySerializer(propertyAcc);
        if (serDef != null) {
            keySer = provider.serializerInstance(propertyAcc, serDef);
        }
        serDef = intr.findContentSerializer(propertyAcc);
        if (serDef != null) {
            ser = provider.serializerInstance(propertyAcc, serDef);
        }
    }
    if (ser == null) {
        ser = _valueSerializer;
    }
    // [databind#124]: May have a content converter
    ser = findContextualConvertingSerializer(provider, property, ser);
    if (ser == null) {
        // 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
        //   we can consider it a static case as well.
        // 20-Aug-2013, tatu: Need to avoid trying to access serializer for java.lang.Object tho
        if (_valueTypeIsStatic && !_valueType.isJavaLangObject()) {
            ser = provider.findValueSerializer(_valueType, property);
        }
    }
    if (keySer == null) {
        keySer = _keySerializer;
    }
    if (keySer == null) {
        keySer = provider.findKeySerializer(_keyType, property);
    } else {
        keySer = provider.handleSecondaryContextualization(keySer, property);
    }

    Object valueToSuppress = _suppressableValue;
    boolean suppressNulls = _suppressNulls;
    if (property != null) {
        JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), null);
        if (inclV != null) {
            JsonInclude.Include incl = inclV.getContentInclusion();
            if (incl != JsonInclude.Include.USE_DEFAULTS) {
                switch (incl) {
                case NON_DEFAULT:
                    valueToSuppress = BeanUtil.getDefaultValue(_valueType);
                    suppressNulls = true;
                    if (valueToSuppress != null) {
                        if (valueToSuppress.getClass().isArray()) {
                            valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
                        }
                    }
                    break;
                case NON_ABSENT:
                    suppressNulls = true;
                    valueToSuppress = _valueType.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;
                case ALWAYS: // default
                default:
                    valueToSuppress = null;
                    // 30-Sep-2016, tatu: Should not need to check global flags here,
                    //   if inclusion forced to be ALWAYS
                    suppressNulls = false;
                    break;
                }
            }
        }
    }
    
    MapEntrySerializer mser = withResolved(property, keySer, ser,
            valueToSuppress, suppressNulls);
    // but note: no (full) filtering or sorting (unlike Maps)
    return mser;
}