@@ -48,6 +48,11 @@ public class JsonValueSerializer
48
48
*/
49
49
protected final AnnotatedMember _accessor ;
50
50
51
+ /**
52
+ * @since 2.12
53
+ */
54
+ protected final TypeSerializer _valueTypeSerializer ;
55
+
51
56
protected final JsonSerializer <Object > _valueSerializer ;
52
57
53
58
protected final BeanProperty _property ;
@@ -86,29 +91,40 @@ public class JsonValueSerializer
86
91
* occurs if and only if the "value method" was annotated with
87
92
* {@link com.fasterxml.jackson.databind.annotation.JsonSerialize#using}), otherwise
88
93
* null
89
- *
90
- * @since 2.8 Earlier method took "raw" Method, but that does not work with access
91
- * to information we need
94
+ *
95
+ * @since 2.12 added {@link TypeSerializer} since 2.11
92
96
*/
93
97
@ SuppressWarnings ("unchecked" )
94
- public JsonValueSerializer (AnnotatedMember accessor , JsonSerializer <?> ser )
98
+ public JsonValueSerializer (AnnotatedMember accessor ,
99
+ TypeSerializer vts , JsonSerializer <?> ser )
95
100
{
96
101
super (accessor .getType ());
97
102
_accessor = accessor ;
98
103
_valueType = accessor .getType ();
104
+ _valueTypeSerializer = vts ;
99
105
_valueSerializer = (JsonSerializer <Object >) ser ;
100
106
_property = null ;
101
107
_forceTypeInformation = true ; // gets reconsidered when we are contextualized
102
108
_dynamicSerializers = PropertySerializerMap .emptyForProperties ();
103
109
}
104
110
111
+ /**
112
+ * @deprecated Since 2.12
113
+ */
114
+ @ Deprecated
115
+ public JsonValueSerializer (AnnotatedMember accessor , JsonSerializer <?> ser ) {
116
+ this (accessor , null , ser );
117
+ }
118
+
119
+ // @since 2.12
105
120
@ SuppressWarnings ("unchecked" )
106
121
public JsonValueSerializer (JsonValueSerializer src , BeanProperty property ,
107
- JsonSerializer <?> ser , boolean forceTypeInfo )
122
+ TypeSerializer vts , JsonSerializer <?> ser , boolean forceTypeInfo )
108
123
{
109
124
super (_notNullClass (src .handledType ()));
110
125
_accessor = src ._accessor ;
111
126
_valueType = src ._valueType ;
127
+ _valueTypeSerializer = vts ;
112
128
_valueSerializer = (JsonSerializer <Object >) ser ;
113
129
_property = property ;
114
130
_forceTypeInformation = forceTypeInfo ;
@@ -120,14 +136,15 @@ private final static Class<Object> _notNullClass(Class<?> cls) {
120
136
return (cls == null ) ? Object .class : (Class <Object >) cls ;
121
137
}
122
138
123
- public JsonValueSerializer withResolved (BeanProperty property ,
124
- JsonSerializer <?> ser , boolean forceTypeInfo )
139
+ protected JsonValueSerializer withResolved (BeanProperty property ,
140
+ TypeSerializer vts , JsonSerializer <?> ser , boolean forceTypeInfo )
125
141
{
126
- if (_property == property && _valueSerializer == ser
127
- && forceTypeInfo == _forceTypeInformation ) {
142
+ if ((_property == property )
143
+ && (_valueTypeSerializer == vts ) && (_valueSerializer == ser )
144
+ && (forceTypeInfo == _forceTypeInformation )) {
128
145
return this ;
129
146
}
130
- return new JsonValueSerializer (this , property , ser , forceTypeInfo );
147
+ return new JsonValueSerializer (this , property , vts , ser , forceTypeInfo );
131
148
}
132
149
133
150
/*
@@ -147,7 +164,7 @@ public boolean isEmpty(SerializerProvider ctxt, Object bean)
147
164
JsonSerializer <Object > ser = _valueSerializer ;
148
165
if (ser == null ) {
149
166
try {
150
- ser = _findDynamicSerializer (ctxt , referenced );
167
+ ser = _findDynamicSerializer (ctxt , referenced . getClass () );
151
168
} catch (JsonMappingException e ) {
152
169
throw new RuntimeJsonMappingException (e );
153
170
}
@@ -170,6 +187,10 @@ public JsonSerializer<?> createContextual(SerializerProvider ctxt,
170
187
BeanProperty property )
171
188
throws JsonMappingException
172
189
{
190
+ TypeSerializer typeSer = _valueTypeSerializer ;
191
+ if (typeSer != null ) {
192
+ typeSer = typeSer .forProperty (property );
193
+ }
173
194
JsonSerializer <?> ser = _valueSerializer ;
174
195
if (ser == null ) {
175
196
// Can only assign serializer statically if the declared type is final:
@@ -188,16 +209,16 @@ public JsonSerializer<?> createContextual(SerializerProvider ctxt,
188
209
* using standard serializer
189
210
*/
190
211
boolean forceTypeInformation = isNaturalTypeWithStdHandling (_valueType .getRawClass (), ser );
191
- return withResolved (property , ser , forceTypeInformation );
212
+ return withResolved (property , typeSer , ser , forceTypeInformation );
192
213
}
193
214
// [databind#2822]: better hold on to "property", regardless
194
215
if (property != _property ) {
195
- return withResolved (property , ser , _forceTypeInformation );
216
+ return withResolved (property , typeSer , ser , _forceTypeInformation );
196
217
}
197
218
} else {
198
219
// 05-Sep-2013, tatu: I _think_ this can be considered a primary property...
199
220
ser = ctxt .handlePrimaryContextualization (ser , property );
200
- return withResolved (property , ser , _forceTypeInformation );
221
+ return withResolved (property , typeSer , ser , _forceTypeInformation );
201
222
}
202
223
return this ;
203
224
}
@@ -221,13 +242,17 @@ public void serialize(Object bean, JsonGenerator gen, SerializerProvider ctxt) t
221
242
222
243
if (value == null ) {
223
244
ctxt .defaultSerializeNull (gen );
224
- return ;
225
- }
226
- JsonSerializer <Object > ser = _valueSerializer ;
227
- if (ser == null ) {
228
- ser = _findDynamicSerializer (ctxt , value );
245
+ } else {
246
+ JsonSerializer <Object > ser = _valueSerializer ;
247
+ if (ser == null ) {
248
+ ser = _findDynamicSerializer (ctxt , value .getClass ());
249
+ }
250
+ if (_valueTypeSerializer != null ) {
251
+ ser .serializeWithType (value , gen , ctxt , _valueTypeSerializer );
252
+ } else {
253
+ ser .serialize (value , gen , ctxt );
254
+ }
229
255
}
230
- ser .serialize (value , gen , ctxt );
231
256
}
232
257
233
258
@ Override
@@ -250,7 +275,7 @@ public void serializeWithType(Object bean, JsonGenerator gen, SerializerProvider
250
275
}
251
276
JsonSerializer <Object > ser = _valueSerializer ;
252
277
if (ser == null ) { // no serializer yet? Need to fetch
253
- ser = _findDynamicSerializer (ctxt , value );
278
+ ser = _findDynamicSerializer (ctxt , value . getClass () );
254
279
} else {
255
280
// 09-Dec-2010, tatu: To work around natural type's refusal to add type info, we do
256
281
// this (note: type is for the wrapper type, not enclosed value!)
@@ -379,15 +404,26 @@ protected boolean isNaturalTypeWithStdHandling(Class<?> rawType, JsonSerializer<
379
404
380
405
// @since 2.12
381
406
protected JsonSerializer <Object > _findDynamicSerializer (SerializerProvider ctxt ,
382
- Object value ) throws JsonMappingException
407
+ Class <?> valueClass ) throws JsonMappingException
383
408
{
384
- Class <?> cc = value .getClass ();
385
- JsonSerializer <Object > serializer = _dynamicSerializers .serializerFor (cc );
386
- if (serializer != null ) {
387
- return serializer ;
409
+ JsonSerializer <Object > serializer = _dynamicSerializers .serializerFor (valueClass );
410
+ if (serializer == null ) {
411
+ if (_valueType .hasGenericTypes ()) {
412
+ final JavaType fullType = ctxt .constructSpecializedType (_valueType , valueClass );
413
+ serializer = ctxt .findPrimaryPropertySerializer (fullType , _property );
414
+ PropertySerializerMap .SerializerAndMapResult result = _dynamicSerializers .addSerializer (fullType , serializer );
415
+ _dynamicSerializers = result .map ;
416
+ } else {
417
+ serializer = ctxt .findPrimaryPropertySerializer (valueClass , _property );
418
+ PropertySerializerMap .SerializerAndMapResult result = _dynamicSerializers .addSerializer (valueClass , serializer );
419
+ _dynamicSerializers = result .map ;
420
+ }
388
421
}
422
+ return serializer ;
423
+
424
+ /*
389
425
if (_valueType.hasGenericTypes()) {
390
- JavaType fullType = ctxt .constructSpecializedType (_valueType , cc );
426
+ JavaType fullType = ctxt.constructSpecializedType(_valueType, valueClass );
391
427
// 31-Oct-2020, tatu: Should not get typed/root serializer, but for now has to do:
392
428
serializer = ctxt.findTypedValueSerializer(fullType, false, _property);
393
429
PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers.addSerializer(fullType, serializer);
@@ -396,12 +432,13 @@ protected JsonSerializer<Object> _findDynamicSerializer(SerializerProvider ctxt,
396
432
return serializer;
397
433
} else {
398
434
// 31-Oct-2020, tatu: Should not get typed/root serializer, but for now has to do:
399
- serializer = ctxt .findTypedValueSerializer (cc , false , _property );
400
- PropertySerializerMap .SerializerAndMapResult result = _dynamicSerializers .addSerializer (cc , serializer );
435
+ serializer = ctxt.findTypedValueSerializer(valueClass , false, _property);
436
+ PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers.addSerializer(valueClass , serializer);
401
437
// did we get a new map of serializers? If so, start using it
402
438
_dynamicSerializers = result.map;
403
439
return serializer;
404
440
}
441
+ */
405
442
}
406
443
407
444
/*
0 commit comments