alibaba / alibaba/fastjson2

[BUG] JSON OBJECT parseObject 解析多层泛型对象 转化失败

Open
#2,771 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
*简要描述您碰到的问题。*
class com.alibaba.fastjson2.JSONObject cannot be cast to class com.cloud.xxx.dto.resp.inner.InnerUserRespDTO (com.alibaba.fastjson2.JSONObject and com.cloud.xxx.dto.resp.inner.InnerUserRespDTO are in unnamed module of loader 'app'

### 环境信息
*请填写以下信息:*

- OS信息: aliyun linux镜像 , mac,windows 都会出现
- JDK信息:jdk17
- 版本信息:fastjson2 2.0.33

openjdk version "17.0.7" 2023-04-18 LTS
OpenJDK Runtime Environment (Red_Hat-17.0.7.0.7-2) (build 17.0.7+7-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.7.0.7-2) (build 17.0.7+7-LTS, mixed mode, sharing)

### 重现步骤
*如何操作可以重现该问题:*
```java
//可在此输入示例代码

TypeReference>> typeRef = new TypeReference>>() {};
ResultDTO> responseDTO = JSON.parseObject(resultJson, typeRef.getType());
Page page = responseDTO.getData();
// 获取分页的对象列表
List resultList = page.getRecords();
// 报错地方
for(InnerUserRespDTO respDto:resultList){
// 业务处理
}
```

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
正常序列化

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*
class com.alibaba.fastjson2.JSONObject cannot be cast to class com.cloud.xxx.dto.resp.inner.InnerUserRespDTO (com.alibaba.fastjson2.JSONObject and com.cloud.xxx.dto.resp.inner.InnerUserRespDTO are in unnamed module of loader 'app'

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*
page为 mybatis-plus的 com.baomidou.mybatisplus.extension.plugins.pagination.Page 版本3.5.3.1

Contributor guide

Open the contributing guide

Research direction

Start at the JSON.parseObject call using the nested TypeReference>> and reproduce the cast failure with the MyBatis-Plus Page type mentioned in the report. Trace how the nested page records are deserialized; done means iterating page.getRecords() yields InnerUserRespDTO instances instead of JSONObject values.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.