Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/com/jsoniter/any/Any.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ public int size() {
return 0;
}

public Any mustBeValid() {
if(this instanceof NotFoundAny) {
throw ((NotFoundAny) this).exception;
} else {
return this;
}
}

public Set<String> keys() {
return EMPTY_KEYS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jsoniter/any/NotFoundAny.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class NotFoundAny extends Any {

private final JsonException exception;
protected final JsonException exception;

public NotFoundAny(Object[] keys, int idx, Object obj) {
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",
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/jsoniter/output/TestAny.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import com.jsoniter.ValueType;
import com.jsoniter.any.*;
import com.jsoniter.spi.JsonException;
import junit.framework.TestCase;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import java.util.Arrays;
import java.util.HashMap;

public class TestAny extends TestCase {

@Rule
public final ExpectedException exception = ExpectedException.none();

static {
// JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
}
Expand Down Expand Up @@ -120,6 +126,12 @@ public void test_array() {
assertEquals("[1,2,3]", any.toString());
}

public void test_not_found() {
Any any = Any.wrap(new int[]{1, 2, 3});
exception.expect(JsonException.class);
any.get("not", "found", "path");
}

public void skip_map() {
HashMap<String, Object> val = new HashMap<String, Object>();
val.put("hello", 1);
Expand Down