Skip to content

Commit 348291b

Browse files
committed
Fixed #73
1 parent fcc9ed8 commit 348291b

6 files changed

Lines changed: 320 additions & 12 deletions

File tree

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/EnumReader.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,16 @@ public Object read(JSONReader reader, JsonParser p) throws IOException {
5151
}
5252
return _byIndex[ix];
5353
}
54-
return _enum(p.getValueAsString().trim());
54+
if (p.hasToken(JsonToken.VALUE_NULL)) {
55+
return null;
56+
}
57+
if (p.hasToken(JsonToken.VALUE_STRING)) {
58+
return _enum(p.getValueAsString().trim());
59+
}
60+
throw JSONObjectException.from(p, "Can not read Enum `"+_valueType.getName()+"` from "
61+
+_tokenDesc(p, p.currentToken()));
5562
}
56-
63+
5764
private Object _enum(String id) throws IOException
5865
{
5966
Object e = _byName.get(id);

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/JSONReader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public Object readValue() throws IOException {
183183
* JSON Object, {@link JSONObjectException} will be thrown.
184184
*/
185185
public Map<Object,Object> readMap() throws IOException {
186-
JsonToken t = _parser.getCurrentToken();
186+
JsonToken t = _parser.currentToken();
187187
if (t == JsonToken.VALUE_NULL) {
188188
return null;
189189
}
@@ -200,7 +200,7 @@ public Map<Object,Object> readMap() throws IOException {
200200
* JSON Array, {@link JSONObjectException} will be thrown.
201201
*/
202202
public List<Object> readList() throws IOException {
203-
JsonToken t = _parser.getCurrentToken();
203+
JsonToken t = _parser.currentToken();
204204
if (t == JsonToken.VALUE_NULL) {
205205
return null;
206206
}
@@ -218,7 +218,7 @@ public List<Object> readList() throws IOException {
218218
*/
219219
public Object[] readArray() throws IOException
220220
{
221-
JsonToken t = _parser.getCurrentToken();
221+
JsonToken t = _parser.currentToken();
222222
if (t == JsonToken.VALUE_NULL) {
223223
return null;
224224
}
@@ -249,7 +249,7 @@ public <T> T readBean(Class<T> type) throws IOException {
249249

250250
@SuppressWarnings("unchecked")
251251
public <T> T[] readArrayOf(Class<T> type) throws IOException {
252-
JsonToken t = _parser.getCurrentToken();
252+
JsonToken t = _parser.currentToken();
253253
if (t == JsonToken.VALUE_NULL) {
254254
return null;
255255
}
@@ -271,7 +271,7 @@ public <T> T[] readArrayOf(Class<T> type) throws IOException {
271271
@SuppressWarnings("unchecked")
272272
public <T> List<T> readListOf(Class<T> type) throws IOException
273273
{
274-
JsonToken t = _parser.getCurrentToken();
274+
JsonToken t = _parser.currentToken();
275275
if (t == JsonToken.VALUE_NULL) {
276276
return null;
277277
}
@@ -293,7 +293,7 @@ public <T> List<T> readListOf(Class<T> type) throws IOException
293293
@SuppressWarnings("unchecked")
294294
public <T> Map<String, T> readMapOf(Class<T> type) throws IOException
295295
{
296-
JsonToken t = _parser.getCurrentToken();
296+
JsonToken t = _parser.currentToken();
297297
if (t == JsonToken.VALUE_NULL) {
298298
return null;
299299
}

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/SimpleValueReader.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Object readNext(JSONReader reader, JsonParser p) throws IOException
4343

4444
// Number types:
4545

46-
case SER_NUMBER_SHORT: // fall through
46+
case SER_NUMBER_SHORT:
4747
return Short.valueOf((short) _nextInt(p));
4848

4949
case SER_NUMBER_INTEGER:
@@ -122,14 +122,17 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
122122

123123
case SER_BOOLEAN:
124124
return p.getValueAsBoolean();
125-
126125
case SER_CHAR:
127126
{
128127
String str = p.getValueAsString();
129128
return (str == null || str.isEmpty()) ? ' ' : str.charAt(0);
130129
}
131130

132131
case SER_CALENDAR:
132+
// [jackson-jr#73]: should allow null
133+
if (p.hasToken(JsonToken.VALUE_NULL)) {
134+
return null;
135+
}
133136
{
134137
long l = _fetchLong(p);
135138
Calendar cal = Calendar.getInstance();
@@ -138,10 +141,18 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
138141
}
139142

140143
case SER_DATE:
144+
// [jackson-jr#73]: should allow null
145+
if (p.hasToken(JsonToken.VALUE_NULL)) {
146+
return null;
147+
}
141148
return new Date(_fetchLong(p));
142149

143150
case SER_CLASS:
144151
{
152+
// [jackson-jr#73]: should allow null
153+
if (p.hasToken(JsonToken.VALUE_NULL)) {
154+
return null;
155+
}
145156
String v = p.getValueAsString();
146157
try {
147158
return Class.forName(v);
@@ -150,13 +161,28 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
150161
}
151162
}
152163
case SER_FILE:
164+
// [jackson-jr#73]: should allow null
165+
if (p.hasToken(JsonToken.VALUE_NULL)) {
166+
return null;
167+
}
153168
return new File(p.getValueAsString());
154169
case SER_UUID:
170+
// [jackson-jr#73]: should allow null
171+
if (p.hasToken(JsonToken.VALUE_NULL)) {
172+
return null;
173+
}
155174
return UUID.fromString(p.getValueAsString());
156175
case SER_URL:
176+
// [jackson-jr#73]: should allow null
177+
if (p.hasToken(JsonToken.VALUE_NULL)) {
178+
return null;
179+
}
157180
return new URL(p.getValueAsString());
158181
case SER_URI:
159-
182+
// [jackson-jr#73]: should allow null
183+
if (p.hasToken(JsonToken.VALUE_NULL)) {
184+
return null;
185+
}
160186
return URI.create(p.getValueAsString());
161187

162188
default: // types that shouldn't get here
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package com.fasterxml.jackson.jr.ob.impl;
2+
3+
import static com.fasterxml.jackson.jr.ob.impl.ValueWriterLocator.*;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.net.URI;
8+
import java.net.URL;
9+
import java.util.*;
10+
11+
import com.fasterxml.jackson.core.JsonParser;
12+
import com.fasterxml.jackson.core.JsonToken;
13+
import com.fasterxml.jackson.jr.ob.JSONObjectException;
14+
import com.fasterxml.jackson.jr.ob.api.ValueReader;
15+
16+
/**
17+
* Default {@link ValueReader} used for simple scalar types and related,
18+
* not including POJO-, {@link java.util.Map} and {@link java.util.Collection}
19+
* types.
20+
*/
21+
public class SimpleValueReader extends ValueReader
22+
{
23+
protected final int _typeId;
24+
25+
public SimpleValueReader(Class<?> raw, int typeId) {
26+
super(raw);
27+
_typeId = typeId;
28+
}
29+
30+
@Override
31+
public Object readNext(JSONReader reader, JsonParser p) throws IOException
32+
{
33+
// NOTE: only cases where we can optimize
34+
switch (_typeId) {
35+
// Textual types, related:
36+
case SER_STRING:
37+
case SER_CHARACTER_SEQUENCE:
38+
return _nextString(p);
39+
40+
case SER_CHAR_ARRAY:
41+
String str = _nextString(p);
42+
return (str == null) ? null : str.toCharArray();
43+
44+
// Number types:
45+
46+
case SER_NUMBER_SHORT: // fall through
47+
return Short.valueOf((short) _nextInt(p));
48+
49+
case SER_NUMBER_INTEGER:
50+
return Integer.valueOf(_nextInt(p));
51+
52+
case SER_NUMBER_LONG:
53+
return Long.valueOf(_nextLong(p));
54+
55+
// Other scalar types:
56+
57+
case SER_BOOLEAN:
58+
{
59+
Boolean b = p.nextBooleanValue();
60+
if (b != null) {
61+
return b;
62+
}
63+
return p.getValueAsBoolean();
64+
}
65+
}
66+
67+
p.nextToken();
68+
return read(reader, p);
69+
}
70+
71+
@Override
72+
public Object read(JSONReader reader, JsonParser p) throws IOException
73+
{
74+
switch (_typeId) {
75+
76+
case SER_MAP:
77+
case SER_LIST:
78+
case SER_COLLECTION:
79+
case SER_OBJECT_ARRAY:
80+
// should never get here: we have dedicated readers
81+
break;
82+
83+
case SER_INT_ARRAY:
84+
return _readIntArray(p);
85+
86+
case SER_TREE_NODE:
87+
return reader._treeCodec().readTree(p);
88+
89+
// Textual types, related:
90+
case SER_STRING:
91+
case SER_CHARACTER_SEQUENCE:
92+
return p.getValueAsString();
93+
case SER_CHAR_ARRAY:
94+
return p.getValueAsString().toCharArray();
95+
case SER_BYTE_ARRAY:
96+
return _readBinary(p);
97+
98+
// Number types:
99+
100+
case SER_NUMBER_FLOAT: // fall through
101+
return Float.valueOf((float) p.getValueAsDouble());
102+
case SER_NUMBER_DOUBLE:
103+
return p.getValueAsDouble();
104+
105+
case SER_NUMBER_BYTE: // fall through
106+
return (byte) p.getValueAsInt();
107+
108+
case SER_NUMBER_SHORT: // fall through
109+
return (short) p.getValueAsInt();
110+
case SER_NUMBER_INTEGER:
111+
return p.getValueAsInt();
112+
case SER_NUMBER_LONG:
113+
return p.getValueAsLong();
114+
115+
case SER_NUMBER_BIG_DECIMAL:
116+
return p.getDecimalValue();
117+
118+
case SER_NUMBER_BIG_INTEGER:
119+
return p.getBigIntegerValue();
120+
121+
// Other scalar types:
122+
123+
case SER_BOOLEAN:
124+
return p.getValueAsBoolean();
125+
126+
case SER_CHAR:
127+
{
128+
String str = p.getValueAsString();
129+
return (str == null || str.isEmpty()) ? ' ' : str.charAt(0);
130+
}
131+
132+
case SER_CALENDAR:
133+
{
134+
long l = _fetchLong(p);
135+
Calendar cal = Calendar.getInstance();
136+
cal.setTimeInMillis(l);
137+
return cal;
138+
}
139+
140+
case SER_DATE:
141+
return new Date(_fetchLong(p));
142+
143+
case SER_CLASS:
144+
{
145+
String v = p.getValueAsString();
146+
try {
147+
return Class.forName(v);
148+
} catch (Exception e) {
149+
throw new JSONObjectException("Failed to bind java.lang.Class from value '"+v+"'");
150+
}
151+
}
152+
case SER_FILE:
153+
return new File(p.getValueAsString());
154+
case SER_UUID:
155+
return UUID.fromString(p.getValueAsString());
156+
case SER_URL:
157+
return new URL(p.getValueAsString());
158+
case SER_URI:
159+
160+
return URI.create(p.getValueAsString());
161+
162+
default: // types that shouldn't get here
163+
//case SER_ENUM:
164+
}
165+
166+
throw JSONObjectException.from(p,
167+
"Can not create a "+_valueType.getName()+" instance out of "+_tokenDesc(p));
168+
}
169+
170+
/*
171+
/**********************************************************************
172+
/* Read methods for scalars
173+
/**********************************************************************
174+
*/
175+
176+
protected byte[] _readBinary(JsonParser p) throws IOException {
177+
return p.getBinaryValue();
178+
}
179+
180+
protected int[] _readIntArray(JsonParser p) throws IOException
181+
{
182+
// !!! TODO
183+
throw new JSONObjectException("Reading of int[] not yet implemented");
184+
}
185+
186+
protected long _fetchLong(JsonParser p) throws IOException
187+
{
188+
JsonToken t = p.getCurrentToken();
189+
if (t == JsonToken.VALUE_NUMBER_INT) {
190+
return p.getLongValue();
191+
}
192+
throw JSONObjectException.from(p, "Can not get long numeric value from JSON (to construct "
193+
+_valueType.getName()+") from "+_tokenDesc(p, t));
194+
}
195+
196+
private final String _nextString(JsonParser p) throws IOException {
197+
String str = p.nextTextValue();
198+
return (str == null) ? p.getValueAsString() : str;
199+
}
200+
201+
private final int _nextInt(JsonParser p) throws IOException {
202+
int i = p.nextIntValue(-2);
203+
if (i != -2) {
204+
return i;
205+
}
206+
return p.getValueAsInt();
207+
}
208+
209+
private final long _nextLong(JsonParser p) throws IOException {
210+
long l = p.nextLongValue(-2L);
211+
if (l != -2L) {
212+
return l;
213+
}
214+
return p.getValueAsLong();
215+
}
216+
}

0 commit comments

Comments
 (0)