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

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

Closed
mineYso opened this issue Jun 4, 2024 · 3 comments
Assignees
Labels
bug Something isn't working fixed
Milestone

Comments

@mineYso
Copy link

mineYso commented Jun 4, 2024

问题描述

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

环境信息

  • OS信息: Windows 10 专业版 22H2 19045.4412
  • JDK信息:openjdk version "17.0.8" 2023-07-18
  • 版本信息:Fastjson2 2.0.49~2.0.51

重现步骤

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

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)

附加信息

@mineYso mineYso added the bug Something isn't working label Jun 4, 2024
@yanxutao89 yanxutao89 self-assigned this Jun 5, 2024
@wenshao wenshao added this to the 2.0.52 milestone Jun 5, 2024
yanxutao89 added a commit that referenced this issue Jun 6, 2024
wenshao pushed a commit that referenced this issue Jun 6, 2024
* fix ignore cast exception, for issue #2665
@wenshao wenshao added the fixed label Jun 6, 2024
@wenshao
Copy link
Member

wenshao commented Jun 6, 2024

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

@mineYso
Copy link
Author

mineYso commented Jun 21, 2024 via email

@wenshao
Copy link
Member

wenshao commented Jul 14, 2024

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

@wenshao wenshao closed this as completed Jul 14, 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

3 participants