Skip to content

Commit

Permalink
Second half of #4337: deserializers
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 24, 2024
1 parent 2d8b83b commit a4c0a59
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanPro
throws JsonMappingException
{
JsonDeserializer<?> deser = _valueDeserializer;
// 23-Jan-2024, tatu: [databind#4337]: May have a content converter
deser = findConvertingContentDeserializer(ctxt, property, deser);
if (deser == null) {
deser = ctxt.findContextualValueDeserializer(_fullType.getReferencedType(), property);
} else { // otherwise directly assigned, probably not contextual yet:
deser = ctxt.handleSecondaryContextualization(deser, property, _fullType.getReferencedType());
}

TypeDeserializer typeDeser = _valueTypeDeserializer;
if (typeDeser != null) {
typeDeser = typeDeser.forProperty(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
ser = provider.handlePrimaryContextualization(ser, property);
}
}
// 23-Jan-2024, tatu: May have a content converter:
// 23-Jan-2024, tatu: [databind#4337]: May have a content converter
ser = findContextualConvertingSerializer(provider, property, ser);

// First, resolve wrt property, resolved serializers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.util.StdConverter;

Expand Down Expand Up @@ -87,6 +87,11 @@ static class PointWrapperMap {
public Map<String,Point> values;
}

static class PointWrapperReference {
@JsonDeserialize(contentConverter=PointConverter.class)
public AtomicReference<Point> ref;
}

static class LowerCaser extends StdConverter<String, String>
{
@Override
Expand Down Expand Up @@ -132,7 +137,7 @@ static class Issue795Bean
@Test
public void testClassAnnotationSimple() throws Exception
{
ConvertingBean bean = objectReader(ConvertingBean.class).readValue("[1,2]");
ConvertingBean bean = MAPPER.readerFor(ConvertingBean.class).readValue("[1,2]");
assertNotNull(bean);
assertEquals(1, bean.x);
assertEquals(2, bean.y);
Expand All @@ -141,7 +146,7 @@ public void testClassAnnotationSimple() throws Exception
@Test
public void testClassAnnotationForLists() throws Exception
{
ConvertingBeanContainer container = objectReader(ConvertingBeanContainer.class)
ConvertingBeanContainer container = MAPPER.readerFor(ConvertingBeanContainer.class)
.readValue("{\"values\":[[1,2],[3,4]]}");
assertNotNull(container);
assertNotNull(container.values);
Expand All @@ -152,7 +157,7 @@ public void testClassAnnotationForLists() throws Exception
@Test
public void testPropertyAnnotationSimple() throws Exception
{
PointWrapper wrapper = objectReader(PointWrapper.class).readValue("{\"value\":[3,4]}");
PointWrapper wrapper = MAPPER.readerFor(PointWrapper.class).readValue("{\"value\":[3,4]}");
assertNotNull(wrapper);
assertNotNull(wrapper.value);
assertEquals(3, wrapper.value.x);
Expand All @@ -162,7 +167,7 @@ public void testPropertyAnnotationSimple() throws Exception
@Test
public void testPropertyAnnotationLowerCasing() throws Exception
{
LowerCaseText text = objectReader(LowerCaseText.class).readValue("{\"text\":\"Yay!\"}");
LowerCaseText text = MAPPER.readerFor(LowerCaseText.class).readValue("{\"text\":\"Yay!\"}");
assertNotNull(text);
assertNotNull(text.text);
assertEquals("yay!", text.text);
Expand All @@ -171,7 +176,7 @@ public void testPropertyAnnotationLowerCasing() throws Exception
@Test
public void testPropertyAnnotationArrayLC() throws Exception
{
LowerCaseTextArray texts = objectReader(LowerCaseTextArray.class).readValue("{\"texts\":[\"ABC\"]}");
LowerCaseTextArray texts = MAPPER.readerFor(LowerCaseTextArray.class).readValue("{\"texts\":[\"ABC\"]}");
assertNotNull(texts);
assertNotNull(texts.texts);
assertEquals(1, texts.texts.length);
Expand All @@ -181,7 +186,7 @@ public void testPropertyAnnotationArrayLC() throws Exception
@Test
public void testPropertyAnnotationForArrays() throws Exception
{
PointWrapperArray array = objectReader(PointWrapperArray.class)
PointWrapperArray array = MAPPER.readerFor(PointWrapperArray.class)
.readValue("{\"values\":[[4,5],[5,4]]}");
assertNotNull(array);
assertNotNull(array.values);
Expand All @@ -192,7 +197,7 @@ public void testPropertyAnnotationForArrays() throws Exception
@Test
public void testPropertyAnnotationForLists() throws Exception
{
PointWrapperList array = objectReader(PointWrapperList.class)
PointWrapperList array = MAPPER.readerFor(PointWrapperList.class)
.readValue("{\"values\":[[7,8],[8,7]]}");
assertNotNull(array);
assertNotNull(array.values);
Expand All @@ -203,7 +208,7 @@ public void testPropertyAnnotationForLists() throws Exception
@Test
public void testPropertyAnnotationForMaps() throws Exception
{
PointWrapperMap map = objectReader(PointWrapperMap.class)
PointWrapperMap map = MAPPER.readerFor(PointWrapperMap.class)
.readValue("{\"values\":{\"a\":[1,2]}}");
assertNotNull(map);
assertNotNull(map.values);
Expand All @@ -214,19 +219,28 @@ public void testPropertyAnnotationForMaps() throws Exception
assertEquals(2, p.y);
}

@Test
public void testPropertyAnnotationForReferences() throws Exception
{
PointWrapperReference w = MAPPER.readerFor(PointWrapperReference.class)
.readValue("{\"ref\": [1,2]}");
assertNotNull(w);
assertNotNull(w.ref);
Point p = w.ref.get();
assertNotNull(p);
assertEquals(1, p.x);
assertEquals(2, p.y);
}

// [databind#795]
@Test
public void testConvertToAbstract() throws Exception
{
Issue795Bean bean = objectReader(Issue795Bean.class)
Issue795Bean bean = MAPPER.readerFor(Issue795Bean.class)
.readValue("{\"value\":\"1.25\"}");
assertNotNull(bean.value);
assertTrue(bean.value instanceof BigDecimal,
"Type not BigDecimal but "+bean.value.getClass());
assertEquals(new BigDecimal("1.25"), bean.value);
}

private ObjectReader objectReader(Class<?> type) {
return MAPPER.readerFor(type);
}
}

0 comments on commit a4c0a59

Please sign in to comment.