diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestCachingOfDeser.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestCachingOfDeser.java new file mode 100644 index 0000000000..9ac9dff1b3 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestCachingOfDeser.java @@ -0,0 +1,109 @@ +package com.fasterxml.jackson.databind.deser; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.BaseMapTest; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +@SuppressWarnings("serial") +public class TestCachingOfDeser extends BaseMapTest +{ + // For [databind#735] + public static class TestMapNoCustom { + + public Map map; + } + + public static class TestMapWithCustom { + + @JsonDeserialize(contentUsing = CustomDeserializer735.class) + public Map map; + } + + public static class TestListWithCustom { + @JsonDeserialize(contentUsing = CustomDeserializer735.class) + public List list; + } + + public static class TestListNoCustom { + public List list; + } + + public static class CustomDeserializer735 extends StdDeserializer { + public CustomDeserializer735() { + super(Integer.class); + } + + @Override + public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + return 100 * p.getValueAsInt(); + } + } + + /* + /********************************************************** + /* Unit tests + /********************************************************** + */ + + final static String MAP_INPUT = "{\"map\":{\"a\":1}}"; + final static String LIST_INPUT = "{\"list\":[1]}"; + + + // Ok: first, use custom-annotated instance first, then standard + public void testCustomMapCaching1() throws Exception + { + + ObjectMapper mapper = new ObjectMapper(); + TestMapWithCustom mapC = mapper.readValue(MAP_INPUT, TestMapWithCustom.class); + TestMapNoCustom mapStd = mapper.readValue(MAP_INPUT, TestMapNoCustom.class); + + assertNotNull(mapC.map); + assertNotNull(mapStd.map); + assertEquals(Integer.valueOf(100), mapC.map.get("a")); + assertEquals(Integer.valueOf(1), mapStd.map.get("a")); + } + + // And then standard first, custom next + public void testCustomMapCaching2() throws Exception + { + ObjectMapper mapper = new ObjectMapper(); + TestMapNoCustom mapStd = mapper.readValue(MAP_INPUT, TestMapNoCustom.class); + TestMapWithCustom mapC = mapper.readValue(MAP_INPUT, TestMapWithCustom.class); + + assertNotNull(mapStd.map); + assertNotNull(mapC.map); + assertEquals(Integer.valueOf(1), mapStd.map.get("a")); + assertEquals(Integer.valueOf(100), mapC.map.get("a")); + } + + // Ok: first, use custom-annotated instance first, then standard + public void testCustomListCaching1() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + TestListWithCustom listC = mapper.readValue(LIST_INPUT, TestListWithCustom.class); + TestListNoCustom listStd = mapper.readValue(LIST_INPUT, TestListNoCustom.class); + + assertNotNull(listC.list); + assertNotNull(listStd.list); + assertEquals(Integer.valueOf(100), listC.list.get(0)); + assertEquals(Integer.valueOf(1), listStd.list.get(0)); + } + + // First custom-annotated, then standard + public void testCustomListCaching2() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + TestListNoCustom listStd = mapper.readValue(LIST_INPUT, TestListNoCustom.class); + TestListWithCustom listC = mapper.readValue(LIST_INPUT, TestListWithCustom.class); + + assertNotNull(listC.list); + assertNotNull(listStd.list); + assertEquals(Integer.valueOf(100), listC.list.get(0)); + assertEquals(Integer.valueOf(1), listStd.list.get(0)); + } +} diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestCustomDeserializers.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestCustomDeserializers.java index 1b7a1e4c5e..ce3e76c22f 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/TestCustomDeserializers.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestCustomDeserializers.java @@ -231,34 +231,6 @@ public JsonDeserializer createContextual(DeserializationContext ctxt, BeanPro } } - // For [databind#735] - public static class TestMapBean735 { - - @JsonDeserialize(contentUsing = CustomDeserializer735.class) - public Map map1; - - public Map map2; - } - - public static class TestListBean735 { - - @JsonDeserialize(contentUsing = CustomDeserializer735.class) - public List list1; - - public List list2; - } - - public static class CustomDeserializer735 extends StdDeserializer { - public CustomDeserializer735() { - super(Integer.class); - } - - @Override - public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - return 100 * p.getValueAsInt(); - } - } - /* /********************************************************** /* Unit tests @@ -360,21 +332,4 @@ public void testContextReadValue() throws Exception assertNotNull(w.value.inner); assertEquals(-13, w.value.inner.x); } - - // [databind#735]: erroneous application of custom deserializer - public void testCustomMapValueDeser735() throws Exception { - String json = "{\"map1\":{\"a\":1},\"map2\":{\"a\":1}}"; - TestMapBean735 bean = MAPPER.readValue(json, TestMapBean735.class); - - assertEquals(100, bean.map1.get("a").intValue()); - assertEquals(1, bean.map2.get("a").intValue()); - } - - public void testCustomListValueDeser735() throws Exception { - String json = "{\"list1\":[1],\"list2\":[1]}"; - TestListBean735 bean = MAPPER.readValue(json, TestListBean735.class); - - assertEquals(100, bean.list1.get(0).intValue()); - assertEquals(1, bean.list2.get(0).intValue()); - } }