• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.convert;
2 
3 import java.math.BigDecimal;
4 import java.util.*;
5 
6 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
7 import com.fasterxml.jackson.databind.util.StdConverter;
8 
9 public class TestConvertingDeserializer
10 extends com.fasterxml.jackson.databind.BaseMapTest
11 {
12     @JsonDeserialize(converter=ConvertingBeanConverter.class)
13     static class ConvertingBean
14     {
15         protected int x, y;
16 
ConvertingBean(int x, int y)17         protected ConvertingBean(int x, int y) {
18             this.x = x;
19             this.y = y;
20         }
21     }
22 
23     static class Point
24     {
25         protected int x, y;
26 
Point(int v1, int v2)27         public Point(int v1, int v2) {
28             x = v1;
29             y = v2;
30         }
31     }
32 
33     static class ConvertingBeanContainer
34     {
35         public List<ConvertingBean> values;
36 
ConvertingBeanContainer()37         public ConvertingBeanContainer() { }
ConvertingBeanContainer(ConvertingBean... beans)38         public ConvertingBeanContainer(ConvertingBean... beans) {
39             values = Arrays.asList(beans);
40         }
41     }
42 
43     static class ConvertingBeanConverter extends StdConverter<int[],ConvertingBean>
44     {
45         @Override
convert(int[] values)46         public ConvertingBean convert(int[] values) {
47             return new ConvertingBean(values[0], values[1]);
48         }
49     }
50 
51     private static class PointConverter extends StdConverter<int[], Point>
52     {
convert(int[] value)53         @Override public Point convert(int[] value) {
54             return new Point(value[0], value[1]);
55         }
56     }
57 
58     static class PointWrapper {
59         @JsonDeserialize(converter=PointConverter.class)
60         public Point value;
61 
PointWrapper()62         protected PointWrapper() { }
PointWrapper(int x, int y)63         public PointWrapper(int x, int y) {
64             value = new Point(x, y);
65         }
66     }
67 
68     static class PointListWrapperArray {
69         @JsonDeserialize(contentConverter=PointConverter.class)
70         public Point[] values;
71     }
72 
73     static class PointListWrapperList {
74         @JsonDeserialize(contentConverter=PointConverter.class)
75         public List<Point> values;
76     }
77 
78     static class PointListWrapperMap {
79         @JsonDeserialize(contentConverter=PointConverter.class)
80         public Map<String,Point> values;
81     }
82 
83     static class LowerCaser extends StdConverter<String, String>
84     {
85         @Override
convert(String value)86         public String convert(String value) {
87             return value.toLowerCase();
88         }
89 
90     }
91 
92     static class LowerCaseText {
93         @JsonDeserialize(converter=LowerCaser.class)
94         public String text;
95     }
96 
97     static class LowerCaseTextArray {
98         @JsonDeserialize(contentConverter=LowerCaser.class)
99         public String[] texts;
100     }
101 
102     // for [databind#795]
103 
104     static class ToNumberConverter extends StdConverter<String,Number>
105     {
106         @Override
convert(String value)107         public Number convert(String value) {
108             return new BigDecimal(value);
109         }
110     }
111 
112     static class Issue795Bean
113     {
114         @JsonDeserialize(converter=ToNumberConverter.class)
115         public Number value;
116     }
117 
118     /*
119     /**********************************************************
120     /* Test methods
121     /**********************************************************
122      */
123 
testClassAnnotationSimple()124     public void testClassAnnotationSimple() throws Exception
125     {
126         ConvertingBean bean = objectReader(ConvertingBean.class).readValue("[1,2]");
127         assertNotNull(bean);
128         assertEquals(1, bean.x);
129         assertEquals(2, bean.y);
130     }
131 
testClassAnnotationForLists()132     public void testClassAnnotationForLists() throws Exception
133     {
134         ConvertingBeanContainer container = objectReader(ConvertingBeanContainer.class)
135                 .readValue("{\"values\":[[1,2],[3,4]]}");
136         assertNotNull(container);
137         assertNotNull(container.values);
138         assertEquals(2, container.values.size());
139         assertEquals(4, container.values.get(1).y);
140     }
141 
testPropertyAnnotationSimple()142     public void testPropertyAnnotationSimple() throws Exception
143     {
144         PointWrapper wrapper = objectReader(PointWrapper.class).readValue("{\"value\":[3,4]}");
145         assertNotNull(wrapper);
146         assertNotNull(wrapper.value);
147         assertEquals(3, wrapper.value.x);
148         assertEquals(4, wrapper.value.y);
149     }
150 
testPropertyAnnotationLowerCasing()151     public void testPropertyAnnotationLowerCasing() throws Exception
152     {
153         LowerCaseText text = objectReader(LowerCaseText.class).readValue("{\"text\":\"Yay!\"}");
154         assertNotNull(text);
155         assertNotNull(text.text);
156         assertEquals("yay!", text.text);
157     }
158 
testPropertyAnnotationArrayLC()159     public void testPropertyAnnotationArrayLC() throws Exception
160     {
161         LowerCaseTextArray texts = objectReader(LowerCaseTextArray.class).readValue("{\"texts\":[\"ABC\"]}");
162         assertNotNull(texts);
163         assertNotNull(texts.texts);
164         assertEquals(1, texts.texts.length);
165         assertEquals("abc", texts.texts[0]);
166     }
167 
testPropertyAnnotationForArrays()168     public void testPropertyAnnotationForArrays() throws Exception
169     {
170         PointListWrapperArray array = objectReader(PointListWrapperArray.class)
171                 .readValue("{\"values\":[[4,5],[5,4]]}");
172         assertNotNull(array);
173         assertNotNull(array.values);
174         assertEquals(2, array.values.length);
175         assertEquals(5, array.values[1].x);
176     }
177 
testPropertyAnnotationForLists()178     public void testPropertyAnnotationForLists() throws Exception
179     {
180         PointListWrapperList array = objectReader(PointListWrapperList.class)
181                 .readValue("{\"values\":[[7,8],[8,7]]}");
182         assertNotNull(array);
183         assertNotNull(array.values);
184         assertEquals(2, array.values.size());
185         assertEquals(7, array.values.get(0).x);
186     }
187 
testPropertyAnnotationForMaps()188     public void testPropertyAnnotationForMaps() throws Exception
189     {
190         PointListWrapperMap map = objectReader(PointListWrapperMap.class)
191                 .readValue("{\"values\":{\"a\":[1,2]}}");
192         assertNotNull(map);
193         assertNotNull(map.values);
194         assertEquals(1, map.values.size());
195         Point p = map.values.get("a");
196         assertNotNull(p);
197         assertEquals(1, p.x);
198         assertEquals(2, p.y);
199     }
200 
201     // [databind#795]
testConvertToAbstract()202     public void testConvertToAbstract() throws Exception
203     {
204         Issue795Bean bean = objectReader(Issue795Bean.class)
205                 .readValue("{\"value\":\"1.25\"}");
206         assertNotNull(bean.value);
207         assertTrue("Type not BigDecimal but "+bean.value.getClass(),
208                 bean.value instanceof BigDecimal);
209         assertEquals(new BigDecimal("1.25"), bean.value);
210     }
211 }
212