fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

[BUG] parse `"$ref"` then get() triggers ClassCastException

Open Cooper-Zhong opened this issue 1 month ago • 0 comments

问题描述

https://github.com/alibaba/fastjson2/blob/334d25696654bb4e0593db33c4ec47afaa217cd9/core/src/test/java/com/alibaba/fastjson2/v1issues/issue_3900/Issue3922.java#L13 fastjson兼容解析"$ref"的json对象后使用get出现ClassCastException。

环境信息

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

重现步骤


import com.alibaba.fastjson.JSONObject;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

public class Issue3922 {
    @Test
    public void test_for_issue3922() throws JSONException {
        String jsonString = "{\"this0\":{\"$ref\":\"1\"}}";
        JSONObject jsonObject = JSONObject.parseObject(jsonString, JSONObject.class);
        JSONObject innerObject = (JSONObject) jsonObject.get("this0");
        System.out.println(innerObject.toString());
        JSONAssert.assertEquals("{\"$ref\":\"1\"}", innerObject.toString(), true);
    }
}

相关日志输出

java.lang.ClassCastException: class com.alibaba.fastjson2.JSONObject cannot be cast to class com.alibaba.fastjson.JSONObject (com.alibaba.fastjson2.JSONObject and com.alibaba.fastjson.JSONObject are in unnamed module of loader 'app')

Cooper-Zhong avatar Jun 30 '24 12:06 Cooper-Zhong