Debian Codegraph @

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/impl/CustomValueReadersTest.java

raw
1 package com.fasterxml.jackson.jr.ob.impl;
2
3 import java.io.IOException;
4 import java.util.Map;
5
6 import com.fasterxml.jackson.core.JsonParser;
7 import com.fasterxml.jackson.jr.ob.JSON;
8 import com.fasterxml.jackson.jr.ob.JSONObjectException;
9 import com.fasterxml.jackson.jr.ob.TestBase;
10 import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
11 import com.fasterxml.jackson.jr.ob.api.ValueReader;
12
13 public class CustomValueReadersTest extends TestBase
14 {
15 static class CustomValue {
16 public int value;
17
18 // 2nd arg just to avoid discovery
19 public CustomValue(int v, boolean b) {
20 // and to ensure it goes through constructor, add 1
21 value = v + 1;
22 }
23 }
24
25 static class CustomValueBean {
26 public CustomValue custom;
27
28 protected CustomValueBean() { }
29 public CustomValueBean(int v) {
30 custom = new CustomValue(v, false);
31 }
32 }
33
34 enum ABC {
35 A, B, C, DEF;
36 }
37
38 static class CustomValueReader extends ValueReader {
39 private final int delta;
40
41 public CustomValueReader(int d) {
42 super(CustomValue.class);
43 delta = d;
44 }
45
46 @Override
47 public Object read(JSONReader reader, JsonParser p) throws IOException {
48 return new CustomValue(p.getIntValue() + delta, true);
49 }
50
51 // Base class impl should be fine, although we'd use this for optimal
52 /*
53 @Override
54 public Object readNext(JSONReader reader, JsonParser p) throws IOException {
55 return new CustomValue(p.nextIntValue(-1), true);
56 }
57 */
58 }
59
60 static class ABCValueReader extends ValueReader {
61 public ABCValueReader() {
62 super(ABC.class);
63 }
64
65 @Override
66 public Object read(JSONReader reader, JsonParser p) throws IOException {
67 final String str = p.getText();
68 if ("n/a".equals(str)) {
69 return ABC.DEF;
70 }
71 return ABC.valueOf(str);
72 }
73 }
74
75 static class CapStringReader extends ValueReader {
76 public CapStringReader() {
77 super(String.class);
78 }
79
80 @Override
81 public Object read(JSONReader reader, JsonParser p) throws IOException {
82 return p.getText().toUpperCase();
83 }
84 }
85
86 static class OverrideStringReader extends ValueReader {
87 final String _value;
88
89 public OverrideStringReader(String str) {
90 super(String.class);
91 _value = str;
92 }
93
94 @Override
95 public Object read(JSONReader reader, JsonParser p) throws IOException {
96 p.skipChildren();
97 return _value;
98 }
99 }
100
101 static class CustomReaders extends ReaderWriterProvider {
102 final int delta;
103
104 public CustomReaders(int d) {
105 delta = d;
106 }
107
108 @Override
109 public ValueReader findValueReader(JSONReader readContext, Class<?> type) {
110 if (type.equals(CustomValue.class)) {
111 return new CustomValueReader(delta);
112 } else if (type.equals(ABC.class)) {
113 return new ABCValueReader();
114 }
115 return null;
116 }
117 }
118
119 static class CapStringReaderProvider extends ReaderWriterProvider {
120 @Override
121 public ValueReader findValueReader(JSONReader readContext, Class<?> type) {
122 if (type.equals(String.class)) {
123 return new CapStringReader();
124 }
125 return null;
126 }
127 }
128
129 static class OverrideStringReaderProvider extends ReaderWriterProvider {
130 final ValueReader vr;
131
132 public OverrideStringReaderProvider(String str) {
133 vr = new OverrideStringReader(str);
134 }
135
136 @Override
137 public ValueReader findValueReader(JSONReader readContext, Class<?> type) {
138 if (type.equals(String.class)) {
139 return vr;
140 }
141 return null;
142 }
143 }
144
145 static class Point {
146 public int _x, _y;
147
148 public Point(int x, int y) {
149 _x = x;
150 _y = y;
151 }
152 }
153
154 static class PointReader extends ValueReader {
155 public PointReader() { super(Point.class); }
156
157 @Override
158 public Object read(JSONReader reader, JsonParser p) throws IOException {
159 Map<String, Object> map = reader.readMap();
160 return new Point((Integer) map.get("x"), (Integer) map.get("y"));
161 }
162 }
163
164 static class PointReaderProvider extends ReaderWriterProvider {
165 @Override
166 public ValueReader findValueReader(JSONReader readContext, Class<?> type) {
167 if (type == Point.class) {
168 return new PointReader();
169 }
170 return null;
171 }
172 }
173
174 static class NoOpProvider extends ReaderWriterProvider {
175 }
176
177 /*
178 /**********************************************************************
179 /* Test methdods
180 /**********************************************************************
181 */
182
183 public void testCustomBeanReader() throws Exception
184 {
185 // First: without handler, will fail to map
186 try {
187 JSON.std.beanFrom(CustomValue.class, "123");
188 fail("Should not pass");
189 } catch (JSONObjectException e) {
190 verifyException(e, ".CustomValue");
191 verifyException(e, "constructor to use");
192 }
193
194 // then with custom, should be fine
195 JSON json = jsonWithProvider(new CustomReaders(0));
196 CustomValue v = json.beanFrom(CustomValue.class, "123");
197 assertEquals(124, v.value);
198
199 // similarly with wrapper
200 CustomValueBean bean = json.beanFrom(CustomValueBean.class,
201 aposToQuotes("{ 'custom' : 137 }"));
202 assertEquals(138, bean.custom.value);
203
204 // but also ensure we can change registered handler(s)
205 JSON json2 = jsonWithProvider(new CustomReaders(100));
206 v = json2.beanFrom(CustomValue.class, "123");
207 assertEquals(224, v.value);
208 }
209
210 public void testChainedCustomBeanReaders() throws Exception
211 {
212 JSON json = jsonWithProviders(new CustomReaders(0),
213 new CustomReaders(100));
214 CustomValue v = json.beanFrom(CustomValue.class, "69");
215 assertEquals(70, v.value);
216
217 json = jsonWithProviders(new CustomReaders(100),
218 new CustomReaders(0));
219 v = json.beanFrom(CustomValue.class, "72");
220 assertEquals(173, v.value);
221 }
222
223 public void testCustomEnumReader() throws Exception
224 {
225 // First: without handler, will fail to map
226 try {
227 JSON.std.beanFrom(ABC.class, quote("n/a"));
228 fail("Should not pass");
229 } catch (JSONObjectException e) {
230 verifyException(e, "Failed to find Enum of type");
231 }
232
233 // then with custom, should be fine
234 JSON json = jsonWithProvider(new CustomReaders(0));
235 ABC v = json.beanFrom(ABC.class, quote("n/a"));
236 assertEquals(ABC.DEF, v);
237
238 // but if we remove, again error
239 JSON json2 = jsonWithProvider((ReaderWriterProvider) null);
240 try {
241 json2.beanFrom(ABC.class, quote("n/a"));
242 fail("Should not pass");
243 } catch (JSONObjectException e) {
244 verifyException(e, "Failed to find Enum of type");
245 }
246 }
247
248 // Even more fun, override default String deserializer!
249 public void testCustomStringReader() throws Exception
250 {
251 String allCaps = jsonWithProvider(new CapStringReaderProvider())
252 .beanFrom(String.class, quote("Some text"));
253 assertEquals("SOME TEXT", allCaps);
254 }
255
256 public void testChainedStringReaders() throws Exception {
257 String result = jsonWithProviders(new CapStringReaderProvider(),
258 new OverrideStringReaderProvider("foo"))
259 .beanFrom(String.class, quote("Some text"));
260 assertEquals("SOME TEXT", result);
261
262 result = jsonWithProviders(new NoOpProvider(), new OverrideStringReaderProvider("foo"))
263 .beanFrom(String.class, quote("Some text"));
264 assertEquals("foo", result);
265
266 // and ok not to have anything, too
267 result = jsonWithProviders(new NoOpProvider(), new NoOpProvider())
268 .beanFrom(String.class, quote("Some text"));
269 assertEquals("Some text", result);
270
271 // Plus nulls fine too
272 result = jsonWithProviders(null, new OverrideStringReaderProvider("foo"))
273 .beanFrom(String.class, quote("Some text"));
274 assertEquals("foo", result);
275 result = jsonWithProviders(new OverrideStringReaderProvider("foo"), null)
276 .beanFrom(String.class, quote("Some text"));
277 assertEquals("foo", result);
278 }
279
280 // But also can use methods from "JSONReader" for convenience
281 public void testCustomDelegatingReader() throws Exception
282 {
283 // First: without handler, will fail to map
284 final String doc = "{\"y\" : 3, \"x\": 2 }";
285 try {
286 JSON.std.beanFrom(Point.class, doc);
287 fail("Should not pass");
288 } catch (JSONObjectException e) {
289 verifyException(e, "$Point");
290 verifyException(e, "constructor to use");
291 }
292
293 // then with custom, should be fine
294 JSON json = jsonWithProvider(new PointReaderProvider());
295 Point v = json.beanFrom(Point.class, doc);
296 assertEquals(2, v._x);
297 assertEquals(3, v._y);
298 }
299 }