From c05c4364bc1675af98b3ec7456c935576a8ba97d Mon Sep 17 00:00:00 2001 From: Jack Singleton Date: Wed, 10 Jul 2013 12:17:23 -0700 Subject: [PATCH 1/3] Added tests for query for null field and query for property on array --- .../com/jayway/jsonpath/JsonModelTest.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java index f811880a..b9d83dee 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java @@ -9,9 +9,7 @@ import java.util.HashMap; import java.util.Map; import static java.util.Arrays.asList; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.*; /** * Created by IntelliJ IDEA. @@ -106,6 +104,24 @@ public class JsonModelTest { 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"); + } } From dfe248c002e3817a3e9df53ed05f7f46e0e3d49b Mon Sep 17 00:00:00 2001 From: Jack Singleton Date: Wed, 10 Jul 2013 12:19:00 -0700 Subject: [PATCH 2/3] Changed behavior of FieldFilter to throw InvalidPathException instead of returning null when a field is queried for on a json array --- .../java/com/jayway/jsonpath/internal/filter/FieldFilter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java index 13a5998f..3fc1b520 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java @@ -35,7 +35,7 @@ public class FieldFilter extends PathTokenFilter { public Object filter(Object obj, JsonProvider jsonProvider, LinkedList filters, boolean inArrayContext) { if (jsonProvider.isList(obj)) { if (!inArrayContext) { - return null; + throw new InvalidPathException("Trying to access field on array"); } else { List result = jsonProvider.createList(); for (Object current : jsonProvider.toList(obj)) { From 7e4e04d739dbbbc5fbe5fb32f94e7c7681b5fc1e Mon Sep 17 00:00:00 2001 From: Jack Singleton Date: Wed, 10 Jul 2013 12:19:37 -0700 Subject: [PATCH 3/3] Removed test for issue 7 which is in conflict with new behavior --- .../test/java/com/jayway/jsonpath/IssuesTest.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java b/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java index 20e4fd5b..dd44daa5 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java @@ -15,18 +15,6 @@ import static junit.framework.Assert.assertTrue; * Time: 8:42 AM */ 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 public void issue_11() throws Exception {