alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.7k stars 479 forks source link

class com.alibaba.fastjson.JSONObject cannot be cast to class com.alibaba.fastjson2.JSONObject[BUG] #2665

Closed mineYso closed 2 months ago

mineYso commented 4 months ago

问题描述

使用FastJson1.x的api出现2.x的转型失败异常。

环境信息

重现步骤

如何操作可以重现该问题:

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public static void testList() {
        try
        {
            String json = "[\r\n"
                + "    {\r\n"
                + "        \"date\": \"2024-06-10\",\r\n"
                + "        \"slots\": [\r\n"
                + "            {\r\n"
                + "                \"createDate\": \"2024-06-10T11:54:58.240+01:00\",\r\n"
                + "                \"expireDate\": \"2024-06-10T11:59:58.240+01:00\",\r\n"
                + "                \"id\": 1176592662,\r\n"
                + "                \"intervention\": {\r\n"
                + "                    \"code\": \"FS\",\r\n"
                + "                    \"name\": \"FA Simple\"\r\n"
                + "                },\r\n"
                + "                \"timeslot\": {\r\n"
                + "                    \"code\": \"AM\",\r\n"
                + "                    \"end\": \"13:00\",\r\n"
                + "                    \"name\": \"Morning\",\r\n"
                + "                    \"start\": \"08:00\"\r\n"
                + "                }\r\n"
                + "            }\r\n"
                + "        ]\r\n"
                + "    },\r\n"
                + "    {\r\n"
                + "        \"date\": \"2024-06-10\",\r\n"
                + "        \"slots\": [\r\n"
                + "            {\r\n"
                + "                \"createDate\": \"2024-05-22T11:54:58.240+01:00\",\r\n"
                + "                \"expireDate\": \"2024-05-22T11:59:58.240+01:00\",\r\n"
                + "                \"id\": 1176592671,\r\n"
                + "                \"intervention\": {\r\n"
                + "                    \"code\": \"FS\",\r\n"
                + "                    \"name\": \"FA Simple\"\r\n"
                + "                },\r\n"
                + "                \"timeslot\": {\r\n"
                + "                    \"code\": \"PM\",\r\n"
                + "                    \"end\": \"18:00\",\r\n"
                + "                    \"name\": \"Afternoon\",\r\n"
                + "                    \"start\": \"13:00\"\r\n"
                + "                }\r\n"
                + "            }\r\n"
                + "        ]\r\n"
                + "    }\r\n"
                + "]";

            JSONArray jsonArr = JSONArray.parseArray(json);
            System.out.println(jsonArr.toJavaObject(List.class));
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

期待的正确结果

能正常输出List信息,不再抛出转型异常

相关日志输出

java.lang.ClassCastException: class com.alibaba.fastjson.JSONObject cannot be cast to class com.alibaba.fastjson2.JSONObject (com.alibaba.fastjson.JSONObject and com.alibaba.fastjson2.JSONObject are in unnamed module of loader 'app') at com.alibaba.fastjson2.reader.ObjectReaderImplList.createInstance(ObjectReaderImplList.java:302) at com.alibaba.fastjson2.reader.ObjectReader.createInstance(ObjectReader.java:37) at com.alibaba.fastjson.JSONArray.toJavaObject(JSONArray.java:877) at com.test.TestMain.testMap(TestUrl.java:165) at com.test.TestMain.main(TestUrl.java:115)

附加信息

wenshao commented 3 months ago

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.52-SNAPSHOT/ 问题已修复,请用2.0.52-SNAPSHOT版本帮忙验证

mineYso commented 3 months ago

你好!

  非常感谢,使用最新快照分支验证,报告的问题已修复了。

在 2024-06-07 07:19:20,"Shaojin Wen" @.***> 写道:

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.52-SNAPSHOT/ 问题已修复,请用2.0.52-SNAPSHOT版本帮忙验证

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

wenshao commented 2 months ago

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