-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathJsonInclude.java
More file actions
641 lines (591 loc) · 25 KB
/
JsonInclude.java
File metadata and controls
641 lines (591 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
package com.fasterxml.jackson.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation 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.
*<p>
* Note that the main inclusion criteria (one annotated with {@link #value})
* is checked on <b>Java object level</b>, for the annotated type,
* and <b>NOT</b> on JSON output -- so even with {@link Include#NON_NULL}
* it is possible that JSON null values are output, if object reference
* in question is not `null`. An example is {@link java.util.concurrent.atomic.AtomicReference}
* instance constructed to reference <code>null</code> value: such a value
* would be serialized as JSON {@code null}, and not filtered out.
*<p>
* To base inclusion on value of <b>contained</b> value(s) (like {@code java.util.Map}
* entries), you will typically also need
* to specify {@link #content()} annotation; for example, specifying only
* {@link #value} as {@link Include#NON_EMPTY} for a {@link java.util.Map} would
* exclude {@link java.util.Map}s with no entries (empty {@code Map}s),
* but would include <code>Map</code>s elements with {@code null} values (even
* though {@code null}s are considered "empty" values).
* To exclude {@code Map}s with only `null`-valued entries,
* you would use both annotations like so:
*<pre>
*public class Bean {
* {@literal @JsonInclude}(value=Include.NON_EMPTY, content=Include.NON_NULL)
* public Map<String,String> entries;
*}
*</pre>
* (in which case filtering first excludes {@code Map} entries with {@code null} values
* and then excludes {@code Map}s that have no entries left).
* <br>
* Similarly you could exclude <code>Map</code>s map entries with
* "empty" values, or "non-default" values (see {@link Include#NON_EMPTY} and
* {@link Include#NON_DEFAULT} for more details).
*<p>
* In addition to {@code Map}s, {@code content} concept is also supported for referential
* types (like {@link java.util.concurrent.atomic.AtomicReference}).
*<p>
* (For Jackson 2 only) Note that `content` is NOT currently (as of Jackson 2.20) supported for
* arrays or {@link java.util.Collection}s; support may be added in
* future versions (but if so, will be configurable to allow disabling it).
*<p>
* (For Jackson 3 only) As of Jackson 3.1.0, {@link #content()} also supports {@link java.util.Collection}s
* and Arrays when {@code tools.jackson.databind.SerializationFeature#APPLY_JSON_INCLUDE_FOR_CONTAINERS}
* feature is enabled -- disabled by default for backward-compatibility (see [databind#5369] for more detail).
*
* @since 2.0
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD,
ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonInclude
{
/**
* Inclusion rule to use for instances (values) of types (Classes) or
* properties annotated; defaults to {@link Include#ALWAYS}.
*
* @return Inclusion rule for value itself
*/
public Include value() default Include.ALWAYS;
/**
* Inclusion rule to use for entries ("content") of annotated
* {@link java.util.Map}s and referential types (like
* {@link java.util.concurrent.atomic.AtomicReference});
* defaults to {@link Include#ALWAYS}.
*
* @return Inclusion rule for content (elements, values of structured types)
*
* @since 2.5
*/
public Include content() default Include.ALWAYS;
/**
* Specifies type of "Filter Object" to use in case
* {@link #value} is {@link JsonInclude.Include#CUSTOM}:
* if so, an instance is created by calling {@code HandlerInstantiator}
* (of {@code ObjectMapper}), which by default simply calls
* zero-argument constructor of the Filter Class.
*<p>
* Whether the value is to be included or not is determined by calling
* Filter's {@code equals(value)} method: if it returns {@code true}
* value is NOT included (it is "filtered out"); if {@code false} value
* IS included ("not filtered out").
*
* @since 2.9
*/
public Class<?> valueFilter() default Void.class;
/**
* Specifies type of "Filter Object" to use in case
* {@link #content} is {@link JsonInclude.Include#CUSTOM}:
* if so, an instance is created by calling {@code HandlerInstantiator}
* (of {@code ObjectMapper}), which by default simply calls
* zero-argument constructor of the Filter Class.
*<p>
* Whether the content value is to be included or not is determined by calling
* Filter's {@code equals(value)} method: if it returns {@code true}
* content value is NOT included (it is "filtered out"); if {@code false} content value
* IS included ("not filtered out").
*
* @since 2.9
*/
public Class<?> contentFilter() default Void.class;
/*
/**********************************************************************
/* Value enumerations
/**********************************************************************
*/
/**
* Enumeration used with {@link JsonInclude}
* to define which properties
* of Java Beans are to be included in serialization.
*/
public enum Include
{
/**
* Value that indicates that property is to be always included,
* independent of value of the property.
*/
ALWAYS,
/**
* Value that indicates that only properties with non-null
* values are to be included.
*/
NON_NULL,
/**
* Value that indicates that properties are included unless their value
* is:
*<ul>
* <li>null</li>
* <li>"absent" value of a referential type (like Java 8 `Optional`, or
* {@link java.util.concurrent.atomic.AtomicReference}); that is, something
* that would not deference to a non-null value.
* </ul>
* This option is mostly used to work with "Optional"s (Java 8, Guava).
*
* @since 2.6
*/
NON_ABSENT,
/**
* Value that indicates that only properties with null value,
* or what is considered empty, are not to be included.
* Definition of emptiness is data type specific; see below
* for details on actual handling.
*<p>
* Default emptiness for all types includes:
*<ul>
* <li><code>Null</code> values.</li>
* <li>"Absent" values (see {@link #NON_ABSENT})</li>
*</ul>
* so that as baseline, "empty" set includes values that would be
* excluded by both {@link #NON_NULL} and {@link #NON_ABSENT}.
*<br>
* Beyond this base, following types have additional empty values:
*<ul>
* <li>For {@link java.util.Collection}s and {@link java.util.Map}s,
* method <code>isEmpty()</code> is called;
* </li>
* <li>For Java arrays, empty arrays are ones with length of 0
* </li>
* <li>For Java {@link java.lang.String}s, <code>length()</code> is called,
* and return value of 0 indicates empty String
* </li>
* </ul>
* and for other types, null values are excluded but other exclusions (if any).
*<p>
* Note that this default handling can be overridden by custom
* <code>JsonSerializer</code> (2.x) / <code>ValueSerializer</code> (3.x)
* implementation: if method <code>isEmpty()</code>
* is overridden, it will be called to see if non-null values are
* considered empty (null is always considered empty).
*<p>
* Compatibility note: Jackson 2.6 included a wider range of "empty" values than
* either earlier (up to 2.5) or later (2.7 and beyond) types; specifically:
*<ul>
* <li>Default values of primitive types (like <code>0</code> for `int`/`java.lang.Integer`
* and `false` for `bool`/`Boolean`)
* </li>
* <li>Timestamp 0 for date/time types
* </li>
*</ul>
* With 2.7, definition has been tightened back to only containing types explained
* above (null, absent, empty String, empty containers), and now
* extended definition may be specified using {@link #NON_DEFAULT}.
*/
NON_EMPTY,
/**
* Meaning of this setting depends on context: whether annotation is
* specified for POJO type (class), or not. In latter case annotation
* is either used as the global default, or as property override.
*<p>
* When used for a POJO, definition is that only values that differ from
* the default values of POJO properties are included. This is done
* by creating an instance of POJO using zero-argument constructor,
* and accessing property values: value is used as the default value
* by using <code>equals()</code> method, except for the case where property
* has `null` value in which case straight null check is used.
*<p>
* When NOT used for a POJO (that is, as a global default, or as property
* override), definition is such that:
*<ul>
* <li>All values considered "empty" (as per {@link #NON_EMPTY}) are excluded</li>
* <li>Primitive/wrapper default values are excluded</li>
* <li>Date/time values that have timestamp (`long` value of milliseconds since
* epoch, see {@link java.util.Date}) of `0L` are excluded</li>
* </ul>
*<p>
* NOTE: Jackson 3.2 has more configurability (via new {@code MapperFeature})
* to configure applicability of this setting for global default inclusion.
*/
NON_DEFAULT,
/**
* Value that indicates that separate {@code filter} Object (specified by
* {@link JsonInclude#valueFilter} for value itself, and/or
* {@link JsonInclude#contentFilter} for contents of structured types)
* is to be used for determining inclusion criteria.
* Filter object's {@code equals(value)} method is called with value
* to serialize; if it returns {@code true} value is <b>excluded</b>
* (that is, filtered out); if {@code false} value is <b>included</b>.
*<p>
* NOTE: the filter will be called for each non-{@code null} value,
* but handling of {@code null} value differs: up to Jackson 2.13, call
* was only made once, but with 2.14 and later filter will be called
* once for each {@code null} value too.
*
* @since 2.9
*/
CUSTOM,
/**
* Pseudo-value used to indicate that the higher-level defaults make
* sense, to avoid overriding inclusion value. For example, if returned
* for a property this would use defaults for the class that contains
* property, if any defined; and if none defined for that, then
* global serialization inclusion details.
*
* @since 2.6
*/
USE_DEFAULTS
;
}
/*
/**********************************************************************
/* Value class used to enclose information
/**********************************************************************
*/
/**
* Helper class used to contain information from a single {@link JsonInclude}
* annotation.
*
* @since 2.6
*/
public static class Value
implements JacksonAnnotationValue<JsonInclude>, // since 2.6
java.io.Serializable
{
private static final long serialVersionUID = 1L;
/**
* Constant that indicates that property is to be always included,
* independent of value of the property.
* <p>
* This will specify the same setting for including a value both
* on <b>Java object level</b> as well as when <b>contained</b>
* in an object reference (see {@link JsonInclude} for further
* details on this distinction).
*
* @since 2.21
*/
public final static Value ALL_ALWAYS = Value
.construct(Include.ALWAYS, Include.ALWAYS);
/**
* Constant that indicates that only properties with non-null
* values are to be included.
* <p>
* This will specify the same setting for including a value both
* on <b>Java object level</b> as well as when <b>contained</b>
* in an object reference (see {@link JsonInclude} for further
* details on this distinction).
*
* @since 2.21
*/
public final static Value ALL_NON_NULL = Value
.construct(Include.NON_NULL, Include.NON_NULL);
/**
* Constant that indicates that properties are included unless their value
* is:
* <ul>
* <li>null</li>
* <li>"absent" value of a referential type (like Java 8 `Optional`, or
* {@link java.util.concurrent.atomic.AtomicReference}); that is, something
* that would not deference to a non-null value.
* </ul>
* This option is mostly used to work with "Optional"s (Java 8, Guava).
* <p>
* This will specify the same setting for including a value both
* on <b>Java object level</b> as well as when <b>contained</b>
* in an object reference (see {@link JsonInclude} for further
* details on this distinction).
*
* @since 2.21
*/
public final static Value ALL_NON_ABSENT = Value
.construct(Include.NON_ABSENT, Include.NON_ABSENT);
/**
* Constant that indicates that only properties with null value,
* or what is considered empty, are not to be included.
* See {@link Include#NON_EMPTY} for further details.
* <p>
* This will specify the same setting for including a value both
* on <b>Java object level</b> as well as when <b>contained</b>
* in an object reference (see {@link JsonInclude} for further
* details on this distinction).
*
* @since 2.21
*/
public final static Value ALL_NON_EMPTY = Value
.construct(Include.NON_EMPTY, Include.NON_EMPTY);
/**
* The equivalent to {@link Include#NON_DEFAULT} for specifying
* inclusion of non-defaults for both values and content.
* <p>
* This will specify the same setting for including a value both
* on <b>Java object level</b> as well as when <b>contained</b>
* in an object reference (see {@link JsonInclude} for further
* details on this distinction).
*
* @since 2.21
*/
public final static Value ALL_NON_DEFAULT = Value
.construct(Include.NON_DEFAULT, Include.NON_DEFAULT);
protected final static Value EMPTY = new Value(Include.USE_DEFAULTS,
Include.USE_DEFAULTS, null, null);
protected final Include _valueInclusion;
protected final Include _contentInclusion;
/**
* @since 2.9
*/
protected final Class<?> _valueFilter;
/**
* @since 2.9
*/
protected final Class<?> _contentFilter;
public Value(JsonInclude src) {
this(src.value(), src.content(),
src.valueFilter(), src.contentFilter());
}
protected Value(Include vi, Include ci,
Class<?> valueFilter, Class<?> contentFilter) {
_valueInclusion = (vi == null) ? Include.USE_DEFAULTS : vi;
_contentInclusion = (ci == null) ? Include.USE_DEFAULTS : ci;
_valueFilter = (valueFilter == Void.class) ? null : valueFilter;
_contentFilter = (contentFilter == Void.class) ? null : contentFilter;
}
public static Value empty() {
return EMPTY;
}
/**
* Helper method that will try to combine values from two {@link Value}
* instances, using one as base settings, and the other as overrides
* to use instead of base values when defined; base values are only
* use if override does not specify a value (matching value is null
* or logically missing).
* Note that one or both of value instances may be `null`, directly;
* if both are `null`, result will also be `null`; otherwise never null.
*
* @since 2.8
*/
public static Value merge(Value base, Value overrides)
{
return (base == null) ? overrides
: base.withOverrides(overrides);
}
/**
* @since 2.8
*/
public static Value mergeAll(Value... values)
{
Value result = null;
for (Value curr : values) {
if (curr != null) {
result = (result == null) ? curr : result.withOverrides(curr);
}
}
return result;
}
// for JDK serialization
protected Object readResolve() {
if ((_valueInclusion == Include.USE_DEFAULTS)
&& (_contentInclusion == Include.USE_DEFAULTS)
&& (_valueFilter == null)
&& (_contentFilter == null)
) {
return EMPTY;
}
return this;
}
/**
* Mutant factory method that merges values of this value with given override
* values, so that any explicitly defined inclusion in overrides has precedence over
* settings of this value instance. If no overrides exist will return <code>this</code>
* instance; otherwise new {@link Value} with changed inclusion values.
*/
public Value withOverrides(Value overrides) {
if ((overrides == null) || (overrides == EMPTY)) {
return this;
}
Include vi = overrides._valueInclusion;
Include ci = overrides._contentInclusion;
Class<?> vf = overrides._valueFilter;
Class<?> cf = overrides._contentFilter;
boolean viDiff = (vi != _valueInclusion) && (vi != Include.USE_DEFAULTS);
boolean ciDiff = (ci != _contentInclusion) && (ci != Include.USE_DEFAULTS);
boolean filterDiff = (vf != _valueFilter) || (cf != _contentFilter);
if (viDiff) {
if (ciDiff) {
return new Value(vi, ci, vf, cf);
}
return new Value(vi, _contentInclusion, vf, cf);
} else if (ciDiff) {
return new Value(_valueInclusion, ci, vf, cf);
} else if (filterDiff) {
return new Value(_valueInclusion, _contentInclusion, vf, cf);
}
return this;
}
/**
* Factory method to use for constructing an instance for components
*/
public static Value construct(Include valueIncl, Include contentIncl) {
if (((valueIncl == Include.USE_DEFAULTS) || (valueIncl == null))
&& ((contentIncl == Include.USE_DEFAULTS) || (contentIncl == null))) {
return EMPTY;
}
return new Value(valueIncl, contentIncl, null, null);
}
/**
* Factory method to use for constructing an instance for components
*
* @since 2.9
*/
public static Value construct(Include valueIncl, Include contentIncl,
Class<?> valueFilter, Class<?> contentFilter)
{
if (valueFilter == Void.class) {
valueFilter = null;
}
if (contentFilter == Void.class) {
contentFilter = null;
}
if (((valueIncl == Include.USE_DEFAULTS) || (valueIncl == null))
&& ((contentIncl == Include.USE_DEFAULTS) || (contentIncl == null))
&& (valueFilter == null)
&& (contentFilter == null)
) {
return EMPTY;
}
return new Value(valueIncl, contentIncl, valueFilter, contentFilter);
}
/**
* Factory method to use for constructing an instance from instance of
* {@link JsonInclude}
*/
public static Value from(JsonInclude src) {
if (src == null) {
return EMPTY;
}
Include vi = src.value();
Include ci = src.content();
if ((vi == Include.USE_DEFAULTS) && (ci == Include.USE_DEFAULTS)) {
return EMPTY;
}
Class<?> vf = src.valueFilter();
if (vf == Void.class) {
vf = null;
}
Class<?> cf = src.contentFilter();
if (cf == Void.class) {
cf = null;
}
return new Value(vi, ci, vf, cf);
}
public Value withValueInclusion(Include incl) {
return (incl == _valueInclusion) ? this
: new Value(incl, _contentInclusion, _valueFilter, _contentFilter);
}
/**
* Mutant factory that will either
*<ul>
* <li>Set <code>value</code> as <code>USE_DEFAULTS</code>
* and <code>valueFilter</code> to <code>filter</code> (if filter not null);
* or</li>
* <li>Set <code>value</code> as <code>ALWAYS</code> (if filter null)
* </li>
* </ul>
*
* @since 2.9
*/
public Value withValueFilter(Class<?> filter) {
Include incl;
if (filter == null || filter == Void.class) { // clear filter
incl = Include.USE_DEFAULTS;
filter = null;
} else {
incl = Include.CUSTOM;
}
return construct(incl, _contentInclusion, filter, _contentFilter);
}
/**
* Mutant factory that will either
*<ul>
* <li>Set <code>content</code> as <code>USE_DEFAULTS</code>
* and <code>contentFilter</code> to <code>filter</code> (if filter not null);
* or</li>
* <li>Set <code>content</code> as <code>ALWAYS</code> (if filter null)
* </li>
* </ul>
*
* @since 2.9
*/
public Value withContentFilter(Class<?> filter) {
Include incl;
if (filter == null || filter == Void.class) { // clear filter
incl = Include.USE_DEFAULTS;
filter = null;
} else {
incl = Include.CUSTOM;
}
return construct(_valueInclusion, incl, _valueFilter, filter);
}
public Value withContentInclusion(Include incl) {
return (incl == _contentInclusion) ? this
: new Value(_valueInclusion, incl, _valueFilter, _contentFilter);
}
@Override
public Class<JsonInclude> valueFor() {
return JsonInclude.class;
}
public Include getValueInclusion() {
return _valueInclusion;
}
public Include getContentInclusion() {
return _contentInclusion;
}
public Class<?> getValueFilter() {
return _valueFilter;
}
public Class<?> getContentFilter() {
return _contentFilter;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(80);
sb.append("JsonInclude.Value(value=")
.append(_valueInclusion)
.append(",content=")
.append(_contentInclusion);
if (_valueFilter != null) {
sb.append(",valueFilter=").append(_valueFilter.getName()).append(".class");
}
if (_contentFilter != null) {
sb.append(",contentFilter=").append(_contentFilter.getName()).append(".class");
}
return sb.append(')').toString();
}
@Override
public int hashCode() {
return (_valueInclusion.hashCode() << 2)
+ _contentInclusion.hashCode()
+ ((_valueFilter == null) ? 0 : _valueFilter.hashCode())
+ ((_contentFilter == null) ? 0 : _contentFilter.hashCode());
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != getClass()) return false;
Value other = (Value) o;
return (other._valueInclusion == _valueInclusion)
&& (other._contentInclusion == _contentInclusion)
&& (other._valueFilter == _valueFilter)
&& (other._contentFilter == _contentFilter)
;
}
}
}