Skip to content

[BUG] JSONPath.eval with String and Object #2584

@Cooperzzy

Description

@Cooperzzy

问题描述

JSONPath.eval 对 string和object结果不一致,感觉是个问题

环境信息

  • OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [Openjdk 17.0.6]
  • 版本信息:[Fastjson2 2.0.50]

重现步骤

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONPath;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue1965_757 {
    @Test
    public void testMutant1() {
        JSONObject obj = JSONObject.of("data", JSONArray.of(1));
        String str = JSON.toJSONString(obj);
        System.out.println(str);
        System.out.println(JSONPath.eval(obj, "$.data[0][0]"));
        System.out.println(JSONPath.eval(str, "$.data[0][0]"));
        assertEquals(JSONPath.eval(str, "$.data[0][0]"), JSONPath.eval(obj, "$.data[0][0]"));
    }

    @Test
    public void testMutant2() {
        JSONObject obj = JSONObject.of("data", JSONArray.of(1, 2));
        String str = JSON.toJSONString(obj);
        System.out.println(str);

        System.out.println(JSONPath.eval(obj, "$.data[0][0]"));
        System.out.println(JSONPath.eval(str, "$.data[0][0]"));
        assertEquals(JSONPath.eval(str, "$.data[0][0]"), JSONPath.eval(obj, "$.data[0][0]"));
    }

    @Test
    public void testMutant3() {
        JSONObject obj = JSONObject.of("data", JSONArray.of(JSONObject.of("id", "1")));
        String str = JSON.toJSONString(obj);
        System.out.println(str);
        System.out.println(JSONPath.eval(obj, "$.data[0][0]"));
        System.out.println(JSONPath.eval(str, "$.data[0][0]"));
        assertEquals(JSONPath.eval(str, "$.data[0][0]"), JSONPath.eval(obj, "$.data[0][0]"));
    }


}

相关日志输出

// 1
{"data":[1]}
1
null

org.opentest4j.AssertionFailedError:
Expected :null
Actual :1

// 2
{"data":[1,2]}
1
null

org.opentest4j.AssertionFailedError:
Expected :null
Actual :1

// 3
{"data":[{"id":"1"}]}
1
1

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