Annotation Type JsonInclude
-
@Target({ANNOTATION_TYPE,METHOD,FIELD,TYPE,PARAMETER}) @Retention(RUNTIME) public @interface JsonIncludeAnnotation used to indicate when value of the annotated property (when used for a field, method or constructor parameter), or all properties of the annotated class (when used for a class), is to be serialized. Without annotation property values are always included, but by using this annotation one can specify simple exclusion rules to reduce amount of properties to write out.Note that the main inclusion criteria (one annotated with
value()) is checked on Java object level, for the annotated type, and NOT on JSON output -- so even withJsonInclude.Include.NON_NULLit is possible that JSON null values are output, if object reference in question is not `null`. An example isAtomicReferenceinstance constructed to referencenullvalue: such a value would be serialized as JSONnull, and not filtered out.To base inclusion on value of contained value(s) (like
java.util.Mapentries), you will typically also need to specifycontent()annotation; for example, specifying onlyvalue()asJsonInclude.Include.NON_EMPTYfor aMapwould excludeMaps with no entries (emptyMaps), but would includeMaps elements withnullvalues (even thoughnulls are considered "empty" values). To excludeMaps with only `null`-valued entries, you would use both annotations like so:public class Bean { @JsonInclude(value=Include.NON_EMPTY, content=Include.NON_NULL) public Map<String,String> entries; }(in which case filtering first excludesMapentries withnullvalues and then excludesMaps that have no entries left).
Similarly you could excludeMaps map entries with "empty" values, or "non-default" values (seeJsonInclude.Include.NON_EMPTYandJsonInclude.Include.NON_DEFAULTfor more details).In addition to
Maps,contentconcept is also supported for referential types (likeAtomicReference).(For Jackson 2 only) Note that `content` is NOT currently (as of Jackson 2.20) supported for arrays or
Collections; support may be added in future versions (but if so, will be configurable to allow disabling it).(For Jackson 3 only) As of Jackson 3.1.0,
content()also supportsCollections and Arrays whentools.jackson.databind.SerializationFeature#APPLY_JSON_INCLUDE_FOR_CONTAINERSfeature is enabled -- disabled by default for backward-compatibility (see [databind#5369] for more detail).- Since:
- 2.0
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description JsonInclude.IncludecontentInclusion rule to use for entries ("content") of annotatedMaps and referential types (likeAtomicReference); defaults toJsonInclude.Include.ALWAYS.java.lang.Class<?>contentFilterSpecifies type of "Filter Object" to use in casecontent()isJsonInclude.Include.CUSTOM: if so, an instance is created by callingHandlerInstantiator(ofObjectMapper), which by default simply calls zero-argument constructor of the Filter Class.JsonInclude.IncludevalueInclusion rule to use for instances (values) of types (Classes) or properties annotated; defaults toJsonInclude.Include.ALWAYS.java.lang.Class<?>valueFilterSpecifies type of "Filter Object" to use in casevalue()isJsonInclude.Include.CUSTOM: if so, an instance is created by callingHandlerInstantiator(ofObjectMapper), which by default simply calls zero-argument constructor of the Filter Class.
-
-
-
Element Detail
-
value
JsonInclude.Include value
Inclusion rule to use for instances (values) of types (Classes) or properties annotated; defaults toJsonInclude.Include.ALWAYS.- Returns:
- Inclusion rule for value itself
- Default:
- com.fasterxml.jackson.annotation.JsonInclude.Include.ALWAYS
-
-
-
content
JsonInclude.Include content
Inclusion rule to use for entries ("content") of annotatedMaps and referential types (likeAtomicReference); defaults toJsonInclude.Include.ALWAYS.- Returns:
- Inclusion rule for content (elements, values of structured types)
- Since:
- 2.5
- Default:
- com.fasterxml.jackson.annotation.JsonInclude.Include.ALWAYS
-
-
-
valueFilter
java.lang.Class<?> valueFilter
Specifies type of "Filter Object" to use in casevalue()isJsonInclude.Include.CUSTOM: if so, an instance is created by callingHandlerInstantiator(ofObjectMapper), which by default simply calls zero-argument constructor of the Filter Class.Whether the value is to be included or not is determined by calling Filter's
equals(value)method: if it returnstruevalue is NOT included (it is "filtered out"); iffalsevalue IS included ("not filtered out").- Since:
- 2.9
- Default:
- java.lang.Void.class
-
-
-
contentFilter
java.lang.Class<?> contentFilter
Specifies type of "Filter Object" to use in casecontent()isJsonInclude.Include.CUSTOM: if so, an instance is created by callingHandlerInstantiator(ofObjectMapper), which by default simply calls zero-argument constructor of the Filter Class.Whether the content value is to be included or not is determined by calling Filter's
equals(value)method: if it returnstruecontent value is NOT included (it is "filtered out"); iffalsecontent value IS included ("not filtered out").- Since:
- 2.9
- Default:
- java.lang.Void.class
-
-