Skip to content

Commit 7e51ede

Browse files
committed
First part of #1888: merge ResolvableSerializer
1 parent 8a7de6b commit 7e51ede

File tree

10 files changed

+51
-57
lines changed

10 files changed

+51
-57
lines changed

src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java

+26-7
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
*<p>
3030
* If serializer is an aggregate one -- meaning it delegates handling of some
3131
* of its contents by using other serializer(s) -- it typically also needs
32-
* to implement {@link com.fasterxml.jackson.databind.ser.ResolvableSerializer},
33-
* which can locate secondary serializers needed. This is important to allow dynamic
32+
* to implement {@link #resolve} which can locate secondary serializers needed.
33+
* This is important to allow dynamic
3434
* overrides of serializers; separate call interface is needed to separate
3535
* resolution of secondary serializers (which may have cyclic link back
3636
* to serializer itself, directly or indirectly).
@@ -44,15 +44,34 @@
4444
* is passed information on property, and can create a newly configured
4545
* serializer for handling that particular property.
4646
*<p>
47-
* If both
48-
* {@link com.fasterxml.jackson.databind.ser.ResolvableSerializer} and
49-
* {@link com.fasterxml.jackson.databind.ser.ContextualSerializer}
50-
* are implemented, resolution of serializers occurs before
51-
* contextualization.
47+
* Resolution of serializers occurs before contextualization.
5248
*/
5349
public abstract class JsonSerializer<T>
5450
implements JsonFormatVisitable
5551
{
52+
/*
53+
/**********************************************************
54+
/* Initialization, with former `ResolvableSerializer
55+
/**********************************************************
56+
*/
57+
58+
/**
59+
* Method called after {@link SerializerProvider} has registered
60+
* the serializer, but before it has returned it to the caller.
61+
* Called object can then resolve its dependencies to other types,
62+
* including self-references (direct or indirect).
63+
*<p>
64+
* Note that this method does NOT return serializer, since resolution
65+
* is not allowed to change actual serializer to use.
66+
*
67+
* @param provider Provider that has constructed serializer this method
68+
* is called on.
69+
*/
70+
public void resolve(SerializerProvider provider)
71+
throws JsonMappingException {
72+
// Default implementation does nothing
73+
}
74+
5675
/*
5776
/**********************************************************
5877
/* Fluent factory methods for constructing decorated versions

src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ public boolean isUnknownTypeSerializer(JsonSerializer<?> ser) {
10221022
* either given a {@link Class} to instantiate (with default constructor),
10231023
* or an uninitialized serializer instance.
10241024
* Either way, serialize will be properly resolved
1025-
* (via {@link com.fasterxml.jackson.databind.ser.ResolvableSerializer}) and/or contextualized
1025+
* (via {@link com.fasterxml.jackson.databind.JsonSerializer#resolve}) and/or contextualized
10261026
* (via {@link com.fasterxml.jackson.databind.ser.ContextualSerializer}) as necessary.
10271027
*
10281028
* @param annotated Annotated entity that contained definition
@@ -1455,19 +1455,15 @@ protected JsonSerializer<Object> _handleContextualResolvable(JsonSerializer<?> s
14551455
BeanProperty property)
14561456
throws JsonMappingException
14571457
{
1458-
if (ser instanceof ResolvableSerializer) {
1459-
((ResolvableSerializer) ser).resolve(this);
1460-
}
1458+
ser.resolve(this);
14611459
return (JsonSerializer<Object>) handleSecondaryContextualization(ser, property);
14621460
}
14631461

14641462
@SuppressWarnings("unchecked")
14651463
protected JsonSerializer<Object> _handleResolvable(JsonSerializer<?> ser)
14661464
throws JsonMappingException
14671465
{
1468-
if (ser instanceof ResolvableSerializer) {
1469-
((ResolvableSerializer) ser).resolve(this);
1470-
}
1466+
ser.resolve(this);
14711467
return (JsonSerializer<Object>) ser;
14721468
}
14731469

@@ -1482,10 +1478,9 @@ protected final DateFormat _dateFormat()
14821478
if (_dateFormat != null) {
14831479
return _dateFormat;
14841480
}
1485-
/* At this point, all timezone configuration should have occurred, with respect
1486-
* to default dateformat configuration. But we still better clone
1487-
* an instance as formatters are stateful, not thread-safe.
1488-
*/
1481+
// At this point, all timezone configuration should have occurred, with respect
1482+
// to default dateformat configuration. But we still better clone
1483+
// an instance as formatters are stateful, not thread-safe.
14891484
DateFormat df = _config.getDateFormat();
14901485
_dateFormat = df = (DateFormat) df.clone();
14911486
// [databind#939]: 26-Sep-2015, tatu: With 2.6, formatter has been (pre)configured

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,11 @@ protected BeanPropertyWriter _constructWriter(SerializerProvider prov,
775775
accessor);
776776
// Unlike most other code paths, serializer produced
777777
// here will NOT be resolved or contextualized, unless done here, so:
778-
if (annotatedSerializer instanceof ResolvableSerializer) {
779-
((ResolvableSerializer) annotatedSerializer).resolve(prov);
778+
if (annotatedSerializer != null) {
779+
annotatedSerializer.resolve(prov);
780+
// 05-Sep-2013, tatu: should be primary property serializer so:
781+
annotatedSerializer = prov.handlePrimaryContextualization(annotatedSerializer, property);
780782
}
781-
// 05-Sep-2013, tatu: should be primary property serializer so:
782-
annotatedSerializer = prov.handlePrimaryContextualization(annotatedSerializer, property);
783783
// And how about polymorphic typing? First special to cover JAXB per-field settings:
784784
TypeSerializer contentTypeSer = null;
785785
// 16-Feb-2014, cgc: contentType serializers for collection-like and map-like types

src/main/java/com/fasterxml/jackson/databind/ser/ContextualSerializer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
* have differing behavior depending on what kind of property is being serialized.
1111
*<p>
1212
* Note that in cases where serializer needs both contextualization and
13-
* resolution -- that is, implements both this interface and {@link ResolvableSerializer}
14-
* -- resolution via {@link ResolvableSerializer} occurs first, and contextual
13+
* resolution, resolution occurs first, and contextual
1514
* resolution (via this interface) later on.
1615
*/
1716
public interface ContextualSerializer

src/main/java/com/fasterxml/jackson/databind/ser/ResolvableSerializer.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
* but before being used. This is typically used to resolve references
99
* to other contained types; for example, bean serializers use this
1010
* to eagerly find serializers for contained field types.
11-
*<p>
12-
* Note that in cases where serializer needs both contextualization and
13-
* resolution -- that is, implements both this interface and {@link ContextualSerializer}
14-
* -- resolution via this interface occurs first, and contextual
15-
* resolution (using {@link ContextualSerializer}) later on.
11+
*
12+
* @deprecated Since 3.0: method demoted to <code>JsonSerializer</code>
1613
*/
14+
@Deprecated
1715
public interface ResolvableSerializer
1816
{
1917
/**

src/main/java/com/fasterxml/jackson/databind/ser/SerializerCache.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ public void addAndResolveNonTypedSerializer(Class<?> type, JsonSerializer<Object
160160
* this because while we do need to register instance first, we also must
161161
* keep lock until resolution is complete.
162162
*/
163-
if (ser instanceof ResolvableSerializer) {
164-
((ResolvableSerializer) ser).resolve(provider);
165-
}
163+
ser.resolve(provider);
166164
}
167165
}
168166

@@ -179,9 +177,7 @@ public void addAndResolveNonTypedSerializer(JavaType type, JsonSerializer<Object
179177
* this because while we do need to register instance first, we also must
180178
* keep lock until resolution is complete.
181179
*/
182-
if (ser instanceof ResolvableSerializer) {
183-
((ResolvableSerializer) ser).resolve(provider);
184-
}
180+
ser.resolve(provider);
185181
}
186182
}
187183

@@ -202,9 +198,7 @@ public void addAndResolveNonTypedSerializer(Class<?> rawType, JavaType fullType,
202198
if ((ob1 == null) || (ob2 == null)) {
203199
_readOnlyMap.set(null);
204200
}
205-
if (ser instanceof ResolvableSerializer) {
206-
((ResolvableSerializer) ser).resolve(provider);
207-
}
201+
ser.resolve(provider);
208202
}
209203
}
210204

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
@SuppressWarnings("serial")
3333
public abstract class BeanSerializerBase
3434
extends StdSerializer<Object>
35-
implements ContextualSerializer, ResolvableSerializer,
35+
implements ContextualSerializer,
3636
JsonFormatVisitable
3737
{
3838
protected final static PropertyName NAME_FOR_OBJECT_REF = new PropertyName("#object-ref");
@@ -265,8 +265,8 @@ private final static BeanPropertyWriter[] rename(BeanPropertyWriter[] props,
265265
*/
266266

267267
/**
268-
* We need to implement {@link ResolvableSerializer} to be able to
269-
* properly handle cyclic type references.
268+
* We need to resolve dependant serializers here
269+
* to be able to properly handle cyclic type references.
270270
*/
271271
@Override
272272
public void resolve(SerializerProvider provider)

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
1010
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
1111
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
12-
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
1312
import com.fasterxml.jackson.databind.util.ClassUtil;
1413
import com.fasterxml.jackson.databind.util.Converter;
1514

@@ -20,13 +19,11 @@
2019
*<p>
2120
* Note that although types may be related, they must not be same; trying
2221
* to do this will result in an exception.
23-
*
24-
* @since 2.1
2522
*/
2623
@SuppressWarnings("serial")
2724
public class StdDelegatingSerializer
2825
extends StdSerializer<Object>
29-
implements ContextualSerializer, ResolvableSerializer,
26+
implements ContextualSerializer,
3027
JsonFormatVisitable
3128
{
3229
protected final Converter<Object,?> _converter;
@@ -95,9 +92,8 @@ protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
9592
@Override
9693
public void resolve(SerializerProvider provider) throws JsonMappingException
9794
{
98-
if ((_delegateSerializer != null)
99-
&& (_delegateSerializer instanceof ResolvableSerializer)) {
100-
((ResolvableSerializer) _delegateSerializer).resolve(provider);
95+
if (_delegateSerializer != null) {
96+
_delegateSerializer.resolve(provider);
10197
}
10298
}
10399

src/test/java/com/fasterxml/jackson/databind/contextual/TestContextualSerialization.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
1212
import com.fasterxml.jackson.databind.module.SimpleModule;
1313
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
14-
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
1514

1615
/**
1716
* Test cases to verify that it is possible to define serializers
@@ -160,7 +159,7 @@ public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty
160159

161160
static class ContextualAndResolvable
162161
extends JsonSerializer<String>
163-
implements ContextualSerializer, ResolvableSerializer
162+
implements ContextualSerializer
164163
{
165164
protected int isContextual;
166165
protected int isResolved;

src/test/java/com/fasterxml/jackson/databind/jsontype/TestWithGenerics.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
1111
import com.fasterxml.jackson.databind.*;
1212
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
13-
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
1413

1514
public class TestWithGenerics extends BaseMapTest
1615
{
@@ -67,10 +66,7 @@ static class SomeObject {
6766
public String someValue = UUID.randomUUID().toString();
6867
}
6968

70-
// Beans for [JACKSON-430]
71-
7269
static class CustomJsonSerializer extends JsonSerializer<Object>
73-
implements ResolvableSerializer
7470
{
7571
private final JsonSerializer<Object> beanSerializer;
7672

@@ -96,9 +92,7 @@ public void serializeWithType( Object value, JsonGenerator jgen, SerializerProvi
9692
@Override
9793
public void resolve(SerializerProvider provider) throws JsonMappingException
9894
{
99-
if (beanSerializer instanceof ResolvableSerializer) {
100-
((ResolvableSerializer) beanSerializer).resolve(provider);
101-
}
95+
beanSerializer.resolve(provider);
10296
}
10397
}
10498

0 commit comments

Comments
 (0)