@@ -16,17 +16,10 @@ public class PropertyBuilder
16
16
{
17
17
// @since 2.7
18
18
private final static Object NO_DEFAULT_MARKER = Boolean .FALSE ;
19
-
19
+
20
20
final protected SerializationConfig _config ;
21
21
final protected BeanDescription _beanDesc ;
22
22
23
- /**
24
- * Default inclusion mode for properties of the POJO for which
25
- * properties are collected; possibly overridden on
26
- * per-property basis.
27
- */
28
- final protected JsonInclude .Value _defaultInclusion ;
29
-
30
23
final protected AnnotationIntrospector _annotationIntrospector ;
31
24
32
25
/**
@@ -40,14 +33,41 @@ public class PropertyBuilder
40
33
*/
41
34
protected Object _defaultBean ;
42
35
36
+ /**
37
+ * Default inclusion mode for properties of the POJO for which
38
+ * properties are collected; possibly overridden on
39
+ * per-property basis. Combines global inclusion defaults and
40
+ * per-type (annotation and type-override) inclusion overrides.
41
+ */
42
+ final protected JsonInclude .Value _defaultInclusion ;
43
+
44
+ /**
45
+ * Marker flag used to indicate that "real" default values are to be used
46
+ * for properties, as per per-type value inclusion of type <code>NON_DEFAULT</code>
47
+ *
48
+ * @since 2.8
49
+ */
50
+ final protected boolean _useRealPropertyDefaults ;
51
+
43
52
public PropertyBuilder (SerializationConfig config , BeanDescription beanDesc )
44
53
{
45
54
_config = config ;
46
55
_beanDesc = beanDesc ;
47
- // NOTE: this includes global defaults and defaults of POJO that contains property,
48
- // but not defaults for types of properties referenced
49
- _defaultInclusion = beanDesc .findPropertyInclusion (
50
- config .getDefaultPropertyInclusion (beanDesc .getBeanClass ()));
56
+ // 08-Sep-2016, tatu: This gets tricky, with 3 levels of definitions:
57
+ // (a) global default inclusion
58
+ // (b) per-type default inclusion (from annotation or config overrides;
59
+ // latter having precedence
60
+ // Cc) per-property override
61
+ //
62
+ // and not only requiring merging, but also considering special handling
63
+ // for NON_DEFAULT in case of (b) (vs (a) or (c))
64
+ JsonInclude .Value inclPerType = JsonInclude .Value .merge (
65
+ beanDesc .findPropertyInclusion (JsonInclude .Value .empty ()),
66
+ config .getDefaultPropertyInclusion (beanDesc .getBeanClass (),
67
+ JsonInclude .Value .empty ()));
68
+ _defaultInclusion = JsonInclude .Value .merge (config .getDefaultPropertyInclusion (),
69
+ inclPerType );
70
+ _useRealPropertyDefaults = inclPerType .getValueInclusion () == JsonInclude .Include .NON_DEFAULT ;
51
71
_annotationIntrospector = _config .getAnnotationIntrospector ();
52
72
}
53
73
@@ -123,15 +143,16 @@ protected BeanPropertyWriter buildWriter(SerializerProvider prov,
123
143
// so that if enclosing class has this, we may need to access values of property,
124
144
// whereas for global defaults OR per-property overrides, we have more
125
145
// static definition. Sigh.
126
- // First: case of class specifying it; try to find POJO property defaults
127
- if (_defaultInclusion . getValueInclusion () == JsonInclude . Include . NON_DEFAULT ) {
146
+ // First: case of class/type specifying it; try to find POJO property defaults
147
+ if (_useRealPropertyDefaults ) {
128
148
// 07-Sep-2016, tatu: may also need to front-load access forcing now
129
149
if (prov .isEnabled (MapperFeature .CAN_OVERRIDE_ACCESS_MODIFIERS )) {
130
150
am .fixAccess (_config .isEnabled (MapperFeature .OVERRIDE_PUBLIC_ACCESS_MODIFIERS ));
131
151
}
132
152
valueToSuppress = getPropertyDefaultValue (propDef .getName (), am , actualType );
133
153
} else {
134
154
valueToSuppress = getDefaultValue (actualType );
155
+ suppressNulls = true ;
135
156
}
136
157
if (valueToSuppress == null ) {
137
158
suppressNulls = true ;
@@ -140,7 +161,6 @@ protected BeanPropertyWriter buildWriter(SerializerProvider prov,
140
161
valueToSuppress = ArrayBuilders .getArrayComparator (valueToSuppress );
141
162
}
142
163
}
143
-
144
164
break ;
145
165
case NON_ABSENT : // new with 2.6, to support Guava/JDK8 Optionals
146
166
// always suppress nulls
0 commit comments