alibaba / alibaba/fastjson2

[BUG] 从JSONObject转换为对象的时候结果不一致

Open
#1,791 13 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

### 问题描述
*简要描述您碰到的问题。*
从JSONObject转换为对象的时候结果不一致

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

- OS信息: [e.g.:mac pro m2 16gb ]
- JDK信息: [e.g.:Azul Zulu 11.0.19]
- 版本信息:[e.g.:Fastjson2 2.0.39.x]

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

1. 使用 `xxx.xxx` 方法
2. 输入 `...` 数据
3. 出现 `...` 错误
```java
enum Status {
VALID(1),
INVALID(-1);

public final int value;

Status(int value) {
this.value = value;
}

public Integer getValue() {
return value;
}

}
@Setter
@Getter
abstract class Base < S extends Enum >{
private S status;

}

@Setter
@Getter
class Derive extends Base{
private Status s;
}

public class FastJson {
public static void main(String[] args) {
Derive derive = new Derive();
derive.setStatus(Status.VALID);
derive.setS(Status.INVALID);
JSONObject params = new JSONObject();
params.put("data", com.alibaba.fastjson.JSONObject.toJSONString(derive));

JSONObject data = params.getJSONObject("data");
String data1 = params.getString("data");
Derive derive1 = data.toJavaObject(Derive.class);
Derive derive2 = JSON.to(Derive.class,data);
Derive derive3 = JSON.parseObject(data1,Derive.class);
Derive derive4 = data.toJavaObject(Derive.class);
Derive derive5 = JSON.to(Derive.class,data);
System.out.println("");

}
}

```

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
可以看到下面在经历JSON.parseObject() 前后 反序列化的结果不一致,缺少了父类的泛型枚举,正常来说结果应该都是和derive3、derive4、derive5一样,而不是出现前2种情况

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*
![image](https://github.com/alibaba/fastjson2/assets/69097897/1b374802-8bc1-489d-a3ee-f6c81a2192ba)

Contributor guide

Open the contributing guide

Research direction

Start with the supplied Java reproducer and compare the results from JSONObject.toJavaObject, JSON.to, and JSON.parseObject for the generic parent enum field. Trace the deserialization path used by each entry point, then confirm the fix by reproducing the example and verifying that all results preserve the parent class's enum type.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.