Skip to content

Commit b2ab29c

Browse files
committed
A further tweak to handle a corner case of #3647
1 parent 09166a0 commit b2ab29c

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public JsonSerializer<Object> createKeySerializer(SerializerProvider ctxt,
244244
config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
245245
}
246246
// null -> no TypeSerializer for key-serializer use case
247-
ser = new JsonValueSerializer(acc, null, delegate);
247+
ser = JsonValueSerializer.construct(config, acc, null, delegate);
248248
} else {
249249
ser = StdKeySerializers.getFallbackKeySerializer(config, keyType.getRawClass(),
250250
beanDesc.getClassInfo());
@@ -405,7 +405,8 @@ protected final JsonSerializer<?> findSerializerByAnnotations(SerializerProvider
405405
if (typeSerializer == null) {
406406
typeSerializer = createTypeSerializer(prov.getConfig(), valueType);
407407
}
408-
return new JsonValueSerializer(valueAccessor, typeSerializer, valueSerializer);
408+
return JsonValueSerializer.construct(prov.getConfig(), valueAccessor,
409+
typeSerializer, valueSerializer);
409410
}
410411
// No well-known annotations...
411412
return null;

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

+45-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.lang.reflect.InvocationTargetException;
55
import java.lang.reflect.Type;
6+
import java.util.Collections;
67
import java.util.LinkedHashSet;
78
import java.util.Set;
89

@@ -73,6 +74,14 @@ public class JsonValueSerializer
7374
*/
7475
protected final boolean _forceTypeInformation;
7576

77+
/**
78+
* Names of properties to ignore from Value class accessed using
79+
* accessor.
80+
*
81+
* @since 2.16
82+
*/
83+
protected final Set<String> _ignoredProperties;
84+
7685
/**
7786
* If value type cannot be statically determined, mapping from
7887
* runtime value types to serializers are cached in this object.
@@ -97,7 +106,8 @@ public class JsonValueSerializer
97106
*/
98107
@SuppressWarnings("unchecked")
99108
public JsonValueSerializer(AnnotatedMember accessor,
100-
TypeSerializer vts, JsonSerializer<?> ser)
109+
TypeSerializer vts, JsonSerializer<?> ser,
110+
Set<String> ignoredProperties)
101111
{
102112
super(accessor.getType());
103113
_accessor = accessor;
@@ -106,15 +116,23 @@ public JsonValueSerializer(AnnotatedMember accessor,
106116
_valueSerializer = (JsonSerializer<Object>) ser;
107117
_property = null;
108118
_forceTypeInformation = true; // gets reconsidered when we are contextualized
119+
_ignoredProperties = ignoredProperties;
109120
_dynamicSerializers = PropertySerializerMap.emptyForProperties();
110121
}
111122

123+
@Deprecated // since 2.16
124+
public JsonValueSerializer(AnnotatedMember accessor,
125+
TypeSerializer vts, JsonSerializer<?> ser)
126+
{
127+
this(accessor, vts, ser, Collections.emptySet());
128+
}
129+
112130
/**
113131
* @deprecated Since 2.12
114132
*/
115133
@Deprecated
116134
public JsonValueSerializer(AnnotatedMember accessor, JsonSerializer<?> ser) {
117-
this(accessor, null, ser);
135+
this(accessor, null, ser, Collections.emptySet());
118136
}
119137

120138
// @since 2.12
@@ -129,9 +147,24 @@ public JsonValueSerializer(JsonValueSerializer src, BeanProperty property,
129147
_valueSerializer = (JsonSerializer<Object>) ser;
130148
_property = property;
131149
_forceTypeInformation = forceTypeInfo;
150+
_ignoredProperties = src._ignoredProperties;
132151
_dynamicSerializers = PropertySerializerMap.emptyForProperties();
133152
}
134153

154+
/**
155+
* @since 2.16
156+
*/
157+
public static JsonValueSerializer construct(SerializationConfig config,
158+
AnnotatedMember accessor,
159+
TypeSerializer vts, JsonSerializer<?> ser)
160+
{
161+
JsonIgnoreProperties.Value ignorals = config.getAnnotationIntrospector()
162+
.findPropertyIgnoralByName(config, accessor);
163+
final Set<String> ignoredProperties = ignorals.findIgnoredForSerialization();
164+
ser = _withIgnoreProperties(ser, ignoredProperties);
165+
return new JsonValueSerializer(accessor, vts, ser, ignoredProperties);
166+
}
167+
135168
@SuppressWarnings("unchecked")
136169
private final static Class<Object> _notNullClass(Class<?> cls) {
137170
return (cls == null) ? Object.class : (Class<Object>) cls;
@@ -205,6 +238,8 @@ public JsonSerializer<?> createContextual(SerializerProvider ctxt,
205238
*/
206239
// 05-Sep-2013, tatu: I _think_ this can be considered a primary property...
207240
ser = ctxt.findPrimaryPropertySerializer(_valueType, property);
241+
ser = _withIgnoreProperties(ser, _ignoredProperties);
242+
208243
/* 09-Dec-2010, tatu: Turns out we must add special handling for
209244
* cases where "native" (aka "natural") type is being serialized,
210245
* using standard serializer
@@ -416,12 +451,13 @@ protected JsonSerializer<Object> _findDynamicSerializer(SerializerProvider ctxt,
416451
if (_valueType.hasGenericTypes()) {
417452
final JavaType fullType = ctxt.constructSpecializedType(_valueType, valueClass);
418453
serializer = ctxt.findPrimaryPropertySerializer(fullType, _property);
454+
serializer = _withIgnoreProperties(serializer, _ignoredProperties);
419455
PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers.addSerializer(fullType, serializer);
420456
_dynamicSerializers = result.map;
421457
} else {
422458
serializer = ctxt.findPrimaryPropertySerializer(valueClass, _property);
423459
// [databind#3647] : Support `@JsonIgnoreProperties` to work with `@JsonValue`
424-
serializer = (JsonSerializer<Object>) _withIgnoreProperties(ctxt, serializer);
460+
serializer = _withIgnoreProperties(serializer, _ignoredProperties);
425461
PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers.addSerializer(valueClass, serializer);
426462
_dynamicSerializers = result.map;
427463
}
@@ -451,18 +487,17 @@ protected JsonSerializer<Object> _findDynamicSerializer(SerializerProvider ctxt,
451487
/**
452488
* Internal helper that configures the provided {@code ser} to ignore properties specified by {@link JsonIgnoreProperties}.
453489
*
454-
* @param ctxt For introspection.
455490
* @param ser Serializer to be configured
456491
* @return Configured serializer with specified properties ignored
457492
* @since 2.16
458493
*/
459494
@SuppressWarnings("unchecked")
460-
protected JsonSerializer<Object> _withIgnoreProperties(SerializerProvider ctxt, JsonSerializer<?> ser) {
461-
JsonIgnoreProperties.Value ignorals = ctxt.getAnnotationIntrospector()
462-
.findPropertyIgnoralByName(ctxt.getConfig(), _accessor);
463-
Set<String> ignored = ignorals.findIgnoredForSerialization();
464-
if (!ignored.isEmpty()) {
465-
ser = ser.withIgnoredProperties(ignored);
495+
protected static JsonSerializer<Object> _withIgnoreProperties(JsonSerializer<?> ser,
496+
Set<String> ignoredProperties) {
497+
if (ser != null) {
498+
if (!ignoredProperties.isEmpty()) {
499+
ser = ser.withIgnoredProperties(ignoredProperties);
500+
}
466501
}
467502
return (JsonSerializer<Object>) ser;
468503
}

src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonValueIgnore3647Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// [databind#3647] : Support @JsonIgnoreProperties to work with @JsonValue
99
public class JsonValueIgnore3647Test extends BaseMapTest
1010
{
11-
static class Foo3647 {
11+
final static class Foo3647 {
1212
public String p1 = "hello";
1313
public String p2 = "world";
1414
}

0 commit comments

Comments
 (0)