alibaba / alibaba/fastjson2

[QUESTION] WriteClassName 两级以上的内嵌对象没有生成@type

Open
#3,520 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 请描述您的问题
*询问有关本项目的使用和其他方面的相关问题。*
```
public class Test {

public static void main(String[] args) {
Person p = new Person();
p.setContact(new Contact());
System.out.println(com.alibaba.fastjson2.JSON.toJSONString(new RpcResult<>(p),
JSONWriter.Feature.WriteClassName,
// JSONWriter.Feature.WriteEnumUsingToString,
JSONWriter.Feature.WriteMapNullValue));
}
}
```

生成的结果:
```
{"@type":"com.xxx.RpcResult","data":{"@type":"com.xxx.model.Person","active":null,"activeTime":null,"birthday":null,"companyName":null,"contact":{"privateContact":null,"publicContact":null},"createTime":null,"department":null,"eid":null,"email":null,"fullPinyin":null,"gender":null,"hide":false,"id":null,"isAdmin":0,"isHidePhone":0,"jobNo":null,"jobTitle":null,"name":null,"oId":null,"openId":null,"orgId":null,"orgInfoId":null,"phone":null,"photoUrl":null,"registerDate":null,"status":0,"tid":null,"uid":null,"updateTime":null,"userName":null,"userType":0,"wbNetworkId":null,"wbUserId":null,"weights":0},"error":null,"errorCode":0,"success":true}
```
期望的结果:
```
{"@type":"com.xxx.RpcResult","data":{"@type":"com.xxx.model.Person","active":null,"activeTime":null,"birthday":null,"companyName":null,"contact":{"@type":"com.xxx.model.Contact","privateContact":null,"publicContact":null},"createTime":null,"department":null,"eid":null,"email":null,"fullPinyin":null,"gender":null,"hide":false,"id":null,"isAdmin":0,"isHidePhone":0,"jobNo":null,"jobTitle":null,"name":null,"oId":null,"openId":null,"orgId":null,"orgInfoId":null,"phone":null,"photoUrl":null,"registerDate":null,"status":0,"tid":null,"uid":null,"updateTime":null,"userName":null,"userType":0,"wbNetworkId":null,"wbUserId":null,"weights":0},"error":null,"errorCode":0,"success":true}
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the nested-object case from the issue using JSON.toJSONString with JSONWriter.Feature.WriteClassName and WriteMapNullValue. Trace why the Contact object inside Person is missing @type, and consider the work complete when the output includes com.xxx.model.Contact as shown in the expected JSON.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
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.