Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,42 +168,48 @@ public <T> T read(Object jsonObject, Configuration configuration) {
boolean optAsPathList = configuration.containsOption(AS_PATH_LIST);
boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST);
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);

if (path.isFunctionPath()) {
if (optAsPathList || optAlwaysReturnList) {
if (optSuppressExceptions) {
try {
if (path.isFunctionPath()) {
if (optAsPathList || optAlwaysReturnList) {
if (optSuppressExceptions) {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
throw new JsonPathException("Options " + AS_PATH_LIST + " and " + ALWAYS_RETURN_LIST + " are not allowed when using path functions!");
}
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
throw new JsonPathException("Options " + AS_PATH_LIST + " and " + ALWAYS_RETURN_LIST + " are not allowed when using path functions!");
}
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
return evaluationContext.getValue(true);
} else if (optAsPathList) {
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) {
return (T) configuration.jsonProvider().createArray();
}
return (T) evaluationContext.getPath();
} else {
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) {
if (optAlwaysReturnList) {
return evaluationContext.getValue(true);
} else if (optAsPathList) {
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) {
return (T) configuration.jsonProvider().createArray();
}
return (T) evaluationContext.getPath();
} else {
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) {
if (optAlwaysReturnList) {
return (T) configuration.jsonProvider().createArray();
} else {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
}
Object res = evaluationContext.getValue(false);
if (optAlwaysReturnList && path.isDefinite()) {
Object array = configuration.jsonProvider().createArray();
configuration.jsonProvider().setArrayIndex(array, 0, res);
return (T) array;
} else {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
return (T) res;
}
}
Object res = evaluationContext.getValue(false);
if (optAlwaysReturnList && path.isDefinite()) {
Object array = configuration.jsonProvider().createArray();
configuration.jsonProvider().setArrayIndex(array, 0, res);
return (T) array;
} else {
return (T) res;
} catch (JsonPathException ex) {
if (optSuppressExceptions) {
return (T) configuration.jsonProvider().createArray();
}
throw ex;
}
}

Expand Down
25 changes: 25 additions & 0 deletions json-path/src/test/java/com/jayway/jsonpath/Issue_908.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jayway.jsonpath;

import org.junit.Test;

public class Issue_908 {

@Test(expected = InvalidPathException.class)
public void test_when_current_context_is_null() {
JsonPath
.parse("{\"test\" : null }")
.read("$.test[?(@ != null)]");
}

@Test
public void test_suppress_exception_when_current_context_is_null() {

Object rs = JsonPath.using(Configuration.builder()
.options(Option.SUPPRESS_EXCEPTIONS).build())
.parse("{\"test\" : null }")
.read("$.test[?(@ != null)]");

assert(rs.toString().equals("[]"));
}

}