Skip to content

[BUG] 2.0.50 JSONPath.eval 代码行为改变。path 是 $,rootObject 不是 json 格式字符串,抛异常。 #2640

@zhangbody

Description

@zhangbody

问题描述

具体看重现代码。

环境信息

  • 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions