Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]JSONPath解析出现NPE #524

Closed
godzhou opened this issue Jul 1, 2022 · 2 comments
Closed

[BUG]JSONPath解析出现NPE #524

godzhou opened this issue Jul 1, 2022 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@godzhou
Copy link

godzhou commented Jul 1, 2022

问题描述

使用JSONPath解析表达式时抛出NullPointerException

环境信息

  • JDK信息: [Openjdk 1.8.0_301]
  • 版本信息:[Fastjson2 2.0.8]

重现步骤

  1. 出现异常的数据结构,包含嵌套数组
{
  "data": [
    [
      {
        "value": 1958,
        "hb": -0.03
      },
      {
        "value": 0.28,
        "hb": -0.04
      }
    ]
  ]
}
  1. path表达式
    $.data[*][?(@.value == 0.28)]

  2. 按照api使用抛出npe

        String jsonStr = "{\"data\":[[{\"value\":1958,\"hb\":-0.03},{\"value\":0.28,\"hb\":-0.04}]]}";

        JSONPath path = JSONPath.of("$.data[*][?(@.value == 0.28)]");
        JSONReader parser = JSONReader.of(jsonStr);
        Object fastjsonRes = path.extract(parser);

期待的正确结果

对比jayway的jsonpath实现,解析结果为
[{"value":0.28,"hb":-0.04}]
相关代码

        String jsonStr = "{\"data\":[[{\"value\":1958,\"hb\":-0.03},{\"value\":0.28,\"hb\":-0.04}]]}";
        Object jaywayRes = JsonPath.parse(jsonStr).read("$.data[*][?(@.value == 0.28)]");

相关日志输出

Exception in thread "main" java.lang.NullPointerException
at com.alibaba.fastjson2.JSONPath$NameFilter.apply(JSONPath.java:1129)
at com.alibaba.fastjson2.JSONPath$NameFilter.eval(JSONPath.java:1101)
at com.alibaba.fastjson2.JSONPath$MultiSegmentPath.extract(JSONPath.java:2708)

附加信息

fastjson的jsonpath解析这种结构也存在问题,虽然表现并不一致

@godzhou godzhou added the bug Something isn't working label Jul 1, 2022
@wenshao wenshao added this to the 2.0.9 milestone Jul 1, 2022
@wenshao
Copy link
Member

wenshao commented Jul 1, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.9-SNAPSHOT/
问题已修复,请用2.0.9-SNAPSHOT版本验证,2.0.9版本预计在7月10日发布

@wenshao
Copy link
Member

wenshao commented Jul 10, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.9
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants