Browse Source

Improved exception handling.

pull/31/merge
Kalle Stenflo 11 years ago
parent
commit
f3e672c2a2
  1. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
  2. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  3. 88
      json-path/src/test/java/com/jayway/jsonpath/DocumentationPageTests.java
  4. 26
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

5
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java

@ -14,6 +14,7 @@
*/ */
package com.jayway.jsonpath.internal.filter; package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -95,8 +96,8 @@ public class ArrayIndexFilter extends PathTokenFilter {
} else { } else {
String[] indexArr = COMMA.split(trimmedCondition); String[] indexArr = COMMA.split(trimmedCondition);
if(jsonProvider.length(obj) == 0){ if(obj == null || jsonProvider.length(obj) == 0){
return result; throw new PathNotFoundException("Failed to access array index: '" + condition + "' since the array is null or empty");
} }
if (indexArr.length == 1) { if (indexArr.length == 1) {

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

@ -89,7 +89,7 @@ public class FieldFilter extends PathTokenFilter {
} }
} }
} else { } else {
throw new PathNotFoundException("Failed to access property: " + condition + " on object " + obj); throw new PathNotFoundException("Failed to access property: '" + condition + "' on object " + obj);
} }
} }

88
json-path/src/test/java/com/jayway/jsonpath/DocumentationPageTests.java

@ -0,0 +1,88 @@
package com.jayway.jsonpath;
import org.junit.Test;
/**
* User: kalle
* Date: 8/23/13
* Time: 10:58 AM
*/
public class DocumentationPageTests {
public static final String JSON = "{ \n" +
" \"store\": {\n" +
" \"book\": [ \n" +
" { \n" +
" \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" },\n" +
" { \n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" },\n" +
" { \n" +
" \"category\": null,\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99,\n" +
" \"edition\": {\n" +
" \"release-date\": \"2013-01-22\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n" +
" }\n" +
" }\n" +
"}";
@Test
public void test_1() {
System.out.println(JsonPath.read(JSON, "$.store.bicycle"));
}
@Test
public void test_2() {
System.out.println(JsonPath.read(JSON, "$.store.book[0]"));
}
@Test
public void test_3() {
System.out.println(JsonPath.read(JSON, "$.store.book[*].author"));
}
@Test
public void test_4() {
System.out.println(JsonPath.read(JSON, "$.store.book[-2:].author"));
}
@Test
public void test_5() {
System.out.println(JsonPath.read(JSON, "$.store.book[1:3].author"));
}
@Test
public void test_6() {
System.out.println(JsonPath.read(JSON, "$.store.book[0]['author', 'category']"));
}
@Test
public void test_7() {
System.out.println(JsonPath.read(JSON, "$..price"));
}
}

26
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -78,7 +78,33 @@ public class JsonPathTest {
private final static String ARRAY_EXPAND = "[{\"parent\": \"ONE\", \"child\": {\"name\": \"NAME_ONE\"}}, [{\"parent\": \"TWO\", \"child\": {\"name\": \"NAME_TWO\"}}]]"; private final static String ARRAY_EXPAND = "[{\"parent\": \"ONE\", \"child\": {\"name\": \"NAME_ONE\"}}, [{\"parent\": \"TWO\", \"child\": {\"name\": \"NAME_TWO\"}}]]";
@Test
public void null_object_in_path() {
String json = "{\n" +
" \"success\": true,\n" +
" \"data\": {\n" +
" \"user\": 3,\n" +
" \"own\": null,\n" +
" \"passes\": null,\n" +
" \"completed\": null\n" +
" },\n" +
" \"data2\": {\n" +
" \"user\": 3,\n" +
" \"own\": null,\n" +
" \"passes\": [{\"id\":\"1\"}],\n" +
" \"completed\": null\n" +
" },\n" +
" \"version\": 1371160528774\n" +
"}";
System.out.println(JsonPath.read(json, "$.data.passes[0].id"));
System.out.println(JsonPath.isPathDefinite("$.data.passes[0].id"));
System.out.println(JsonPath.read(json, "$.data2.passes[0].id"));
System.out.println(JsonPath.isPathDefinite("$.data2.passes[0].id"));
}
@Test @Test
public void array_start_expands() throws Exception { public void array_start_expands() throws Exception {

Loading…
Cancel
Save