Option.SUPRESS_EXCEPTION 不工作 - JsonPath 2.7.0(或更高版本)

问题描述 投票:0回答:1

我试图在评估 JSON 路径时抑制所有异常。但是当我将 JSON 路径库升级到 2.7 时,它似乎没有按预期工作。例如:

var node = objectMapper.readTree("{\"test\" :  null}");

    JsonPath.using(Configuration.builder()
        .options(Option.SUPPRESS_EXCEPTIONS).build())
        .parse(node.toString())
        .read("$.test[?(@ != null)]");

我们抛出了一个异常。

Filter: [?] can not be applied to primitives. Current context is: null
com.jayway.jsonpath.InvalidPathException: Filter: [?] can not be applied to primitives. Current context is: null

相关问题:https://github.com/json-path/JsonPath/issues/908

java jsonpath json-path-expression
1个回答
0
投票

新的更新似乎删除了 try/catch 语句来检查 SUPPRESS_EXCEPTION 选项。 所以你可以使用 try/catch 自己检查它。像这个:

try {
  JsonPath.using(Configuration.builder()
          .options(Option.SUPPRESS_EXCEPTIONS).build())
      .parse(node.toString())
      .read("$.test[?(@ != null)]");
} catch (JsonPathException ex) {
  if (is suppress exception) {
    return 'default value';
  } 
  throw  ex;
}
© www.soinside.com 2019 - 2024. All rights reserved.