alibaba / alibaba/fastjson2

[BUG] JSONField注解的unwrapped属性失效

Open
#1,885 1 comment 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

### 问题描述

启用 unwrapped 属性后 效果不符合预期
当有多个 unwrapped 属性的字段时 只有字段名按字符串排序时的最后一位 能正常反序列化
序列化时 是 没有问题的

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

- OS信息: [e.g.:Windows11]
- JDK信息: [e.g.:Oraclejdk 1.8.0_351]
- 版本信息:[e.g.:Fastjson2 2.0.40]

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

```java
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import lombok.Getter;

public class FastJsonTest {

public static void main(String[] args) {
User user;
JSONObject userInfo = JSONObject.of();
userInfo.put("姓名", "xxx");
userInfo.put("性别", 1);
userInfo.put("身高", 149);
userInfo.put("体重", 41);
userInfo.put("胸围", 93);
userInfo.put("腰围", 52);
userInfo.put("臀围", 85);
System.out.println(userInfo);
user = userInfo.to(User.class);
System.out.println("JSONObject#to: " + user); // info and bwh is null
user = userInfo.toJavaObject(User.class);
System.out.println("JSONObject#toJavaObject: " + user); // info and bwh is null
user = JSON.parseObject(userInfo.toJSONString(), User.class);
System.out.println("JSON#parseObject: " + user); // bwh is null
}

@Data
public static class User {
@JSONField(unwrapped = true)
private Info info;
@JSONField(unwrapped = true)
private BWH bwh;
}

@Data
public static class Info {
@JSONField(name = "姓名")
private String name;
@JSONField(name = "性别")
private Sex sex;
@JSONField(name = "身高")
private Integer stature;
@JSONField(name = "体重")
private Integer weight;
}

@Data
public static class BWH {
@JSONField(name = "胸围")
private Integer bustline;
@JSONField(name = "腰围")
private Integer waistline;
@JSONField(name = "臀围")
private Integer hipline;
}

@Getter
public enum Sex {
MALE(0, "男"),
FEMALE(1, "女");

@JSONField(value = true)
private final int key;
private final String name;

Sex(int key, String name) {
this.key = key;
this.name = name;
}
}
}
```

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

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

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

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied Java reproducer and compare JSONObject#to, JSONObject#toJavaObject, and JSON#parseObject with the two @JSONField(unwrapped = true) fields. Trace the deserialization path for Info and BWH, then add a regression test covering all Chinese-named fields; done means both unwrapped objects deserialize completely while serialization remains unchanged.

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.