Skip to content

Commit 6ae934b

Browse files
committed
Further refactoring wrt #2909 fix
1 parent 170c6d4 commit 6ae934b

File tree

1 file changed

+89
-101
lines changed

1 file changed

+89
-101
lines changed

src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java

Lines changed: 89 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,17 @@ public JsonValueSerializer withResolved(BeanProperty property,
137137
*/
138138

139139
@Override // since 2.12
140-
public boolean isEmpty(SerializerProvider ctxt, Object value0)
140+
public boolean isEmpty(SerializerProvider ctxt, Object bean)
141141
{
142-
Object referenced = _accessor.getValue(value0);
143-
142+
// 31-Oct-2020, tatu: Should perhaps catch access issue here... ?
143+
Object referenced = _accessor.getValue(bean);
144144
if (referenced == null) {
145145
return true;
146146
}
147147
JsonSerializer<Object> ser = _valueSerializer;
148148
if (ser == null) {
149149
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);
160151
} catch (JsonMappingException e) {
161152
throw new RuntimeJsonMappingException(e);
162153
}
@@ -175,7 +166,7 @@ public boolean isEmpty(SerializerProvider ctxt, Object value0)
175166
* statically figure out what the result type must be.
176167
*/
177168
@Override
178-
public JsonSerializer<?> createContextual(SerializerProvider provider,
169+
public JsonSerializer<?> createContextual(SerializerProvider ctxt,
179170
BeanProperty property)
180171
throws JsonMappingException
181172
{
@@ -185,14 +176,13 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
185176
// if not, we don't really know the actual type until we get the instance.
186177

187178
// 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()) {
190180
/* 10-Mar-2010, tatu: Ideally we would actually separate out type
191181
* serializer from value serializer; but, alas, there's no access
192182
* to serializer factory at this point...
193183
*/
194184
// 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);
196186
/* 09-Dec-2010, tatu: Turns out we must add special handling for
197187
* cases where "native" (aka "natural") type is being serialized,
198188
* using standard serializer
@@ -206,7 +196,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
206196
}
207197
} else {
208198
// 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);
210200
return withResolved(property, ser, _forceTypeInformation);
211201
}
212202
return this;
@@ -219,88 +209,81 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
219209
*/
220210

221211
@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
223213
{
214+
Object value;
224215
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);
244217
} 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;
246225
}
226+
JsonSerializer<Object> ser = _valueSerializer;
227+
if (ser == null) {
228+
ser = _findDynamicSerializer(ctxt, value);
229+
}
230+
ser.serialize(value, gen, ctxt);
247231
}
248232

249233
@Override
250-
public void serializeWithType(Object bean, JsonGenerator gen, SerializerProvider provider,
234+
public void serializeWithType(Object bean, JsonGenerator gen, SerializerProvider ctxt,
251235
TypeSerializer typeSer0) throws IOException
252236
{
253237
// Regardless of other parts, first need to find value to serialize:
254-
Object value = null;
238+
Object value;
255239
try {
256240
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+
260264
return;
261265
}
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() + "()");
294266
}
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);
295272
}
296-
273+
274+
/*
275+
/**********************************************************
276+
/* Schema generation
277+
/**********************************************************
278+
*/
279+
297280
@SuppressWarnings("deprecation")
298281
@Override
299-
public JsonNode getSchema(SerializerProvider provider, Type typeHint)
282+
public JsonNode getSchema(SerializerProvider ctxt, Type typeHint)
300283
throws JsonMappingException
301284
{
302285
if (_valueSerializer instanceof SchemaAware) {
303-
return ((SchemaAware)_valueSerializer).getSchema(provider, null);
286+
return ((SchemaAware)_valueSerializer).getSchema(ctxt, null);
304287
}
305288
return com.fasterxml.jackson.databind.jsonschema.JsonSchema.getDefaultSchemaNode();
306289
}
@@ -372,6 +355,12 @@ protected boolean _acceptJsonFormatVisitorForEnum(JsonFormatVisitorWrapper visit
372355
return true;
373356
}
374357

358+
/*
359+
/**********************************************************
360+
/* Other internal helper methods
361+
/**********************************************************
362+
*/
363+
375364
protected boolean isNaturalTypeWithStdHandling(Class<?> rawType, JsonSerializer<?> ser)
376365
{
377366
// First: do we have a natural type being handled?
@@ -389,36 +378,35 @@ protected boolean isNaturalTypeWithStdHandling(Class<?> rawType, JsonSerializer<
389378
}
390379

391380
// @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
394383
{
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
400395
_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
414402
_dynamicSerializers = result.map;
403+
return serializer;
415404
}
416-
return serializer;
417405
}
418406

419407
/*
420408
/**********************************************************
421-
/* Other methods
409+
/* Standard method overrides
422410
/**********************************************************
423411
*/
424412

0 commit comments

Comments
 (0)