Skip to content

Commit 0f565a1

Browse files
committed
get returns optional any
1 parent 88e45d0 commit 0f565a1

9 files changed

Lines changed: 173 additions & 147 deletions

File tree

src/main/java/com/jsoniter/any/Any.java

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,23 @@ public Any next() {
8484
public abstract ValueType valueType();
8585

8686
public <T> T bindTo(T obj, Object... keys) {
87-
Any found = get(keys);
88-
if (found == null) {
89-
return null;
90-
}
91-
return found.bindTo(obj);
87+
return get(keys).bindTo(obj);
9288
}
9389

9490
public <T> T bindTo(T obj) {
9591
return (T) object();
9692
}
9793

9894
public <T> T bindTo(TypeLiteral<T> typeLiteral, T obj, Object... keys) {
99-
Any found = get(keys);
100-
if (found == null) {
101-
return null;
102-
}
103-
return found.bindTo(typeLiteral, obj);
95+
return get(keys).bindTo(typeLiteral, obj);
10496
}
10597

10698
public <T> T bindTo(TypeLiteral<T> typeLiteral, T obj) {
10799
return (T) object();
108100
}
109101

110102
public Object object(Object... keys) {
111-
Any found = get(keys);
112-
if (found == null) {
113-
return null;
114-
}
115-
return found.object();
103+
return get(keys).object();
116104
}
117105

118106
public abstract Object object();
@@ -126,23 +114,15 @@ public List<Any> asList() {
126114
}
127115

128116
public <T> T as(Class<T> clazz, Object... keys) {
129-
Any found = get(keys);
130-
if (found == null) {
131-
return null;
132-
}
133-
return found.as(clazz);
117+
return get(keys).as(clazz);
134118
}
135119

136120
public <T> T as(Class<T> clazz) {
137121
return (T) object();
138122
}
139123

140124
public <T> T as(TypeLiteral<T> typeLiteral, Object... keys) {
141-
Any found = get(keys);
142-
if (found == null) {
143-
return null;
144-
}
145-
return found.as(typeLiteral);
125+
return get(keys).as(typeLiteral);
146126
}
147127

148128
public <T> T as(TypeLiteral<T> typeLiteral) {
@@ -151,7 +131,7 @@ public <T> T as(TypeLiteral<T> typeLiteral) {
151131

152132
public final boolean toBoolean(Object... keys) {
153133
Any found = get(keys);
154-
if (found == null) {
134+
if (found.valueType() == ValueType.INVALID) {
155135
return false;
156136
}
157137
return found.toBoolean();
@@ -163,7 +143,7 @@ public boolean toBoolean() {
163143

164144
public final int toInt(Object... keys) {
165145
Any found = get(keys);
166-
if (found == null) {
146+
if (found.valueType() == ValueType.INVALID) {
167147
return 0;
168148
}
169149
return found.toInt();
@@ -175,7 +155,7 @@ public int toInt() {
175155

176156
public final long toLong(Object... keys) {
177157
Any found = get(keys);
178-
if (found == null) {
158+
if (found.valueType() == ValueType.INVALID) {
179159
return 0;
180160
}
181161
return found.toLong();
@@ -187,7 +167,7 @@ public long toLong() {
187167

188168
public final float toFloat(Object... keys) {
189169
Any found = get(keys);
190-
if (found == null) {
170+
if (found.valueType() == ValueType.INVALID) {
191171
return 0;
192172
}
193173
return found.toFloat();
@@ -199,7 +179,7 @@ public float toFloat() {
199179

200180
public final double toDouble(Object... keys) {
201181
Any found = get(keys);
202-
if (found == null) {
182+
if (found.valueType() == ValueType.INVALID) {
203183
return 0;
204184
}
205185
return found.toDouble();
@@ -211,8 +191,8 @@ public double toDouble() {
211191

212192
public final String toString(Object... keys) {
213193
Any found = get(keys);
214-
if (found == null) {
215-
return null;
194+
if (found.valueType() == ValueType.INVALID) {
195+
return "";
216196
}
217197
return found.toString();
218198
}
@@ -234,39 +214,22 @@ public Iterator<Any> iterator() {
234214
public EntryIterator entries() { return EMPTY_ENTRIES_ITERATOR; }
235215

236216
public Any get(int index) {
237-
return null;
217+
return new NotFoundAny(index, object());
238218
}
239219

240220
public Any get(Object key) {
241-
return null;
221+
return new NotFoundAny(key, object());
242222
}
243223

244224
public final Any get(Object... keys) {
245-
try {
246-
return get(keys, 0);
247-
} catch (IndexOutOfBoundsException e) {
248-
return null;
249-
} catch (ClassCastException e) {
250-
return null;
251-
}
225+
return get(keys, 0);
252226
}
253227

254228
public Any get(Object[] keys, int idx) {
255229
if (idx == keys.length) {
256230
return this;
257231
}
258-
return null;
259-
}
260-
261-
public final Any require(Object... keys) {
262-
return require(keys, 0);
263-
}
264-
265-
public Any require(Object[] keys, int idx) {
266-
if (idx == keys.length) {
267-
return this;
268-
}
269-
throw reportPathNotFound(keys, idx);
232+
return new NotFoundAny(keys, idx, object());
270233
}
271234

272235
public Any set(int newVal) {

src/main/java/com/jsoniter/any/ArrayAny.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void writeTo(JsonStream stream) throws IOException {
3535
return;
3636
}
3737
iter.next().writeTo(stream);
38-
while(iter.hasNext()) {
38+
while (iter.hasNext()) {
3939
stream.writeMore();
4040
iter.next().writeTo(stream);
4141
}
@@ -54,7 +54,11 @@ public Iterator<Any> iterator() {
5454

5555
@Override
5656
public Any get(int index) {
57-
return val.get(index);
57+
try {
58+
return val.get(index);
59+
} catch (IndexOutOfBoundsException e) {
60+
return new NotFoundAny(index, object());
61+
}
5862
}
5963

6064
@Override
@@ -66,25 +70,17 @@ public Any get(Object[] keys, int idx) {
6670
if (isWildcard(key)) {
6771
ArrayList<Any> result = new ArrayList<Any>();
6872
for (Any element : val) {
69-
result.add(element.get(keys, idx+1));
73+
result.add(element.get(keys, idx + 1));
7074
}
7175
return Any.wrapAnyList(result);
7276
}
73-
return val.get((Integer) key).get(keys, idx+1);
74-
}
75-
76-
@Override
77-
public Any require(Object[] keys, int idx) {
78-
if (idx == keys.length) {
79-
return this;
80-
}
81-
Any result = null;
8277
try {
83-
result = val.get((Integer) keys[idx]);
78+
return val.get((Integer) key).get(keys, idx + 1);
8479
} catch (IndexOutOfBoundsException e) {
85-
reportPathNotFound(keys, idx);
80+
return new NotFoundAny(keys, idx, object());
81+
} catch (ClassCastException e) {
82+
return new NotFoundAny(keys, idx, object());
8683
}
87-
return result.require(keys, idx + 1);
8884
}
8985

9086
@Override

src/main/java/com/jsoniter/any/ArrayLazyAny.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ public Any get(int index) {
5858
try {
5959
return fillCache(index);
6060
} catch (IndexOutOfBoundsException e) {
61-
return null;
62-
} catch (ClassCastException e) {
63-
return null;
61+
return new NotFoundAny(index, object());
6462
}
6563
}
6664

@@ -78,21 +76,13 @@ public Any get(Object[] keys, int idx) {
7876
}
7977
return Any.wrapAnyList(result);
8078
}
81-
return fillCache((Integer) key).get(keys, idx + 1);
82-
}
83-
84-
@Override
85-
public Any require(Object[] keys, int idx) {
86-
if (idx == keys.length) {
87-
return this;
88-
}
89-
Any result = null;
9079
try {
91-
result = fillCache((Integer) keys[idx]);
80+
return fillCache((Integer) key).get(keys, idx + 1);
9281
} catch (IndexOutOfBoundsException e) {
93-
reportPathNotFound(keys, idx);
82+
return new NotFoundAny(keys, idx, object());
83+
} catch (ClassCastException e) {
84+
return new NotFoundAny(keys, idx, object());
9485
}
95-
return result.require(keys, idx + 1);
9686
}
9787

9888
private void fillCache() {
@@ -104,8 +94,8 @@ private void fillCache() {
10494
}
10595
try {
10696
JsonIterator iter = JsonIterator.tlsIter.get();
97+
iter.reset(data, lastParsedPos, tail);
10798
if (lastParsedPos == head) {
108-
iter.reset(data, lastParsedPos, tail);
10999
if (!CodegenAccess.readArrayStart(iter)) {
110100
lastParsedPos = tail;
111101
return;
@@ -134,11 +124,11 @@ private Any fillCache(int target) {
134124
}
135125
try {
136126
JsonIterator iter = JsonIterator.tlsIter.get();
127+
iter.reset(data, lastParsedPos, tail);
137128
if (lastParsedPos == head) {
138-
iter.reset(data, lastParsedPos, tail);
139129
if (!CodegenAccess.readArrayStart(iter)) {
140130
lastParsedPos = tail;
141-
return null;
131+
throw new IndexOutOfBoundsException();
142132
}
143133
Any element = iter.readAny();
144134
cache.add(element);
@@ -160,7 +150,7 @@ private Any fillCache(int target) {
160150
} catch (IOException e) {
161151
throw new JsonException(e);
162152
}
163-
return null;
153+
throw new IndexOutOfBoundsException();
164154
}
165155

166156
private class LazyIterator implements Iterator<Any> {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.jsoniter.any;
2+
3+
import com.jsoniter.ValueType;
4+
import com.jsoniter.output.JsonStream;
5+
import com.jsoniter.spi.JsonException;
6+
7+
import java.io.IOException;
8+
import java.util.Arrays;
9+
10+
class NotFoundAny extends Any {
11+
12+
private final JsonException exception;
13+
14+
public NotFoundAny(Object[] keys, int idx, Object obj) {
15+
this.exception = new JsonException(String.format("Value not found: failed to get path %s, because #%s %s not found in %s",
16+
Arrays.toString(keys), idx, keys[idx], obj));
17+
}
18+
19+
public NotFoundAny(int index, Object obj) {
20+
this.exception = new JsonException(String.format("Value not found: failed to get index %s, because %s not found in %s",
21+
index, index, obj));
22+
}
23+
24+
public NotFoundAny(Object key, Object obj) {
25+
this.exception = new JsonException(String.format("Value not found: failed to get key %s, because %s not found in %s",
26+
key, key, obj));
27+
}
28+
29+
@Override
30+
public ValueType valueType() {
31+
return ValueType.INVALID;
32+
}
33+
34+
@Override
35+
public Object object() {
36+
throw exception;
37+
}
38+
39+
@Override
40+
public void writeTo(JsonStream stream) throws IOException {
41+
throw exception;
42+
}
43+
44+
@Override
45+
public Any get(int index) {
46+
return this;
47+
}
48+
49+
@Override
50+
public Any get(Object key) {
51+
return this;
52+
}
53+
54+
@Override
55+
public Any get(Object[] keys, int idx) {
56+
return this;
57+
}
58+
}

0 commit comments

Comments
 (0)