• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.deser;
2 
3 import java.io.IOException;
4 import java.util.List;
5 import java.util.Map;
6 
7 import com.fasterxml.jackson.core.JsonParser;
8 import com.fasterxml.jackson.databind.BaseMapTest;
9 import com.fasterxml.jackson.databind.DeserializationContext;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
12 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
13 
14 @SuppressWarnings("serial")
15 public class TestCachingOfDeser extends BaseMapTest
16 {
17     // For [databind#735]
18     public static class TestMapNoCustom {
19 
20         public Map<String, Integer> map;
21     }
22 
23     public static class TestMapWithCustom {
24 
25         @JsonDeserialize(contentUsing = CustomDeserializer735.class)
26         public Map<String, Integer> map;
27     }
28 
29     public static class TestListWithCustom {
30         @JsonDeserialize(contentUsing = CustomDeserializer735.class)
31         public List<Integer> list;
32     }
33 
34     public static class TestListNoCustom {
35         public List<Integer> list;
36     }
37 
38     public static class CustomDeserializer735 extends StdDeserializer<Integer> {
CustomDeserializer735()39         public CustomDeserializer735() {
40             super(Integer.class);
41         }
42 
43         @Override
deserialize(JsonParser p, DeserializationContext ctxt)44         public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
45             return 100 * p.getValueAsInt();
46         }
47     }
48 
49     /*
50     /**********************************************************
51     /* Unit tests
52     /**********************************************************
53      */
54 
55     final static String MAP_INPUT = "{\"map\":{\"a\":1}}";
56     final static String LIST_INPUT = "{\"list\":[1]}";
57 
58 
59     // Ok: first, use custom-annotated instance first, then standard
testCustomMapCaching1()60     public void testCustomMapCaching1() throws Exception
61     {
62 
63         ObjectMapper mapper = new ObjectMapper();
64         TestMapWithCustom mapC = mapper.readValue(MAP_INPUT, TestMapWithCustom.class);
65         TestMapNoCustom mapStd = mapper.readValue(MAP_INPUT, TestMapNoCustom.class);
66 
67         assertNotNull(mapC.map);
68         assertNotNull(mapStd.map);
69         assertEquals(Integer.valueOf(100), mapC.map.get("a"));
70         assertEquals(Integer.valueOf(1), mapStd.map.get("a"));
71     }
72 
73     // And then standard first, custom next
testCustomMapCaching2()74     public void testCustomMapCaching2() throws Exception
75     {
76         ObjectMapper mapper = new ObjectMapper();
77         TestMapNoCustom mapStd = mapper.readValue(MAP_INPUT, TestMapNoCustom.class);
78         TestMapWithCustom mapC = mapper.readValue(MAP_INPUT, TestMapWithCustom.class);
79 
80         assertNotNull(mapStd.map);
81         assertNotNull(mapC.map);
82         assertEquals(Integer.valueOf(1), mapStd.map.get("a"));
83         assertEquals(Integer.valueOf(100), mapC.map.get("a"));
84     }
85 
86     // Ok: first, use custom-annotated instance first, then standard
testCustomListCaching1()87     public void testCustomListCaching1() throws Exception {
88         ObjectMapper mapper = new ObjectMapper();
89         TestListWithCustom listC = mapper.readValue(LIST_INPUT, TestListWithCustom.class);
90         TestListNoCustom listStd = mapper.readValue(LIST_INPUT, TestListNoCustom.class);
91 
92         assertNotNull(listC.list);
93         assertNotNull(listStd.list);
94         assertEquals(Integer.valueOf(100), listC.list.get(0));
95         assertEquals(Integer.valueOf(1), listStd.list.get(0));
96     }
97 
98     // First custom-annotated, then standard
testCustomListCaching2()99     public void testCustomListCaching2() throws Exception {
100         ObjectMapper mapper = new ObjectMapper();
101         TestListNoCustom listStd = mapper.readValue(LIST_INPUT, TestListNoCustom.class);
102         TestListWithCustom listC = mapper.readValue(LIST_INPUT, TestListWithCustom.class);
103 
104         assertNotNull(listC.list);
105         assertNotNull(listStd.list);
106         assertEquals(Integer.valueOf(100), listC.list.get(0));
107         assertEquals(Integer.valueOf(1), listStd.list.get(0));
108     }
109 }
110