Skip to content

Commit 8e6d0e4

Browse files
authored
Add JsonInclude.Value convenience constants (#314)
1 parent b4cfad2 commit 8e6d0e4

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ NOTE: Jackson 3.x components rely on 2.x annotations; there are no separate
1616

1717
2.21 (not yet released)
1818

19-
No changes since 2.20
19+
#314: Add `JsonInclude.Value` convenience constants
20+
(contributed by @runeflobakk)
2021

2122
2.20 (28-Aug-2025)
2223

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,84 @@ public static class Value
268268
{
269269
private static final long serialVersionUID = 1L;
270270

271+
/**
272+
* Constant that indicates that property is to be always included,
273+
* independent of value of the property.
274+
* <p>
275+
* This will specify the same setting for including a value both
276+
* on <b>Java object level</b> as well as when <b>contained</b>
277+
* in an object reference (see {@link JsonInclude} for further
278+
* details on this distinction).
279+
*
280+
* @since 2.21
281+
*/
282+
public final static Value ALL_ALWAYS = Value
283+
.construct(Include.ALWAYS, Include.ALWAYS);
284+
285+
/**
286+
* Constant that indicates that only properties with non-null
287+
* values are to be included.
288+
* <p>
289+
* This will specify the same setting for including a value both
290+
* on <b>Java object level</b> as well as when <b>contained</b>
291+
* in an object reference (see {@link JsonInclude} for further
292+
* details on this distinction).
293+
*
294+
* @since 2.21
295+
*/
296+
public final static Value ALL_NON_NULL = Value
297+
.construct(Include.NON_NULL, Include.NON_NULL);
298+
299+
/**
300+
* Constant that indicates that properties are included unless their value
301+
* is:
302+
* <ul>
303+
* <li>null</li>
304+
* <li>"absent" value of a referential type (like Java 8 `Optional`, or
305+
* {@link java.util.concurrent.atomic.AtomicReference}); that is, something
306+
* that would not deference to a non-null value.
307+
* </ul>
308+
* This option is mostly used to work with "Optional"s (Java 8, Guava).
309+
* <p>
310+
* This will specify the same setting for including a value both
311+
* on <b>Java object level</b> as well as when <b>contained</b>
312+
* in an object reference (see {@link JsonInclude} for further
313+
* details on this distinction).
314+
*
315+
* @since 2.21
316+
*/
317+
public final static Value ALL_NON_ABSENT = Value
318+
.construct(Include.NON_ABSENT, Include.NON_ABSENT);
319+
320+
/**
321+
* Constant that indicates that only properties with null value,
322+
* or what is considered empty, are not to be included.
323+
* See {@link Include#NON_EMPTY} for further details.
324+
* <p>
325+
* This will specify the same setting for including a value both
326+
* on <b>Java object level</b> as well as when <b>contained</b>
327+
* in an object reference (see {@link JsonInclude} for further
328+
* details on this distinction).
329+
*
330+
* @since 2.21
331+
*/
332+
public final static Value ALL_NON_EMPTY = Value
333+
.construct(Include.NON_EMPTY, Include.NON_EMPTY);
334+
335+
/**
336+
* The equivalent to {@link Include#NON_DEFAULT} for specifying
337+
* inclusion of non-defaults for both values and content.
338+
* <p>
339+
* This will specify the same setting for including a value both
340+
* on <b>Java object level</b> as well as when <b>contained</b>
341+
* in an object reference (see {@link JsonInclude} for further
342+
* details on this distinction).
343+
*
344+
* @since 2.21
345+
*/
346+
public final static Value ALL_NON_DEFAULT = Value
347+
.construct(Include.NON_DEFAULT, Include.NON_DEFAULT);
348+
271349
protected final static Value EMPTY = new Value(Include.USE_DEFAULTS,
272350
Include.USE_DEFAULTS, null, null);
273351

0 commit comments

Comments
 (0)