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

The following examples show how to use com.fasterxml.jackson.annotation.JsonInclude#Include . 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: JacksonResourceFieldInformationProvider.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<JsonIncludeStrategy> getJsonIncludeStrategy(BeanAttributeInformation attributeDesc) {
	Optional<JsonInclude> includeAnnotation = attributeDesc.getAnnotation(JsonInclude.class);
	if (includeAnnotation.isPresent()) {
		JsonInclude.Include value = includeAnnotation.get().value();
		JsonIncludeStrategy strategy;
		if (NON_NULL.equals(value)) {
			strategy = JsonIncludeStrategy.NOT_NULL;
		}
		else if (JsonInclude.Include.NON_EMPTY.equals(value)) {
			strategy = JsonIncludeStrategy.NON_EMPTY;
		}
		else {
			strategy = JsonIncludeStrategy.DEFAULT;
		}
		return Optional.of(strategy);
	}
	return Optional.empty();
}
 
Example 2
Source File: JsonMapper.java    From Moss with Apache License 2.0 5 votes vote down vote up
public JsonMapper(JsonInclude.Include include) {
    mapper = new ObjectMapper();

    if (include != null) {
        mapper.setSerializationInclusion(include);
    }

    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
 
Example 3
Source File: AnnotationIntrospectorPair.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated
@Override
public JsonInclude.Include findSerializationInclusionForContent(Annotated a, JsonInclude.Include defValue)
{
    // note: call secondary first, to give lower priority
    defValue = _secondary.findSerializationInclusionForContent(a, defValue);
    defValue = _primary.findSerializationInclusionForContent(a, defValue);
    return defValue;
}
 
Example 4
Source File: SimpleBeanPropertyDefinition.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method called to create instance for virtual properties.
 *
 * @since 2.5
 */
public static SimpleBeanPropertyDefinition construct(MapperConfig<?> config,
        AnnotatedMember member, PropertyName name, PropertyMetadata metadata,
        JsonInclude.Include inclusion)
{
    JsonInclude.Value inclValue
         = ((inclusion == null) || (inclusion == JsonInclude.Include.USE_DEFAULTS)) 
         ? EMPTY_INCLUDE : JsonInclude.Value.construct(inclusion, null);
    return new SimpleBeanPropertyDefinition(config.getAnnotationIntrospector(),
            member, name, metadata, inclValue);
}
 
Example 5
Source File: VirtualBeanPropertyWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected static Object _suppressableValue(JsonInclude.Value inclusion) {
    if (inclusion == null) {
        return false;
    }
    JsonInclude.Include incl = inclusion.getValueInclusion();
    if ((incl == JsonInclude.Include.ALWAYS)
            || (incl == JsonInclude.Include.NON_NULL)
            || (incl == JsonInclude.Include.USE_DEFAULTS)) {
        return null;
    }
    return MARKER_FOR_EMPTY;
}
 
Example 6
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 7
Source File: JsonMapper.java    From mini-platform with MIT License 5 votes vote down vote up
public JsonMapper(JsonInclude.Include include) {
    mapper = new ObjectMapper();
    //设置输出时包含属性的风格
    if (include != null) {
        mapper.setSerializationInclusion(include);
    }
    //设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    //
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, false);
}
 
Example 8
Source File: JsonTools.java    From pampas with Apache License 2.0 5 votes vote down vote up
public JsonTools(JsonInclude.Include include) {
    mapper = new ObjectMapper();
    // 设置输出时包含属性的风格
    if (include != null) {
        mapper.setSerializationInclusion(include);
    }
    // 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    SimpleModule javaTimeModule = new SimpleModule();
    javaTimeModule.addSerializer(Date.class, new JsonDateSerializer());
    javaTimeModule.addDeserializer(Date.class, new JsonDateDeserializer());
    mapper.registerModule(javaTimeModule);
}
 
Example 9
Source File: JsonHelper.java    From demo-project with MIT License 4 votes vote down vote up
public JsonHelper(JsonInclude.Include include) {
	mapper = new ObjectMapper();
	mapper.setSerializationInclusion(include);
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
 
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: 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 12
Source File: BasicSerializerFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Helper method that does figures out content inclusion value to use, if any,
 * and construct re-configured {@link MapSerializer} appropriately.
 *
 * @since 2.9
 */
@SuppressWarnings("deprecation")
protected MapSerializer _checkMapContentInclusion(SerializerProvider prov,
        BeanDescription beanDesc, MapSerializer mapSer)
    throws JsonMappingException
{
    final JavaType contentType = mapSer.getContentType();
    JsonInclude.Value inclV = _findInclusionWithContent(prov, beanDesc,
            contentType, Map.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) {
        if (!prov.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES)) {
            return mapSer.withContentInclusion(null, true);
        }
        return mapSer;
    }

    // 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: // new with 2.6, to support Guava/JDK8 Optionals
        // and for referential types, also "empty", which in their case means "absent"
        valueToSuppress = contentType.isReferenceType()
                ? MapSerializer.MARKER_FOR_EMPTY : null;
        break;
    case NON_EMPTY:
        valueToSuppress = MapSerializer.MARKER_FOR_EMPTY;
        break;
    case CUSTOM: // new with 2.9
        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 mapSer.withContentInclusion(valueToSuppress, suppressNulls);
}
 
Example 13
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 14
Source File: BasicSerializerFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected JsonSerializer<?> buildAtomicReferenceSerializer(SerializerProvider prov,
        ReferenceType refType, BeanDescription beanDesc, boolean staticTyping,
        TypeSerializer contentTypeSerializer, JsonSerializer<Object> contentSerializer)
    throws JsonMappingException
{
    final JavaType contentType = refType.getReferencedType();
    JsonInclude.Value inclV = _findInclusionWithContent(prov, beanDesc,
            contentType, AtomicReference.class);
    
    // Need to support global legacy setting, for now:
    JsonInclude.Include incl = (inclV == null) ? JsonInclude.Include.USE_DEFAULTS : inclV.getContentInclusion();
    Object valueToSuppress;
    boolean suppressNulls;

    if (incl == JsonInclude.Include.USE_DEFAULTS
            || incl == JsonInclude.Include.ALWAYS) {
        valueToSuppress = null;
        suppressNulls = false;
    } else {
        suppressNulls = true;
        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;
        }
    }
    AtomicReferenceSerializer ser = new AtomicReferenceSerializer(refType, staticTyping,
            contentTypeSerializer, contentSerializer);
    return ser.withContentInclusion(valueToSuppress, suppressNulls);
}
 
Example 15
Source File: JacksonDsl.java    From spring-fu with Apache License 2.0 4 votes vote down vote up
/**
 * Set the default property inclusion
 */
public JacksonDsl defaultPropertyInclusion(JsonInclude.Include include) {
	this.properties.setDefaultPropertyInclusion(include);
	return this;
}
 
Example 16
Source File: JsonSerializerParameters.java    From domino-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <p>getInclude.</p>
 *
 * @return a {@link com.fasterxml.jackson.annotation.JsonInclude.Include} object.
 */
JsonInclude.Include getInclude();
 
Example 17
Source File: JsonSerializerParameters.java    From domino-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <p>setInclude.</p>
 *
 * @param include a {@link com.fasterxml.jackson.annotation.JsonInclude.Include} object.
 * @return a {@link org.dominokit.jacksonapt.JsonSerializerParameters} object.
 */
JsonSerializerParameters setInclude(JsonInclude.Include include);
 
Example 18
Source File: Jackson2ObjectMapperFactoryBean.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Set a custom inclusion strategy for serialization.
 * @see com.fasterxml.jackson.annotation.JsonInclude.Include
 */
public void setSerializationInclusion(JsonInclude.Include serializationInclusion) {
	this.builder.serializationInclusion(serializationInclusion);
}
 
Example 19
Source File: Jackson2ObjectMapperFactoryBean.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Set a custom inclusion strategy for serialization.
 * @see com.fasterxml.jackson.annotation.JsonInclude.Include
 */
public void setSerializationInclusion(JsonInclude.Include serializationInclusion) {
	this.builder.serializationInclusion(serializationInclusion);
}
 
Example 20
Source File: Jackson2ObjectMapperFactoryBean.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Set a custom inclusion strategy for serialization.
 * @see com.fasterxml.jackson.annotation.JsonInclude.Include
 */
public void setSerializationInclusion(JsonInclude.Include serializationInclusion) {
	this.builder.serializationInclusion(serializationInclusion);
}