Browse Source

suppress exception when evaluate a json path.

the Option.SUPRESS-EXCEPTION is not working in this case   :
var node = objectMapper.readTree("{\"test\" :  null}");

    JsonPath.using(Configuration.builder()
        .options(Option.SUPPRESS_EXCEPTIONS).build())
        .parse(node.toString())
        .read("$.test[?(@ != null)]");
pull/909/head
Vu Manh Dat 2 years ago committed by GitHub
parent
commit
fd48f6b249
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 64
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

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

@ -168,42 +168,48 @@ public class JsonPath {
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;
}
}

Loading…
Cancel
Save