alibaba / alibaba/fastjson2

[BUG]v2.0.22版本JSON.parseObject(result, new TypeReference<AjaxResult.PageResult<TagView>>() {})转化异常

Open
#1,072 3 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

### 问题描述
*简要描述您碰到的问题。*
字符串转嵌套范式类时异常

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

- OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
- JDK信息: [e.g.:Openjdk 1.8.0_312]
- 版本信息:[e.g.:Fastjson2 2.0.22]

### 重现步骤
*如何操作可以重现该问题:*

1. 使用 `xxx.xxx` 方法
2. 输入 `...` 数据
3. 出现 `...` 错误
TagView类
```java
@Getter
@Setter
public class TagItemModel implements Serializable {
private static final long serialVersionUID = -6280378922670952440L;
protected String item;
protected String type;
protected Integer history;
protected Integer circle;
protected String description;
}

@Getter
@Setter
public class TagView implements Comparable,Serializable{

private static final long serialVersionUID = 4164477310819397654L;

private String namespace;
private String tag;
private String type;
// private int source;
private List items;
private Integer history;
private String label;
private String description;

@Override
public int compareTo(TagView o) {
if (this.namespace.compareTo(o.getNamespace()) == 0) {
return this.tag.compareTo(o.tag);
}
else {
return this.namespace.compareTo(o.getNamespace());
}
}
}
```
AjaxResult.PageResult类
```java
@Getter
@Setter
public class AjaxResult {
private String message;
private int status;
private long timestamp = Instant.now().toEpochMilli();

@Getter
@Setter
public static class MutiResult extends AjaxResult {
private List data;

public MutiResult(){
super();
}

public MutiResult(List data){
super();
this.data = data;
}

public MutiResult(int status) {
this(status, null);
}

public MutiResult(int status,String message){
super(status,message);
}

public MutiResult(int status,String message,List data){
super(status,message);
this.data = data;
}

}

@Getter
@Setter
public static class PageResult extends MutiResult {
private int pageNum = 1;
private int pageSize;
private long total;

public PageResult(){
super();
}

public PageResult(List data){
super(data);
this.pageSize = data==null?0:data.size();
this.total = this.pageSize;
}

public PageResult(long total, List data){
super(data);
this.pageSize = data==null?0:data.size();
this.total = total;
}

public PageResult(String message,long total, List data){
super(AJAX_STATUS_SUCCESS,message,data);
this.pageNum = 1;
this.pageSize = data==null?0:data.size();
}

public PageResult(int status) {
this(status, "");
}

public PageResult(int status,String message){
super(status,message);
}

public PageResult(int status,String message,List data){
super(status,message,data);
}
public PageResult(int pageNum, int pageSize, long total, List data){
super(data);
this.pageNum = pageNum;
this.pageSize = pageSize;
this.total = total;
}

public PageResult(int status,String message,int pageNum, int pageSize, long total, List data){
super(status,message,data);
this.pageNum = pageNum;
this.pageSize = pageSize;
this.total = total;
}

@Override
public String toString() {
return JSON.toJSONString(this, SystemGlobalConfig.FASTJSON_DEFAULT_SERIALIZER_FEATURES);
}
}
}

```
**### Json数据**
```json
{
"status":0,
"message":"查询成功",
"timestamp":1673586491000,
"data":[
{
"namespace":"unit08",
"tag":"AM",
"history":0,
"items":[
{
"circle":0,
"item":"AV",
"type":"double"
}
]
}
]
}
```

```java
//result为上述json对象值
AjaxResult.PageResult resultTag = JSON.parseObject(result, new TypeReference>() {})
```
### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*

**### 正确发序列化**
### 相关日志输出
*请复制并粘贴任何相关的日志输出。*

### **错误信息提示:HashMap无法转化TagView**

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*

Contributor guide

Open the contributing guide

Research direction

Start at the JSON.parseObject call using TypeReference> and reproduce it with the supplied JSON and nested PageResult, MutiResult, TagView, and TagItemModel classes. Trace how the generic data list is deserialized, then verify that each data element becomes a TagView rather than a HashMap and that the conversion no longer reports the shown error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.