Skip to content

Commit 153a169

Browse files
committed
Implemented reader (deserialization) side of #65
1 parent be7385e commit 153a169

2 files changed

Lines changed: 99 additions & 1 deletion

File tree

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/api/ValueReader.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,20 @@ protected ValueReader(Class<?> valueType) {
4444
* Method called to deserialize value of type supported by this reader, using
4545
* given parser. Parser is not yet positioned to the (first) token
4646
* of the value to read and needs to be advanced.
47+
*<p>
48+
* Default implementation simply calls `p.nextToken()` first, then calls
49+
* {#link {@link #read(JSONReader, JsonParser)}, but some implementations
50+
* may decide to implement this differently to use (slightly) more efficient
51+
* accessor in {@link JsonParser}, like {@link JsonParser#nextIntValue(int)}.
4752
*
4853
* @param reader Context object that allows calling other read methods for contained
4954
* values of different types (for example for collection readers).
5055
* @param p Underlying parser used for reading decoded token stream
5156
*/
52-
public abstract Object readNext(JSONReader reader, JsonParser p) throws IOException;
57+
public Object readNext(JSONReader reader, JsonParser p) throws IOException {
58+
p.nextToken();
59+
return read(reader, p);
60+
}
5361

5462
/*
5563
/**********************************************************************
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.fasterxml.jackson.jr.ob.impl;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.core.JsonParser;
6+
import com.fasterxml.jackson.jr.ob.JSON;
7+
import com.fasterxml.jackson.jr.ob.JSONObjectException;
8+
import com.fasterxml.jackson.jr.ob.TestBase;
9+
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
10+
import com.fasterxml.jackson.jr.ob.api.ValueReader;
11+
12+
public class CustomValueHandlersTest extends TestBase
13+
{
14+
static class CustomValue {
15+
public int value;
16+
17+
// 2nd arg just to avoid discovery
18+
public CustomValue(int v, boolean b) {
19+
// and to ensure it goes through constructor, add 1
20+
value = v + 1;
21+
}
22+
}
23+
24+
static class CustomValueBean {
25+
public CustomValue custom;
26+
27+
protected CustomValueBean() { }
28+
public CustomValueBean(int v) {
29+
custom = new CustomValue(v, false);
30+
}
31+
}
32+
33+
static class CustomValueReader extends ValueReader {
34+
public CustomValueReader() {
35+
super(CustomValue.class);
36+
}
37+
38+
@Override
39+
public Object read(JSONReader reader, JsonParser p) throws IOException {
40+
return new CustomValue(p.getIntValue(), true);
41+
}
42+
43+
// Base class impl should be fine, although we'd use this for optimal
44+
/*
45+
@Override
46+
public Object readNext(JSONReader reader, JsonParser p) throws IOException {
47+
return new CustomValue(p.nextIntValue(-1), true);
48+
}
49+
*/
50+
}
51+
52+
static class CustomReaders extends ReaderWriterProvider {
53+
@Override
54+
public ValueReader findBeanReader(JSONReader readContext, Class<?> type) {
55+
if (type.equals(CustomValue.class)) {
56+
return new CustomValueReader();
57+
}
58+
return null;
59+
}
60+
}
61+
62+
/*
63+
/**********************************************************************
64+
/* Test methdods
65+
/**********************************************************************
66+
*/
67+
68+
public void testSimpleCustomReader() throws Exception
69+
{
70+
// First: without handler, will fail to map
71+
try {
72+
JSON.std.beanFrom(CustomValue.class, "123");
73+
fail("Should not pass");
74+
} catch (JSONObjectException e) {
75+
verifyException(e, ".CustomValue");
76+
verifyException(e, "constructor to use");
77+
}
78+
79+
// then with custom, should be fine
80+
JSON json = JSON.std
81+
.with(new CustomReaders());
82+
CustomValue v = json.beanFrom(CustomValue.class, "123");
83+
assertEquals(124, v.value);
84+
85+
// similarly with wrapper
86+
CustomValueBean bean = json.beanFrom(CustomValueBean.class,
87+
aposToQuotes("{ 'custom' : 137 }"));
88+
assertEquals(138, bean.custom.value);
89+
}
90+
}

0 commit comments

Comments
 (0)