Skip to content

Commit 04b2025

Browse files
committed
json-iterator#99 added mustBeValid method to Any class
1 parent 411647c commit 04b2025

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ public int size() {
181181
return 0;
182182
}
183183

184+
public Any mustBeValid() {
185+
if(this instanceof NotFoundAny) {
186+
throw ((NotFoundAny) this).exception;
187+
} else {
188+
return this;
189+
}
190+
}
191+
184192
public Set<String> keys() {
185193
return EMPTY_KEYS;
186194
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class NotFoundAny extends Any {
1111

12-
private final JsonException exception;
12+
protected final JsonException exception;
1313

1414
public NotFoundAny(Object[] keys, int idx, Object obj) {
1515
this.exception = new JsonException(String.format("Value not found: failed to get path %s, because #%s section of the path ( %s ) not found in %s",

src/test/java/com/jsoniter/output/TestAny.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
import com.jsoniter.ValueType;
44
import com.jsoniter.any.*;
5+
import com.jsoniter.spi.JsonException;
56
import junit.framework.TestCase;
7+
import org.junit.Rule;
8+
import org.junit.rules.ExpectedException;
69

710
import java.util.Arrays;
811
import java.util.HashMap;
912

1013
public class TestAny extends TestCase {
1114

15+
@Rule
16+
public final ExpectedException exception = ExpectedException.none();
17+
1218
static {
1319
// JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
1420
}
@@ -120,6 +126,12 @@ public void test_array() {
120126
assertEquals("[1,2,3]", any.toString());
121127
}
122128

129+
public void test_not_found() {
130+
Any any = Any.wrap(new int[]{1, 2, 3});
131+
exception.expect(JsonException.class);
132+
any.get("not", "found", "path");
133+
}
134+
123135
public void skip_map() {
124136
HashMap<String, Object> val = new HashMap<String, Object>();
125137
val.put("hello", 1);

0 commit comments

Comments
 (0)