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] 2.0.50 JSONPath.eval 代码行为改变。path 是 $,rootObject 不是 json 格式字符串,抛异常。 #2640

Closed
zhangbody opened this issue May 29, 2024 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@zhangbody
Copy link

问题描述

具体看重现代码。

环境信息

  • OS信息:Windows 10
  • JDK信息:Oracle Java 1.8.0_381
  • 版本信息:Fastjson2 2.0.50

重现步骤

import org.noear.snack.ONode;

import com.alibaba.fastjson.JSONPath;
import com.jayway.jsonpath.JsonPath;

public class TestPath {

  public static void main(String[] args) {

    // com.jayway.jsonpath.JsonPath
    System.out.println("" + JsonPath.read("wangfang", "$"));
    // 输出 wangfang

    // org.noear.snack.ONode
    ONode n = ONode.load("wangfang"); //
    System.out.println("" + n.get("$").toObject());
    // 输出 null

    // JSONPath2 2.0.50
    System.out.println(JSONPath.eval("wangfang", "$"));
    // 异常 com.alibaba.fastjson2.JSONException: offset 1, character w, line 1, column 1, fastjson-version 2.0.50 wangfang

    // JSONPath2 2.0.49
    System.out.println(JSONPath.eval("wangfang", "$"));
    // 输出 wangfang
  }

}

期待的正确结果

fastjson2 2.0.49 版本返回字符串 wangfang,2.0.50 版本抛出异常。

jayway JsonPath 返回字符串 wangfang。

snack 返回 null,应该是和第一步解析 json 有关。因为上面的字符串不是 json 格式的 string。

以前返回 wangfang,估计和 $ 这个 path 的含义有关。直接返回当前数据。

相关日志输出

Exception in thread "main" com.alibaba.fastjson2.JSONException: offset 1, character w, line 1, column 1, fastjson-version 2.0.50 wangfang
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:302)
at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:2183)
at com.alibaba.fastjson2.JSONReader.readAny(JSONReader.java:2334)
at com.alibaba.fastjson2.JSONPath$RootPath.extract(JSONPath.java:1126)
at com.alibaba.fastjson2.JSONPath.extract(JSONPath.java:56)
at com.alibaba.fastjson2.JSONPath.eval(JSONPath.java:66)
at com.alibaba.fastjson.JSONPath.eval(JSONPath.java:54)
at TestPath.main(TestPath.java:17)

@zhangbody zhangbody added the bug Something isn't working label May 29, 2024
@wenshao
Copy link
Member

wenshao commented May 29, 2024

这个是真实业务场景还是测试?

@zhangbody
Copy link
Author

这个是真实业务场景还是测试?

业务场景中出现的,以前没注意有这个情况。

我们从 fastjson1 开始用,以前肯定没出现过。

@wenshao wenshao added this to the 2.0.51 milestone May 30, 2024
@wenshao wenshao added the fixed label May 31, 2024
@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

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

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

No branches or pull requests

2 participants