Skip to content

Commit 4595936

Browse files
committed
Start work on supporting exclusion of "absent" Optional values
1 parent 36c8d5d commit 4595936

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ NOTE: Annotations module will never contain changes in patch versions,
1515

1616
#56: Improve `ObjectIdGenerators.key()` to handle `null` appropriately by returning `null`
1717
#58: Add new properties for `@JsonIgnoreProperties`, "allowGetters", "allowSetters"
18+
- Add `JsonInclude.Include.NON_ABSENT` value, for excluding "absent" Optional values.
1819

1920
2.5.0 (01-Jan-2015)
2021

src/main/java/com/fasterxml/jackson/annotation/JsonInclude.java

+25-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Note that inclusion criteria is checked on <b>Java object level</b>
1717
* and <b>NOT</b> on JSON output -- so even with {@link Include#NON_NULL}
1818
* it is possible that JSON null values are output, if object reference
19-
* in question is not `null`. An example is {@link java.util.concurrent.AtomicReference}
19+
* in question is not `null`. An example is {@link java.util.concurrent.atomic.AtomicReference}
2020
* instance constructed to reference <code>null</code> value: such a value
2121
* would be serialized as JSON null, and not filtered out.
2222
* In such cases {@link Include#NON_EMPTY} should be used instead, since missing
@@ -75,14 +75,19 @@ public enum Include
7575
NON_NULL,
7676

7777
/**
78-
* Value that indicates that only properties that have values
79-
* that differ from default settings (meaning values they have
80-
* when Bean is constructed with its no-arguments constructor)
81-
* are to be included. Value is generally not useful with
82-
* {@link java.util.Map}s, since they have no default values;
83-
* and if used, works same as {@link #ALWAYS}.
78+
* Value that indicates that properties are included unless their value
79+
* is:
80+
*<ul>
81+
* <li>null</li>
82+
* <li>"absent" value of a referential type (like Java 8 `Optional`, or
83+
* {link java.utl.concurrent.atomic.AtomicReference}); that is, something
84+
* that would not deference to a non-null value.
85+
* </ul>
86+
* This option is mostly used to work with "Optional"s (Java 8, Guava).
87+
*
88+
* @since 2.6
8489
*/
85-
NON_DEFAULT,
90+
NON_ABSENT,
8691

8792
/**
8893
* Value that indicates that only properties that have values
@@ -113,8 +118,18 @@ public enum Include
113118
* is overridden, it will be called to see if non-null values are
114119
* considered empty (null is always considered empty).
115120
*/
116-
NON_EMPTY
121+
NON_EMPTY,
122+
123+
/**
124+
* Value that indicates that only properties that have values
125+
* that differ from default settings (meaning values they have
126+
* when Bean is constructed with its no-arguments constructor)
127+
* are to be included. Value is generally not useful with
128+
* {@link java.util.Map}s, since they have no default values;
129+
* and if used, works same as {@link #ALWAYS}.
130+
*/
131+
NON_DEFAULT
132+
117133
;
118134
}
119-
120135
}

0 commit comments

Comments
 (0)