Browse Source

Merge branch 'master' of https://github.com/jayway/JsonPath

pull/29/head
Kalle Stenflo 11 years ago
parent
commit
a5b30d18c4
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  2. 12
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  3. 22
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java

@ -38,7 +38,7 @@ public class FieldFilter extends PathTokenFilter {
public Object filter(Object obj, JsonProvider jsonProvider, LinkedList<Filter> filters, boolean inArrayContext) { public Object filter(Object obj, JsonProvider jsonProvider, LinkedList<Filter> filters, boolean inArrayContext) {
if (jsonProvider.isList(obj)) { if (jsonProvider.isList(obj)) {
if (!inArrayContext) { if (!inArrayContext) {
return null; throw new InvalidPathException("Trying to access field on array");
} else { } else {
List<Object> result = jsonProvider.createList(); List<Object> result = jsonProvider.createList();
for (Object current : jsonProvider.toList(obj)) { for (Object current : jsonProvider.toList(obj)) {

12
json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

@ -15,18 +15,6 @@ import static junit.framework.Assert.assertTrue;
* Time: 8:42 AM * Time: 8:42 AM
*/ */
public class IssuesTest { public class IssuesTest {
@Test
public void issue_7() throws Exception {
String json = "{ \"foo\" : [\n" +
" { \"id\": 1 }, \n" +
" { \"id\": 2 }, \n" +
" { \"id\": 3 }\n" +
" ] }";
assertNull(JsonPath.read(json, "$.foo.id"));
}
@Test @Test
public void issue_11() throws Exception { public void issue_11() throws Exception {

22
json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -9,9 +9,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.*;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
@ -106,6 +104,24 @@ public class JsonModelTest {
JsonModel.model(DOCUMENT).get("store.invalid"); JsonModel.model(DOCUMENT).get("store.invalid");
} }
@Test
public void query_for_null_property_returns_null() {
String documentWithNull =
"{ \"store\": {\n" +
" \"book\": { \n" +
" \"color\": null\n" +
" }\n" +
" }\n" +
"}";
Object color = JsonModel.model(documentWithNull).get("store.book.color");
assertNull(color);
}
@Test(expected = InvalidPathException.class)
public void query_for_property_on_array_throws() throws Exception {
JsonModel.model(DOCUMENT).get("store.book.color");
}
} }

Loading…
Cancel
Save