@@ -137,26 +137,17 @@ public JsonValueSerializer withResolved(BeanProperty property,
137
137
*/
138
138
139
139
@ Override // since 2.12
140
- public boolean isEmpty (SerializerProvider ctxt , Object value0 )
140
+ public boolean isEmpty (SerializerProvider ctxt , Object bean )
141
141
{
142
- Object referenced = _accessor . getValue ( value0 );
143
-
142
+ // 31-Oct-2020, tatu: Should perhaps catch access issue here... ?
143
+ Object referenced = _accessor . getValue ( bean );
144
144
if (referenced == null ) {
145
145
return true ;
146
146
}
147
147
JsonSerializer <Object > ser = _valueSerializer ;
148
148
if (ser == null ) {
149
149
try {
150
- Class <?> cc = referenced .getClass ();
151
- ser = _dynamicSerializers .serializerFor (cc );
152
- if (ser == null ) {
153
- if (_valueType .hasGenericTypes ()) {
154
- ser = _findAndAddDynamic (_dynamicSerializers ,
155
- ctxt .constructSpecializedType (_valueType , cc ), ctxt );
156
- } else {
157
- ser = _findAndAddDynamic (_dynamicSerializers , cc , ctxt );
158
- }
159
- }
150
+ ser = _findDynamicSerializer (ctxt , referenced );
160
151
} catch (JsonMappingException e ) {
161
152
throw new RuntimeJsonMappingException (e );
162
153
}
@@ -175,7 +166,7 @@ public boolean isEmpty(SerializerProvider ctxt, Object value0)
175
166
* statically figure out what the result type must be.
176
167
*/
177
168
@ Override
178
- public JsonSerializer <?> createContextual (SerializerProvider provider ,
169
+ public JsonSerializer <?> createContextual (SerializerProvider ctxt ,
179
170
BeanProperty property )
180
171
throws JsonMappingException
181
172
{
@@ -185,14 +176,13 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
185
176
// if not, we don't really know the actual type until we get the instance.
186
177
187
178
// 10-Mar-2010, tatu: Except if static typing is to be used
188
- if (provider .isEnabled (MapperFeature .USE_STATIC_TYPING ) || _valueType .isFinal ()) {
189
- // false -> no need to cache
179
+ if (ctxt .isEnabled (MapperFeature .USE_STATIC_TYPING ) || _valueType .isFinal ()) {
190
180
/* 10-Mar-2010, tatu: Ideally we would actually separate out type
191
181
* serializer from value serializer; but, alas, there's no access
192
182
* to serializer factory at this point...
193
183
*/
194
184
// 05-Sep-2013, tatu: I _think_ this can be considered a primary property...
195
- ser = provider .findPrimaryPropertySerializer (_valueType , property );
185
+ ser = ctxt .findPrimaryPropertySerializer (_valueType , property );
196
186
/* 09-Dec-2010, tatu: Turns out we must add special handling for
197
187
* cases where "native" (aka "natural") type is being serialized,
198
188
* using standard serializer
@@ -206,7 +196,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
206
196
}
207
197
} else {
208
198
// 05-Sep-2013, tatu: I _think_ this can be considered a primary property...
209
- ser = provider .handlePrimaryContextualization (ser , property );
199
+ ser = ctxt .handlePrimaryContextualization (ser , property );
210
200
return withResolved (property , ser , _forceTypeInformation );
211
201
}
212
202
return this ;
@@ -219,88 +209,81 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
219
209
*/
220
210
221
211
@ Override
222
- public void serialize (Object bean , JsonGenerator gen , SerializerProvider provider ) throws IOException
212
+ public void serialize (Object bean , JsonGenerator gen , SerializerProvider ctxt ) throws IOException
223
213
{
214
+ Object value ;
224
215
try {
225
- Object value = _accessor .getValue (bean );
226
- if (value == null ) {
227
- provider .defaultSerializeNull (gen );
228
- return ;
229
- }
230
- JsonSerializer <Object > ser = _valueSerializer ;
231
- if (ser == null ) {
232
- Class <?> cc = value .getClass ();
233
- ser = _dynamicSerializers .serializerFor (cc );
234
- if (ser == null ) {
235
- if (_valueType .hasGenericTypes ()) {
236
- ser = _findAndAddDynamic (_dynamicSerializers ,
237
- provider .constructSpecializedType (_valueType , cc ), provider );
238
- } else {
239
- ser = _findAndAddDynamic (_dynamicSerializers , cc , provider );
240
- }
241
- }
242
- }
243
- ser .serialize (value , gen , provider );
216
+ value = _accessor .getValue (bean );
244
217
} catch (Exception e ) {
245
- wrapAndThrow (provider , e , bean , _accessor .getName () + "()" );
218
+ value = null ;
219
+ wrapAndThrow (ctxt , e , bean , _accessor .getName () + "()" );
220
+ }
221
+
222
+ if (value == null ) {
223
+ ctxt .defaultSerializeNull (gen );
224
+ return ;
246
225
}
226
+ JsonSerializer <Object > ser = _valueSerializer ;
227
+ if (ser == null ) {
228
+ ser = _findDynamicSerializer (ctxt , value );
229
+ }
230
+ ser .serialize (value , gen , ctxt );
247
231
}
248
232
249
233
@ Override
250
- public void serializeWithType (Object bean , JsonGenerator gen , SerializerProvider provider ,
234
+ public void serializeWithType (Object bean , JsonGenerator gen , SerializerProvider ctxt ,
251
235
TypeSerializer typeSer0 ) throws IOException
252
236
{
253
237
// Regardless of other parts, first need to find value to serialize:
254
- Object value = null ;
238
+ Object value ;
255
239
try {
256
240
value = _accessor .getValue (bean );
257
- // and if we got null, can also just write it directly
258
- if (value == null ) {
259
- provider .defaultSerializeNull (gen );
241
+ } catch (Exception e ) {
242
+ value = null ;
243
+ wrapAndThrow (ctxt , e , bean , _accessor .getName () + "()" );
244
+ }
245
+
246
+ // and if we got null, can also just write it directly
247
+ if (value == null ) {
248
+ ctxt .defaultSerializeNull (gen );
249
+ return ;
250
+ }
251
+ JsonSerializer <Object > ser = _valueSerializer ;
252
+ if (ser == null ) { // no serializer yet? Need to fetch
253
+ ser = _findDynamicSerializer (ctxt , value );
254
+ } else {
255
+ // 09-Dec-2010, tatu: To work around natural type's refusal to add type info, we do
256
+ // this (note: type is for the wrapper type, not enclosed value!)
257
+ if (_forceTypeInformation ) {
258
+ // Confusing? Type id is for POJO and NOT for value returned by JsonValue accessor...
259
+ WritableTypeId typeIdDef = typeSer0 .writeTypePrefix (gen ,
260
+ typeSer0 .typeId (bean , JsonToken .VALUE_STRING ));
261
+ ser .serialize (value , gen , ctxt );
262
+ typeSer0 .writeTypeSuffix (gen , typeIdDef );
263
+
260
264
return ;
261
265
}
262
- JsonSerializer <Object > ser = _valueSerializer ;
263
- if (ser == null ) { // no serializer yet? Need to fetch
264
- Class <?> cc = value .getClass ();
265
- ser = _dynamicSerializers .serializerFor (cc );
266
- if (ser == null ) {
267
- if (_valueType .hasGenericTypes ()) {
268
- ser = _findAndAddDynamic (_dynamicSerializers ,
269
- provider .constructSpecializedType (_valueType , cc ), provider );
270
- } else {
271
- ser = _findAndAddDynamic (_dynamicSerializers , cc , provider );
272
- }
273
- }
274
- } else {
275
- // 09-Dec-2010, tatu: To work around natural type's refusal to add type info, we do
276
- // this (note: type is for the wrapper type, not enclosed value!)
277
- if (_forceTypeInformation ) {
278
- // Confusing? Type id is for POJO and NOT for value returned by JsonValue accessor...
279
- WritableTypeId typeIdDef = typeSer0 .writeTypePrefix (gen ,
280
- typeSer0 .typeId (bean , JsonToken .VALUE_STRING ));
281
- ser .serialize (value , gen , provider );
282
- typeSer0 .writeTypeSuffix (gen , typeIdDef );
283
-
284
- return ;
285
- }
286
- }
287
- // 28-Sep-2016, tatu: As per [databind#1385], we do need to do some juggling
288
- // to use different Object for type id (logical type) and actual serialization
289
- // (delegate type).
290
- TypeSerializerRerouter rr = new TypeSerializerRerouter (typeSer0 , bean );
291
- ser .serializeWithType (value , gen , provider , rr );
292
- } catch (Exception e ) {
293
- wrapAndThrow (provider , e , bean , _accessor .getName () + "()" );
294
266
}
267
+ // 28-Sep-2016, tatu: As per [databind#1385], we do need to do some juggling
268
+ // to use different Object for type id (logical type) and actual serialization
269
+ // (delegate type).
270
+ TypeSerializerRerouter rr = new TypeSerializerRerouter (typeSer0 , bean );
271
+ ser .serializeWithType (value , gen , ctxt , rr );
295
272
}
296
-
273
+
274
+ /*
275
+ /**********************************************************
276
+ /* Schema generation
277
+ /**********************************************************
278
+ */
279
+
297
280
@ SuppressWarnings ("deprecation" )
298
281
@ Override
299
- public JsonNode getSchema (SerializerProvider provider , Type typeHint )
282
+ public JsonNode getSchema (SerializerProvider ctxt , Type typeHint )
300
283
throws JsonMappingException
301
284
{
302
285
if (_valueSerializer instanceof SchemaAware ) {
303
- return ((SchemaAware )_valueSerializer ).getSchema (provider , null );
286
+ return ((SchemaAware )_valueSerializer ).getSchema (ctxt , null );
304
287
}
305
288
return com .fasterxml .jackson .databind .jsonschema .JsonSchema .getDefaultSchemaNode ();
306
289
}
@@ -372,6 +355,12 @@ protected boolean _acceptJsonFormatVisitorForEnum(JsonFormatVisitorWrapper visit
372
355
return true ;
373
356
}
374
357
358
+ /*
359
+ /**********************************************************
360
+ /* Other internal helper methods
361
+ /**********************************************************
362
+ */
363
+
375
364
protected boolean isNaturalTypeWithStdHandling (Class <?> rawType , JsonSerializer <?> ser )
376
365
{
377
366
// First: do we have a natural type being handled?
@@ -389,36 +378,35 @@ protected boolean isNaturalTypeWithStdHandling(Class<?> rawType, JsonSerializer<
389
378
}
390
379
391
380
// @since 2.12
392
- protected final JsonSerializer <Object > _findAndAddDynamic ( PropertySerializerMap map ,
393
- Class <?> type , SerializerProvider provider ) throws JsonMappingException
381
+ protected JsonSerializer <Object > _findDynamicSerializer ( SerializerProvider ctxt ,
382
+ Object value ) throws JsonMappingException
394
383
{
395
- // 31-Oct-2020, tatu: Should not get typed/root serializer, but for now has to do:
396
- JsonSerializer <Object > serializer = provider .findTypedValueSerializer (type , false , _property );
397
- PropertySerializerMap .SerializerAndMapResult result = _dynamicSerializers .addSerializer (type , serializer );
398
- // did we get a new map of serializers? If so, start using it
399
- if (map != result .map ) {
384
+ Class <?> cc = value .getClass ();
385
+ JsonSerializer <Object > serializer = _dynamicSerializers .serializerFor (cc );
386
+ if (serializer != null ) {
387
+ return serializer ;
388
+ }
389
+ if (_valueType .hasGenericTypes ()) {
390
+ JavaType fullType = ctxt .constructSpecializedType (_valueType , cc );
391
+ // 31-Oct-2020, tatu: Should not get typed/root serializer, but for now has to do:
392
+ serializer = ctxt .findTypedValueSerializer (fullType , false , _property );
393
+ PropertySerializerMap .SerializerAndMapResult result = _dynamicSerializers .addSerializer (fullType , serializer );
394
+ // did we get a new map of serializers? If so, start using it
400
395
_dynamicSerializers = result .map ;
401
- }
402
- return serializer ;
403
- }
404
-
405
- // @since 2.12
406
- protected final JsonSerializer <Object > _findAndAddDynamic (PropertySerializerMap map ,
407
- JavaType type , SerializerProvider provider ) throws JsonMappingException
408
- {
409
- // 31-Oct-2020, tatu: Should not get typed/root serializer, but for now has to do:
410
- JsonSerializer <Object > serializer = provider .findTypedValueSerializer (type , false , _property );
411
- PropertySerializerMap .SerializerAndMapResult result = _dynamicSerializers .addSerializer (type , serializer );
412
- // did we get a new map of serializers? If so, start using it
413
- if (map != result .map ) {
396
+ return serializer ;
397
+ } else {
398
+ // 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 );
401
+ // did we get a new map of serializers? If so, start using it
414
402
_dynamicSerializers = result .map ;
403
+ return serializer ;
415
404
}
416
- return serializer ;
417
405
}
418
406
419
407
/*
420
408
/**********************************************************
421
- /* Other methods
409
+ /* Standard method overrides
422
410
/**********************************************************
423
411
*/
424
412
0 commit comments