Skip to content

Commit ae77a0c

Browse files
authored
Support @JsonIgnoreProperties to work with @JsonValue (#4070)
1 parent e03eaf0 commit ae77a0c

File tree

5 files changed

+141
-38
lines changed

5 files changed

+141
-38
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.util.Iterator;
5+
import java.util.Set;
56

67
import com.fasterxml.jackson.core.*;
78

@@ -104,6 +105,18 @@ public JsonSerializer<?> withFilterId(Object filterId) {
104105
return this;
105106
}
106107

108+
/**
109+
* Mutant factory method called to create a new instance after excluding specified set of
110+
* properties by name, if there is any.
111+
*
112+
* @return Serializer instance that without specified set of properties to ignore (if any)
113+
* @param ignoredProperties Set of property names to ignore for serialization;
114+
* @since 2.16
115+
*/
116+
public JsonSerializer<?> withIgnoredProperties(Set<String> ignoredProperties) {
117+
return this;
118+
}
119+
107120
/*
108121
/**********************************************************
109122
/* Serialization methods

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ protected BeanSerializerBase withProperties(BeanPropertyWriter[] properties,
126126
return new BeanSerializer(this, properties, filteredProperties);
127127
}
128128

129+
@Override // @since 2.16
130+
public JsonSerializer<?> withIgnoredProperties(Set<String> toIgnore) {
131+
return new BeanSerializer(this, toIgnore, null);
132+
}
133+
129134
/**
130135
* Implementation has to check whether as-array serialization
131136
* is possible reliably; if (and only if) so, will construct

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.LinkedHashSet;
77
import java.util.Set;
88

9+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
910
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
1011

1112
import com.fasterxml.jackson.core.*;
@@ -418,6 +419,8 @@ protected JsonSerializer<Object> _findDynamicSerializer(SerializerProvider ctxt,
418419
_dynamicSerializers = result.map;
419420
} else {
420421
serializer = ctxt.findPrimaryPropertySerializer(valueClass, _property);
422+
// [databind#3647] : Support `@JsonIgnoreProperties` to work with `@JsonValue`
423+
serializer = (JsonSerializer<Object>) _withIgnoreProperties(ctxt, serializer);
421424
PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers.addSerializer(valueClass, serializer);
422425
_dynamicSerializers = result.map;
423426
}
@@ -444,6 +447,24 @@ protected JsonSerializer<Object> _findDynamicSerializer(SerializerProvider ctxt,
444447
*/
445448
}
446449

450+
/**
451+
* Internal helper that configures the provided {@code ser} to ignore properties specified by {@link JsonIgnoreProperties}.
452+
*
453+
* @param ctxt For introspection.
454+
* @param ser Serializer to be configured
455+
* @return Configured serializer with specified properties ignored
456+
* @since 2.16
457+
*/
458+
private JsonSerializer<?> _withIgnoreProperties(SerializerProvider ctxt, JsonSerializer<?> ser) {
459+
final AnnotationIntrospector ai = ctxt.getAnnotationIntrospector();
460+
final SerializationConfig config = ctxt.getConfig();
461+
462+
JsonIgnoreProperties.Value ignorals = ai.findPropertyIgnoralByName(config, _accessor);
463+
Set<String> ignored = ignorals.findIgnoredForSerialization();
464+
ser = ser.withIgnoredProperties(ignored);
465+
return ser;
466+
}
467+
447468
/*
448469
/**********************************************************
449470
/* Standard method overrides
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.fasterxml.jackson.databind.ser.filter;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
import com.fasterxml.jackson.databind.BaseMapTest;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
8+
// [databind#3647] : Support @JsonIgnoreProperties to work with @JsonValue
9+
public class JsonValueIgnore3647Test extends BaseMapTest
10+
{
11+
static class Foo3647 {
12+
public String p1 = "hello";
13+
public String p2 = "world";
14+
}
15+
16+
static class Bar3647 {
17+
@JsonValue
18+
@JsonIgnoreProperties("p1")
19+
public Foo3647 getFoo() {
20+
return new Foo3647();
21+
}
22+
}
23+
24+
@JsonIgnoreProperties({"a"})
25+
static class Bean3647 {
26+
public String a = "hello";
27+
public String b = "world";
28+
}
29+
30+
static class Container3647 {
31+
@JsonValue
32+
@JsonIgnoreProperties("b")
33+
public Bean3647 getBean() {
34+
return new Bean3647();
35+
}
36+
}
37+
38+
static class Base3647 {
39+
public String a = "hello";
40+
public String b = "world";
41+
}
42+
43+
static class BaseContainer3647 {
44+
@JsonValue
45+
public Base3647 getBean() {
46+
return new Base3647();
47+
}
48+
}
49+
50+
static class MixinContainer3647 {
51+
@JsonIgnoreProperties("b")
52+
public Base3647 getBean() {
53+
return new Base3647();
54+
}
55+
}
56+
57+
@JsonIgnoreProperties({"a", "b"})
58+
static class Mixin3647 {
59+
public String a = "hello";
60+
public String b = "world";
61+
}
62+
63+
/*
64+
/**********************************************************************
65+
/* Test methods
66+
/**********************************************************************
67+
*/
68+
69+
private final ObjectMapper MAPPER = newJsonMapper();
70+
71+
public void testIgnorePropsAndJsonValueAtSameLevel() throws Exception
72+
{
73+
assertEquals("{\"p2\":\"world\"}",
74+
MAPPER.writeValueAsString(new Bar3647()));
75+
}
76+
77+
public void testUnionOfIgnorals() throws Exception
78+
{
79+
assertEquals("{}",
80+
MAPPER.writeValueAsString(new Container3647()));
81+
}
82+
83+
public void testMixinContainerAndJsonValue() throws Exception
84+
{
85+
ObjectMapper mapper = jsonMapperBuilder()
86+
.addMixIn(BaseContainer3647.class, MixinContainer3647.class)
87+
.build();
88+
89+
assertEquals("{\"a\":\"hello\"}",
90+
mapper.writeValueAsString(new BaseContainer3647()));
91+
}
92+
93+
public void testMixinAndJsonValue() throws Exception
94+
{
95+
ObjectMapper mapper = jsonMapperBuilder()
96+
.addMixIn(Base3647.class, Mixin3647.class)
97+
.build();
98+
99+
assertEquals("{}",
100+
mapper.writeValueAsString(new Base3647()));
101+
}
102+
}

src/test/java/com/fasterxml/jackson/failing/JsonValueIgnore3647Test.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)