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 ef28f39e..4d57e325 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 @@ -37,7 +37,7 @@ public class FieldFilter extends PathTokenFilter { public Object filter(Object obj, JsonProvider jsonProvider, LinkedList filters, boolean inArrayContext) { if (jsonProvider.isArray(obj)) { if (!inArrayContext) { - throw new PathNotFoundException("Trying to access the field '" + condition +"' in an array context."); + throw new PathNotFoundException("Path '" + condition + "' is being applied to an array. Arrays can not have attributes."); } else { Object result = jsonProvider.createArray(); for (Object current : jsonProvider.toIterable(obj)) { @@ -73,7 +73,7 @@ public class FieldFilter extends PathTokenFilter { Collection keys = jsonProvider.getPropertyKeys(obj); if(!keys.contains(condition) && split.length == 1){ - throw new PathNotFoundException("Path '" + condition + "' not found in the current context."); + throw new PathNotFoundException("Path '" + condition + "' not found in the current context:\n" + jsonProvider.toJson(obj)); } else { if(split.length == 1){ @@ -87,11 +87,9 @@ public class FieldFilter extends PathTokenFilter { } return res; } - - } } else { - throw new PathNotFoundException(); + throw new PathNotFoundException("Failed to access property: " + condition + " on object " + obj); } } diff --git a/json-path/src/test/java/com/jayway/jsonpath/NullHandlingTest.java b/json-path/src/test/java/com/jayway/jsonpath/NullHandlingTest.java new file mode 100644 index 00000000..8a4d5220 --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/NullHandlingTest.java @@ -0,0 +1,68 @@ +package com.jayway.jsonpath; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +import static junit.framework.Assert.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * User: kalle + * Date: 8/22/13 + * Time: 10:39 AM + */ +public class NullHandlingTest { + + public static final String DOCUMENT = "{\n" + + " \"root-property\": \"root-property-value\",\n" + + " \"root-property-null\": null,\n" + + " \"children\": [\n" + + " {\n" + + " \"id\": 0,\n" + + " \"name\": \"name-0\",\n" + + " \"age\": 0\n" + + " },\n" + + " {\n" + + " \"id\": 1,\n" + + " \"name\": \"name-1\",\n" + + " \"age\": null" + + " },\n" + + " {\n" + + " \"id\": 3,\n" + + " \"name\": \"name-3\"\n" + + " }\n" + + " ]\n" + + "}"; + + + @Test(expected = PathNotFoundException.class) + public void not_defined_property_throws_PathNotFoundException () { + JsonPath.read(DOCUMENT, "$.children[2].age"); + } + + @Test + public void null_property_returns_null () { + Integer age = JsonPath.read(DOCUMENT, "$.children[1].age"); + assertEquals(null, age); + } + + @Test + public void the_age_of_all_with_age_defined() { + List result = JsonPath.read(DOCUMENT, "$.children[*].age"); + + assertThat(result, Matchers.hasItems(0, null)); + } + @Test + public void path2(){ + System.out.println(JsonPath.read("{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}", "a[?(@.b==4)].c")); + } + + public void path(){ + System.out.println(JsonPath.read("{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}", "a[?(@.b==5)].d")); + } + + +}