Browse Source

Improved Exception handling.

pull/10/merge
Kalle Stenflo 11 years ago
parent
commit
9ea098d74c
  1. 6
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 27
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
  3. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/PathTokenFilter.java
  4. 3
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  5. 3
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

6
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -255,11 +255,11 @@ public class JsonPath {
result = filter.filter(result, configuration, contextFilters, inArrayContext);
/*
if(result == null && !pathToken.isEndToken()){
throw new PathNotFoundException("Path token: " + pathToken.getFragment() + " not found in json");
throw new PathNotFoundException("Path token: '" + pathToken.getFragment() + "' not found.");
}
*/
if (!inArrayContext) {
inArrayContext = filter.isArrayFilter();

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

@ -15,6 +15,7 @@
package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.regex.Pattern;
@ -101,20 +102,30 @@ public class ArrayIndexFilter extends PathTokenFilter {
} else {
String[] indexArr = COMMA.split(trimmedCondition);
if(obj == null || jsonProvider.length(obj) == 0){
//throw new PathNotFoundException("Failed to access array index: '" + condition + "' since the array is null or empty");
//if(obj == null || jsonProvider.length(obj) == 0){
if(obj == null){
return result;
}
if (indexArr.length == 1) {
return jsonProvider.getProperty(obj, indexArr[0]);
try {
if (indexArr.length == 1) {
/*
if(jsonProvider.length(obj) == 0){
throw new PathNotFoundException("Array index [" + indexArr[0] + "] not found in path");
}
*/
} else {
for (String idx : indexArr) {
jsonProvider.setProperty(result, jsonProvider.length(result), jsonProvider.getProperty(obj, idx.trim()));
return jsonProvider.getProperty(obj, indexArr[0]);
} else {
for (String idx : indexArr) {
jsonProvider.setProperty(result, jsonProvider.length(result), jsonProvider.getProperty(obj, idx.trim()));
}
return result;
}
return result;
} catch (IndexOutOfBoundsException e){
throw new PathNotFoundException("Array index " + indexArr + " not found in path", e);
}
}
}

9
json-path/src/main/java/com/jayway/jsonpath/internal/filter/PathTokenFilter.java

@ -58,6 +58,13 @@ public abstract class PathTokenFilter {
@Override
public String toString() {
return getClass().getSimpleName() + " => " + condition;
if(isArrayFilter()){
return getClass().getSimpleName() + " => " + condition;
} else {
return getClass().getSimpleName() + " => '" + condition + "'";
}
}
}

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

@ -25,8 +25,7 @@ import static org.junit.Assert.assertThat;
*/
public class IssuesTest {
//@Test(expected = PathNotFoundException.class)
@Test()
@Test(expected = PathNotFoundException.class)
public void issue_11() throws Exception {
String json = "{ \"foo\" : [] }";
List<String> result = JsonPath.read(json, "$.foo[?(@.rel= 'item')][0].uri");

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

@ -308,7 +308,7 @@ public class JsonPathTest {
List<String> all = JsonPath.read(DOCUMENT, "$..*");
}
@Test(expected = IndexOutOfBoundsException.class)
@Test(expected = PathNotFoundException.class)
public void access_index_out_of_bounds_does_not_throw_exception() throws Exception {
Object res = JsonPath.read(DOCUMENT, "$.store.book[100].author");
@ -317,4 +317,5 @@ public class JsonPathTest {
}

Loading…
Cancel
Save