Skip to content

Commit 54dc162

Browse files
committed
Continuing work to support "shape-shifting"
1 parent 2184c14 commit 54dc162

25 files changed

+167
-107
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public boolean isNonStaticInnerClass() {
193193
* defined by possible annotations and possible per-type config overrides.
194194
*/
195195
public abstract JsonFormat.Value findExpectedFormat(Class<?> baseType);
196-
196+
197197
/**
198198
* Method for finding {@link Converter} used for serializing instances
199199
* of this class.

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

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.Iterator;
55

6+
import com.fasterxml.jackson.annotation.JsonFormat;
67
import com.fasterxml.jackson.core.*;
78

89
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable;
@@ -151,6 +152,45 @@ public JsonSerializer<?> withFilterId(Object filterId) {
151152
return this;
152153
}
153154

155+
/**
156+
* Mutant factory called if there is need to create a serializer with specified
157+
* format overrides (typically from property on which this serializer would be used,
158+
* based on type declaration). Method is called before {@link #createContextual}
159+
* but right after serializer is either constructed or fetched from cache.
160+
*<p>
161+
* Method can do one of three things:
162+
*<ul>
163+
* <li>Return {@code this} instance as is: this means that none of overrides has any effect
164+
* </li>
165+
* <li>Return an alternate {@link JsonSerializer}, suitable for use with specified format
166+
* </li>
167+
* <li>Return {@code null} to indicate that this serializer instance is not suitable for
168+
* handling format variation, but does not know how to construct new serializer: caller
169+
* will typically then call {@link SerializerFactory} with overrides to construct new serializer
170+
* </li>
171+
*</ul>
172+
* One example of second approach is the case where {@link JsonFormat.Shape#STRING} indicates String
173+
* representation and code can just construct simple "string-like serializer", or variant of itself
174+
* (similar to how {@link #createContextual} is often implemented).
175+
* And third case (returning {@code null}) is applicable for cases like format defines
176+
* {@link JsonFormat.Shape#POJO}, requesting "introspect serializer for POJO regardless of type":
177+
* {@link SerializerFactory} is needed for full re-introspection, typically.
178+
*
179+
* @param formatOverrides (not null) Override settings, NOT including original format settings (which
180+
* serializer needs to explicitly retain if needed)
181+
*
182+
* @since 3.0
183+
*/
184+
public JsonSerializer<?> withFormatOverride(SerializationConfig config,
185+
JsonFormat.Value formatOverrides)
186+
{
187+
// First: if no override, safe to use as is:
188+
if (formatOverrides.getShape() == JsonFormat.Shape.ANY) {
189+
return this;
190+
}
191+
return null;
192+
}
193+
154194
/*
155195
/**********************************************************************
156196
/* Serialization methods
@@ -211,7 +251,7 @@ public void serializeWithType(T value, JsonGenerator gen, SerializerProvider ser
211251

212252
/*
213253
/**********************************************************************
214-
/* Other accessors
254+
/* Accessors for serializer metadata
215255
/**********************************************************************
216256
*/
217257

@@ -221,26 +261,10 @@ public void serializeWithType(T value, JsonGenerator gen, SerializerProvider ser
221261
* may be a more generic (super-type) -- but it should not be
222262
* incorrect (return a non-related type).
223263
*<p>
224-
* Default implementation will return null, which essentially means
225-
* same as returning <code>Object.class</code> would; that is, that
226-
* nothing is known about handled type.
227-
*<p>
264+
* NOTE: starting with 3.0, left {@code abstract}.
228265
*/
229-
public Class<T> handledType() { return null; }
266+
public Class<?> handledType() { return Object.class; }
230267

231-
/**
232-
* Method called to check whether given serializable value is
233-
* considered "empty" value (for purposes of suppressing serialization
234-
* of empty values).
235-
*<p>
236-
* Default implementation will consider only null values to be empty.
237-
*/
238-
public boolean isEmpty(SerializerProvider provider, T value)
239-
throws IOException
240-
{
241-
return (value == null);
242-
}
243-
244268
/**
245269
* Method that can be called to see whether this serializer instance
246270
* will use Object Id to handle cyclic references.
@@ -258,7 +282,7 @@ public boolean usesObjectId() {
258282
public boolean isUnwrappingSerializer() {
259283
return false;
260284
}
261-
285+
262286
/**
263287
* Accessor that can be used to determine if this serializer uses
264288
* another serializer for actual serialization, by delegating
@@ -284,6 +308,25 @@ public Iterator<PropertyWriter> properties() {
284308
return ClassUtil.emptyIterator();
285309
}
286310

311+
/*
312+
/**********************************************************************
313+
/* Accessors for introspecting handling of values
314+
/**********************************************************************
315+
*/
316+
317+
/**
318+
* Method called to check whether given serializable value is
319+
* considered "empty" value (for purposes of suppressing serialization
320+
* of empty values).
321+
*<p>
322+
* Default implementation will consider only null values to be empty.
323+
*/
324+
public boolean isEmpty(SerializerProvider provider, T value)
325+
throws IOException
326+
{
327+
return (value == null);
328+
}
329+
287330
/*
288331
/**********************************************************************
289332
/* Default JsonFormatVisitable implementation

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public abstract class ContainerSerializer<T>
4141
/**********************************************************************
4242
*/
4343

44-
protected ContainerSerializer(Class<T> t) {
44+
protected ContainerSerializer(Class<?> t) {
4545
super(t);
4646
_property = null;
4747
_dynamicValueSerializers = PropertySerializerMap.emptyForProperties();
@@ -51,8 +51,9 @@ protected ContainerSerializer(Class<T> t) {
5151
* Alternate constructor that is (alas!) needed to work
5252
* around kinks of generic type handling
5353
*/
54+
@Deprecated
5455
protected ContainerSerializer(Class<?> t, boolean dummy) {
55-
super(t, dummy);
56+
super(t);
5657
_property = null;
5758
_dynamicValueSerializers = PropertySerializerMap.emptyForProperties();
5859
}
@@ -73,7 +74,7 @@ protected ContainerSerializer(ContainerSerializer<?> src) {
7374
}
7475

7576
protected ContainerSerializer(ContainerSerializer<?> src, BeanProperty prop) {
76-
super(src._handledType, false);
77+
super(src._handledType);
7778
_property = prop;
7879
// 16-Apr-2018, tatu: Could retain, possibly, in some cases... but may be
7980
// dangerous as general practice so reset

src/main/java/com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ public UnknownSerializer() {
1717
super(Object.class);
1818
}
1919

20-
/**
21-
* @since 2.6
22-
*/
2320
public UnknownSerializer(Class<?> cls) {
24-
super(cls, false);
21+
super(cls);
2522
}
2623

2724
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class AsArraySerializerBase<T>
5656
protected AsArraySerializerBase(Class<?> cls, JavaType et, boolean staticTyping,
5757
TypeSerializer vts, JsonSerializer<?> elementSerializer)
5858
{
59-
super(cls, false);
59+
super(cls);
6060
_elementType = et;
6161
// static if explicitly requested, or if element type is final
6262
_staticTyping = staticTyping || (et != null && et.isFinal());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected MapSerializer(Set<String> ignoredEntries,
138138
TypeSerializer vts,
139139
JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer)
140140
{
141-
super(Map.class, false);
141+
super(Map.class);
142142
_ignoredEntries = ((ignoredEntries == null) || ignoredEntries.isEmpty())
143143
? null : ignoredEntries;
144144
_keyType = keyType;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class RawSerializer<T>
2222
* just take wild card and coerce type.
2323
*/
2424
public RawSerializer(Class<?> cls) {
25-
super(cls, false);
25+
super(cls);
2626
}
2727

2828
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public abstract class StaticListSerializerBase<T extends Collection<?>>
2727
protected final Boolean _unwrapSingle;
2828

2929
protected StaticListSerializerBase(Class<?> cls) {
30-
super(cls, false);
30+
super(cls);
3131
_unwrapSingle = null;
3232
}
3333

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public StdDelegatingSerializer(Converter<?,?> converter)
6666
@SuppressWarnings("unchecked")
6767
public <T> StdDelegatingSerializer(Class<T> cls, Converter<T,?> converter)
6868
{
69-
super(cls, false);
69+
super(cls);
7070
_converter = (Converter<Object,?>)converter;
7171
_delegateType = null;
7272
_delegateSerializer = null;

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static class Default extends StdSerializer<Object> {
129129
protected final int _typeId;
130130

131131
public Default(int typeId, Class<?> type) {
132-
super(type, false);
132+
super(type);
133133
_typeId = typeId;
134134
}
135135

@@ -189,7 +189,7 @@ public static class Dynamic extends StdSerializer<Object>
189189
protected transient PropertySerializerMap _dynamicSerializers;
190190

191191
public Dynamic() {
192-
super(String.class, false);
192+
super(String.class);
193193
_dynamicSerializers = PropertySerializerMap.emptyForProperties();
194194
}
195195

@@ -243,7 +243,7 @@ protected JsonSerializer<Object> _findAndAddDynamic(PropertySerializerMap map,
243243
*/
244244
public static class StringKeySerializer extends StdSerializer<Object>
245245
{
246-
public StringKeySerializer() { super(String.class, false); }
246+
public StringKeySerializer() { super(String.class); }
247247

248248
@Override
249249
public void serialize(Object value, JsonGenerator g, SerializerProvider provider) throws IOException {
@@ -253,15 +253,13 @@ public void serialize(Object value, JsonGenerator g, SerializerProvider provider
253253

254254
/**
255255
* Specialized instance to use for Enum keys, as per [databind#1322]
256-
*
257-
* @since 2.8
258256
*/
259257
public static class EnumKeySerializer extends StdSerializer<Object>
260258
{
261259
protected final EnumValues _values;
262260

263261
protected EnumKeySerializer(Class<?> enumType, EnumValues values) {
264-
super(enumType, false);
262+
super(enumType);
265263
_values = values;
266264
}
267265

0 commit comments

Comments
 (0)