3
3
import java .io .IOException ;
4
4
import java .lang .reflect .InvocationTargetException ;
5
5
import java .lang .reflect .Type ;
6
+ import java .util .Collections ;
6
7
import java .util .LinkedHashSet ;
7
8
import java .util .Set ;
8
9
@@ -73,6 +74,14 @@ public class JsonValueSerializer
73
74
*/
74
75
protected final boolean _forceTypeInformation ;
75
76
77
+ /**
78
+ * Names of properties to ignore from Value class accessed using
79
+ * accessor.
80
+ *
81
+ * @since 2.16
82
+ */
83
+ protected final Set <String > _ignoredProperties ;
84
+
76
85
/**
77
86
* If value type cannot be statically determined, mapping from
78
87
* runtime value types to serializers are cached in this object.
@@ -97,7 +106,8 @@ public class JsonValueSerializer
97
106
*/
98
107
@ SuppressWarnings ("unchecked" )
99
108
public JsonValueSerializer (AnnotatedMember accessor ,
100
- TypeSerializer vts , JsonSerializer <?> ser )
109
+ TypeSerializer vts , JsonSerializer <?> ser ,
110
+ Set <String > ignoredProperties )
101
111
{
102
112
super (accessor .getType ());
103
113
_accessor = accessor ;
@@ -106,15 +116,23 @@ public JsonValueSerializer(AnnotatedMember accessor,
106
116
_valueSerializer = (JsonSerializer <Object >) ser ;
107
117
_property = null ;
108
118
_forceTypeInformation = true ; // gets reconsidered when we are contextualized
119
+ _ignoredProperties = ignoredProperties ;
109
120
_dynamicSerializers = PropertySerializerMap .emptyForProperties ();
110
121
}
111
122
123
+ @ Deprecated // since 2.16
124
+ public JsonValueSerializer (AnnotatedMember accessor ,
125
+ TypeSerializer vts , JsonSerializer <?> ser )
126
+ {
127
+ this (accessor , vts , ser , Collections .emptySet ());
128
+ }
129
+
112
130
/**
113
131
* @deprecated Since 2.12
114
132
*/
115
133
@ Deprecated
116
134
public JsonValueSerializer (AnnotatedMember accessor , JsonSerializer <?> ser ) {
117
- this (accessor , null , ser );
135
+ this (accessor , null , ser , Collections . emptySet () );
118
136
}
119
137
120
138
// @since 2.12
@@ -129,9 +147,24 @@ public JsonValueSerializer(JsonValueSerializer src, BeanProperty property,
129
147
_valueSerializer = (JsonSerializer <Object >) ser ;
130
148
_property = property ;
131
149
_forceTypeInformation = forceTypeInfo ;
150
+ _ignoredProperties = src ._ignoredProperties ;
132
151
_dynamicSerializers = PropertySerializerMap .emptyForProperties ();
133
152
}
134
153
154
+ /**
155
+ * @since 2.16
156
+ */
157
+ public static JsonValueSerializer construct (SerializationConfig config ,
158
+ AnnotatedMember accessor ,
159
+ TypeSerializer vts , JsonSerializer <?> ser )
160
+ {
161
+ JsonIgnoreProperties .Value ignorals = config .getAnnotationIntrospector ()
162
+ .findPropertyIgnoralByName (config , accessor );
163
+ final Set <String > ignoredProperties = ignorals .findIgnoredForSerialization ();
164
+ ser = _withIgnoreProperties (ser , ignoredProperties );
165
+ return new JsonValueSerializer (accessor , vts , ser , ignoredProperties );
166
+ }
167
+
135
168
@ SuppressWarnings ("unchecked" )
136
169
private final static Class <Object > _notNullClass (Class <?> cls ) {
137
170
return (cls == null ) ? Object .class : (Class <Object >) cls ;
@@ -205,6 +238,8 @@ public JsonSerializer<?> createContextual(SerializerProvider ctxt,
205
238
*/
206
239
// 05-Sep-2013, tatu: I _think_ this can be considered a primary property...
207
240
ser = ctxt .findPrimaryPropertySerializer (_valueType , property );
241
+ ser = _withIgnoreProperties (ser , _ignoredProperties );
242
+
208
243
/* 09-Dec-2010, tatu: Turns out we must add special handling for
209
244
* cases where "native" (aka "natural") type is being serialized,
210
245
* using standard serializer
@@ -416,12 +451,13 @@ protected JsonSerializer<Object> _findDynamicSerializer(SerializerProvider ctxt,
416
451
if (_valueType .hasGenericTypes ()) {
417
452
final JavaType fullType = ctxt .constructSpecializedType (_valueType , valueClass );
418
453
serializer = ctxt .findPrimaryPropertySerializer (fullType , _property );
454
+ serializer = _withIgnoreProperties (serializer , _ignoredProperties );
419
455
PropertySerializerMap .SerializerAndMapResult result = _dynamicSerializers .addSerializer (fullType , serializer );
420
456
_dynamicSerializers = result .map ;
421
457
} else {
422
458
serializer = ctxt .findPrimaryPropertySerializer (valueClass , _property );
423
459
// [databind#3647] : Support `@JsonIgnoreProperties` to work with `@JsonValue`
424
- serializer = ( JsonSerializer < Object >) _withIgnoreProperties (ctxt , serializer );
460
+ serializer = _withIgnoreProperties (serializer , _ignoredProperties );
425
461
PropertySerializerMap .SerializerAndMapResult result = _dynamicSerializers .addSerializer (valueClass , serializer );
426
462
_dynamicSerializers = result .map ;
427
463
}
@@ -451,18 +487,17 @@ protected JsonSerializer<Object> _findDynamicSerializer(SerializerProvider ctxt,
451
487
/**
452
488
* Internal helper that configures the provided {@code ser} to ignore properties specified by {@link JsonIgnoreProperties}.
453
489
*
454
- * @param ctxt For introspection.
455
490
* @param ser Serializer to be configured
456
491
* @return Configured serializer with specified properties ignored
457
492
* @since 2.16
458
493
*/
459
494
@ SuppressWarnings ("unchecked" )
460
- protected JsonSerializer <Object > _withIgnoreProperties (SerializerProvider ctxt , JsonSerializer <?> ser ) {
461
- JsonIgnoreProperties . Value ignorals = ctxt . getAnnotationIntrospector ()
462
- . findPropertyIgnoralByName ( ctxt . getConfig (), _accessor );
463
- Set < String > ignored = ignorals . findIgnoredForSerialization ();
464
- if (! ignored . isEmpty ()) {
465
- ser = ser . withIgnoredProperties ( ignored );
495
+ protected static JsonSerializer <Object > _withIgnoreProperties (JsonSerializer <?> ser ,
496
+ Set < String > ignoredProperties ) {
497
+ if ( ser != null ) {
498
+ if (! ignoredProperties . isEmpty ()) {
499
+ ser = ser . withIgnoredProperties ( ignoredProperties );
500
+ }
466
501
}
467
502
return (JsonSerializer <Object >) ser ;
468
503
}
0 commit comments