Java Code Examples for com.fasterxml.jackson.annotation.JsonInclude#Value

The following examples show how to use com.fasterxml.jackson.annotation.JsonInclude#Value . 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: SimpleBeanPropertyDefinition.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.9
 */
protected SimpleBeanPropertyDefinition(AnnotationIntrospector intr,
        AnnotatedMember member, PropertyName fullName, PropertyMetadata metadata,
        JsonInclude.Value inclusion)
{
    _annotationIntrospector = intr;
    _member = member;
    _fullName = fullName;
    _metadata = (metadata == null) ? PropertyMetadata.STD_OPTIONAL: metadata;
    _inclusion = inclusion;
}
 
Example 2
Source File: VirtualBeanPropertyWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated // since 2.8
protected VirtualBeanPropertyWriter(BeanPropertyDefinition propDef,
        Annotations contextAnnotations, JavaType declaredType,
        JsonSerializer<?> ser, TypeSerializer typeSer, JavaType serType,
        JsonInclude.Value inclusion)
{
    this(propDef, contextAnnotations, declaredType, ser, typeSer, serType, inclusion, null);
}
 
Example 3
Source File: BasicSerializerFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Helper method used for finding inclusion definitions for structured
 * container types like <code>Map</code>s and referential types
 * (like <code>AtomicReference</code>).
 *
 * @param contentType Declared full content type of container
 * @param configType Raw base type under which `configOverride`, if any, needs to be defined
 */
protected JsonInclude.Value _findInclusionWithContent(SerializerProvider prov,
        BeanDescription beanDesc,
        JavaType contentType, Class<?> configType)
    throws JsonMappingException
{
    final SerializationConfig config = prov.getConfig();

    // Defaulting gets complicated because we might have two distinct
    //   axis to consider: Container type itself , and then value (content) type.
    //  Start with Container-defaults, then use more-specific value override, if any.

    // Start by getting global setting, overridden by Map-type-override
    JsonInclude.Value inclV = beanDesc.findPropertyInclusion(config.getDefaultPropertyInclusion());
    inclV = config.getDefaultPropertyInclusion(configType, inclV);

    // and then merge content-type overrides, if any. But note that there's
    // content-to-value inclusion shift we have to do
    JsonInclude.Value valueIncl = config.getDefaultPropertyInclusion(contentType.getRawClass(), null);

    if (valueIncl != null) {
        switch (valueIncl.getValueInclusion()) {
        case USE_DEFAULTS:
            break;
        case CUSTOM:
            inclV = inclV.withContentFilter(valueIncl.getContentFilter());
            break;
        default:
            inclV = inclV.withContentInclusion(valueIncl.getValueInclusion());
        }
    }
    return inclV;
}
 
Example 4
Source File: AnnotationIntrospectorPair.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JsonInclude.Value findPropertyInclusion(Annotated a)
{
    JsonInclude.Value v2 = _secondary.findPropertyInclusion(a);
    JsonInclude.Value v1 = _primary.findPropertyInclusion(a);

    if (v2 == null) { // shouldn't occur but
        return v1;
    }
    return v2.withOverrides(v1);
}
 
Example 5
Source File: ConfigOverrides.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides,
        JsonInclude.Value defIncl,
        JsonSetter.Value defSetter,
        VisibilityChecker<?> defVisibility,
        Boolean defMergeable) {
    _overrides = overrides;
    _defaultInclusion = defIncl;
    _defaultSetterInfo = defSetter;
    _visibilityChecker = defVisibility;
    _defaultMergeable = defMergeable;
}
 
Example 6
Source File: BasicBeanDescription.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method for determining whether null properties should be written
 * out for a Bean of introspected type. This is based on global
 * feature (lowest priority, passed as argument)
 * and per-class annotation (highest priority).
 */
@Override
public JsonInclude.Value findPropertyInclusion(JsonInclude.Value defValue) {
    if (_annotationIntrospector != null) {
        JsonInclude.Value incl = _annotationIntrospector.findPropertyInclusion(_classInfo);
        if (incl != null) {
            return (defValue == null) ? incl : defValue.withOverrides(incl);
        }
    }
    return defValue;
}
 
Example 7
Source File: AttributePropertyWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected AttributePropertyWriter(String attrName, BeanPropertyDefinition propDef,
        Annotations contextAnnotations, JavaType declaredType,
        JsonInclude.Value inclusion)
{
    super(propDef, contextAnnotations, declaredType,
            /* value serializer */ null, /* type serializer */ null, /* ser type */ null,
            inclusion,
            // 10-Oct-2016, tatu: Could enable per-view settings too in future
            null);
    _attrName = attrName;
}
 
Example 8
Source File: VirtualBeanPropertyWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected static boolean _suppressNulls(JsonInclude.Value inclusion) {
    if (inclusion == null) {
        return false;
    }
    JsonInclude.Include incl = inclusion.getValueInclusion();
    return (incl != JsonInclude.Include.ALWAYS) && (incl != JsonInclude.Include.USE_DEFAULTS);
}
 
Example 9
Source File: StdSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.8
 */
protected JsonInclude.Value findIncludeOverrides(SerializerProvider provider,
        BeanProperty prop, Class<?> typeForDefaults)
{
    if (prop != null) {
        return prop.findPropertyInclusion(provider.getConfig(), typeForDefaults);
    }
    // even without property or AnnotationIntrospector, may have type-specific defaults
    return provider.getDefaultPropertyInclusion(typeForDefaults);
}
 
Example 10
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 11
Source File: BasicSerializerFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @since 2.9
 */
protected JsonSerializer<?> buildMapEntrySerializer(SerializerProvider prov,
        JavaType type, BeanDescription beanDesc, boolean staticTyping,
        JavaType keyType, JavaType valueType)
    throws JsonMappingException
{
    // [databind#865]: Allow serialization "as POJO" -- note: to undo, declare
    //   serialization as `Shape.NATURAL` instead; that's JSON Object too.
    JsonFormat.Value formatOverride = prov.getDefaultPropertyFormat(Map.Entry.class);
    JsonFormat.Value formatFromAnnotation = beanDesc.findExpectedFormat(null);
    JsonFormat.Value format = JsonFormat.Value.merge(formatFromAnnotation, formatOverride);
    if (format.getShape() == JsonFormat.Shape.OBJECT) {
        return null;
    }
    MapEntrySerializer ser = new MapEntrySerializer(valueType, keyType, valueType,
            staticTyping, createTypeSerializer(prov.getConfig(), valueType), null);

    final JavaType contentType = ser.getContentType();
    JsonInclude.Value inclV = _findInclusionWithContent(prov, beanDesc,
            contentType, Map.Entry.class);

    // Need to support global legacy setting, for now:
    JsonInclude.Include incl = (inclV == null) ? JsonInclude.Include.USE_DEFAULTS : inclV.getContentInclusion();
    if (incl == JsonInclude.Include.USE_DEFAULTS
            || incl == JsonInclude.Include.ALWAYS) {
        return ser;
    }

    // NOTE: mostly copied from `PropertyBuilder`; would be nice to refactor
    // but code is not identical nor are these types related
    Object valueToSuppress;
    boolean suppressNulls = true; // almost always, but possibly not with CUSTOM

    switch (incl) {
    case NON_DEFAULT:
        valueToSuppress = BeanUtil.getDefaultValue(contentType);
        if (valueToSuppress != null) {
            if (valueToSuppress.getClass().isArray()) {
                valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
            }
        }
        break;
    case NON_ABSENT:
        valueToSuppress = contentType.isReferenceType()
                ? MapSerializer.MARKER_FOR_EMPTY : null;
        break;
    case NON_EMPTY:
        valueToSuppress = MapSerializer.MARKER_FOR_EMPTY;
        break;
    case CUSTOM:
        valueToSuppress = prov.includeFilterInstance(null, inclV.getContentFilter());
        if (valueToSuppress == null) { // is this legal?
            suppressNulls = true;
        } else {
            suppressNulls = prov.includeFilterSuppressNulls(valueToSuppress);
        }
        break;
    case NON_NULL:
    default: // should not matter but...
        valueToSuppress = null;
        break;
    }
    return ser.withContentInclusion(valueToSuppress, suppressNulls);
}
 
Example 12
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;
}
 
Example 13
Source File: SimpleBeanPropertyDefinition.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public JsonInclude.Value findInclusion() {
    return _inclusion;
}
 
Example 14
Source File: SerializerProvider.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @since 2.8
 */
public final JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType) {
    return _config.getDefaultPropertyInclusion();
}
 
Example 15
Source File: ConfigOverrides.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @since 2.9
 */
public void setDefaultInclusion(JsonInclude.Value v) {
    _defaultInclusion = v;
}
 
Example 16
Source File: ConfigOverrides.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public JsonInclude.Value getDefaultInclusion() {
    return _defaultInclusion;
}
 
Example 17
Source File: BeanDescription.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Method for finding annotation-indicated inclusion definition (if any);
 * possibly overriding given default value.
 *<p>
 * NOTE: does NOT use global inclusion default settings as the base, unless
 * passed as `defValue`.
 *
 * @since 2.7
 */
public abstract JsonInclude.Value findPropertyInclusion(JsonInclude.Value defValue);
 
Example 18
Source File: BeanPropertyDefinition.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Method used to check if this property has specific inclusion override
 * associated with it or not.
 * It should NOT check for any default settings (global, per-type, or
 * containing POJO settings)
 * 
 * @since 2.5
 */
public abstract JsonInclude.Value findInclusion();
 
Example 19
Source File: MutableConfigOverride.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Override inclusion setting for all properties contained in POJOs of the
 * associated type.
 *
 * @param v Inclusion setting to apply contained properties.
 */
public MutableConfigOverride setInclude(JsonInclude.Value v) {
    _include = v;
    return this;
}
 
Example 20
Source File: ConfigOverride.java    From lams with GNU General Public License v2.0 votes vote down vote up
public JsonInclude.Value getInclude() { return _include; }