Skip to content

Commit 52ca177

Browse files
authored
Second half of #4337: deserializers (#4339)
1 parent 2d8b83b commit 52ca177

3 files changed

Lines changed: 32 additions & 15 deletions

File tree

src/main/java/com/fasterxml/jackson/databind/deser/std/ReferenceTypeDeserializer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanPro
6666
throws JsonMappingException
6767
{
6868
JsonDeserializer<?> deser = _valueDeserializer;
69+
// 23-Jan-2024, tatu: [databind#4337]: May have a content converter
70+
deser = findConvertingContentDeserializer(ctxt, property, deser);
6971
if (deser == null) {
7072
deser = ctxt.findContextualValueDeserializer(_fullType.getReferencedType(), property);
7173
} else { // otherwise directly assigned, probably not contextual yet:
7274
deser = ctxt.handleSecondaryContextualization(deser, property, _fullType.getReferencedType());
7375
}
76+
7477
TypeDeserializer typeDeser = _valueTypeDeserializer;
7578
if (typeDeser != null) {
7679
typeDeser = typeDeser.forProperty(property);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
216216
ser = provider.handlePrimaryContextualization(ser, property);
217217
}
218218
}
219-
// 23-Jan-2024, tatu: May have a content converter:
219+
// 23-Jan-2024, tatu: [databind#4337]: May have a content converter
220220
ser = findContextualConvertingSerializer(provider, property, ser);
221221

222222
// First, resolve wrt property, resolved serializers

src/test/java/com/fasterxml/jackson/databind/convert/ConvertingDeserializerTest.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import java.math.BigDecimal;
44
import java.util.*;
5+
import java.util.concurrent.atomic.AtomicReference;
56

67
import org.junit.jupiter.api.Test;
78

89
import com.fasterxml.jackson.databind.ObjectMapper;
9-
import com.fasterxml.jackson.databind.ObjectReader;
1010
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1111
import com.fasterxml.jackson.databind.util.StdConverter;
1212

@@ -87,6 +87,11 @@ static class PointWrapperMap {
8787
public Map<String,Point> values;
8888
}
8989

90+
static class PointWrapperReference {
91+
@JsonDeserialize(contentConverter=PointConverter.class)
92+
public AtomicReference<Point> ref;
93+
}
94+
9095
static class LowerCaser extends StdConverter<String, String>
9196
{
9297
@Override
@@ -132,7 +137,7 @@ static class Issue795Bean
132137
@Test
133138
public void testClassAnnotationSimple() throws Exception
134139
{
135-
ConvertingBean bean = objectReader(ConvertingBean.class).readValue("[1,2]");
140+
ConvertingBean bean = MAPPER.readerFor(ConvertingBean.class).readValue("[1,2]");
136141
assertNotNull(bean);
137142
assertEquals(1, bean.x);
138143
assertEquals(2, bean.y);
@@ -141,7 +146,7 @@ public void testClassAnnotationSimple() throws Exception
141146
@Test
142147
public void testClassAnnotationForLists() throws Exception
143148
{
144-
ConvertingBeanContainer container = objectReader(ConvertingBeanContainer.class)
149+
ConvertingBeanContainer container = MAPPER.readerFor(ConvertingBeanContainer.class)
145150
.readValue("{\"values\":[[1,2],[3,4]]}");
146151
assertNotNull(container);
147152
assertNotNull(container.values);
@@ -152,7 +157,7 @@ public void testClassAnnotationForLists() throws Exception
152157
@Test
153158
public void testPropertyAnnotationSimple() throws Exception
154159
{
155-
PointWrapper wrapper = objectReader(PointWrapper.class).readValue("{\"value\":[3,4]}");
160+
PointWrapper wrapper = MAPPER.readerFor(PointWrapper.class).readValue("{\"value\":[3,4]}");
156161
assertNotNull(wrapper);
157162
assertNotNull(wrapper.value);
158163
assertEquals(3, wrapper.value.x);
@@ -162,7 +167,7 @@ public void testPropertyAnnotationSimple() throws Exception
162167
@Test
163168
public void testPropertyAnnotationLowerCasing() throws Exception
164169
{
165-
LowerCaseText text = objectReader(LowerCaseText.class).readValue("{\"text\":\"Yay!\"}");
170+
LowerCaseText text = MAPPER.readerFor(LowerCaseText.class).readValue("{\"text\":\"Yay!\"}");
166171
assertNotNull(text);
167172
assertNotNull(text.text);
168173
assertEquals("yay!", text.text);
@@ -171,7 +176,7 @@ public void testPropertyAnnotationLowerCasing() throws Exception
171176
@Test
172177
public void testPropertyAnnotationArrayLC() throws Exception
173178
{
174-
LowerCaseTextArray texts = objectReader(LowerCaseTextArray.class).readValue("{\"texts\":[\"ABC\"]}");
179+
LowerCaseTextArray texts = MAPPER.readerFor(LowerCaseTextArray.class).readValue("{\"texts\":[\"ABC\"]}");
175180
assertNotNull(texts);
176181
assertNotNull(texts.texts);
177182
assertEquals(1, texts.texts.length);
@@ -181,7 +186,7 @@ public void testPropertyAnnotationArrayLC() throws Exception
181186
@Test
182187
public void testPropertyAnnotationForArrays() throws Exception
183188
{
184-
PointWrapperArray array = objectReader(PointWrapperArray.class)
189+
PointWrapperArray array = MAPPER.readerFor(PointWrapperArray.class)
185190
.readValue("{\"values\":[[4,5],[5,4]]}");
186191
assertNotNull(array);
187192
assertNotNull(array.values);
@@ -192,7 +197,7 @@ public void testPropertyAnnotationForArrays() throws Exception
192197
@Test
193198
public void testPropertyAnnotationForLists() throws Exception
194199
{
195-
PointWrapperList array = objectReader(PointWrapperList.class)
200+
PointWrapperList array = MAPPER.readerFor(PointWrapperList.class)
196201
.readValue("{\"values\":[[7,8],[8,7]]}");
197202
assertNotNull(array);
198203
assertNotNull(array.values);
@@ -203,7 +208,7 @@ public void testPropertyAnnotationForLists() throws Exception
203208
@Test
204209
public void testPropertyAnnotationForMaps() throws Exception
205210
{
206-
PointWrapperMap map = objectReader(PointWrapperMap.class)
211+
PointWrapperMap map = MAPPER.readerFor(PointWrapperMap.class)
207212
.readValue("{\"values\":{\"a\":[1,2]}}");
208213
assertNotNull(map);
209214
assertNotNull(map.values);
@@ -214,19 +219,28 @@ public void testPropertyAnnotationForMaps() throws Exception
214219
assertEquals(2, p.y);
215220
}
216221

222+
@Test
223+
public void testPropertyAnnotationForReferences() throws Exception
224+
{
225+
PointWrapperReference w = MAPPER.readerFor(PointWrapperReference.class)
226+
.readValue("{\"ref\": [1,2]}");
227+
assertNotNull(w);
228+
assertNotNull(w.ref);
229+
Point p = w.ref.get();
230+
assertNotNull(p);
231+
assertEquals(1, p.x);
232+
assertEquals(2, p.y);
233+
}
234+
217235
// [databind#795]
218236
@Test
219237
public void testConvertToAbstract() throws Exception
220238
{
221-
Issue795Bean bean = objectReader(Issue795Bean.class)
239+
Issue795Bean bean = MAPPER.readerFor(Issue795Bean.class)
222240
.readValue("{\"value\":\"1.25\"}");
223241
assertNotNull(bean.value);
224242
assertTrue(bean.value instanceof BigDecimal,
225243
"Type not BigDecimal but "+bean.value.getClass());
226244
assertEquals(new BigDecimal("1.25"), bean.value);
227245
}
228-
229-
private ObjectReader objectReader(Class<?> type) {
230-
return MAPPER.readerFor(type);
231-
}
232246
}

0 commit comments

Comments
 (0)