alibaba / alibaba/fastjson2

Map null的时候输出json为null 不应该是“{}”才合适吗

Open
#815 3 comments 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

### 请描述您的问题
*询问有关本项目的使用和其他方面的相关问题。*
环境信息
请填写以下信息:

springboot 2.5.13
fastjson2 2.0.14
JDK信息 1.8

是我配置出现了问题嘛?

``` java
@Bean
public HttpMessageConverters customConverters() {
List> messageConverters = new ArrayList<>();

FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//自定义配置...
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss:ms");

config.setReaderFeatures(
JSONReader.Feature.FieldBased, // 反序列化
JSONReader.Feature.SupportArrayToBean, // 支持数据映射的方式
JSONReader.Feature.InitStringFieldAsEmpty, // 初始化String字段为空字符串""
JSONReader.Feature.AllowUnQuotedFieldNames // 不支持不带双引号的key
);

config.setWriterFeatures(
JSONWriter.Feature.PrettyFormat, // 格式化输出
JSONWriter.Feature.WriteNulls, // 序列化输出空值字段
JSONWriter.Feature.NullAsDefaultValue, // 将空置输出为缺省值,Number类型的null都输出为0,String类型的null输出为"",数组和Collection类型的输出为[]
JSONWriter.Feature.BrowserCompatible // 在大范围超过JavaScript支持的整数,输出为字符串格式
);

converter.setFastJsonConfig(config);
converter.setDefaultCharset(StandardCharsets.UTF_8);
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
messageConverters.add(0, converter);

return new HttpMessageConverters(true, messageConverters);

}
```
实际输出的结果
``` json
{
"map": null
}
```
期待的正确结果
``` json
{
"map": {}
}
```
不应该为 "map": {} 才合适吗

Contributor guide

Open the contributing guide

Research direction

Reproduce the reported output with Spring Boot 2.5.13, fastjson2 2.0.14, JDK 8, and the shown FastJsonHttpMessageConverter configuration. Start by tracing how FastJsonConfig writer features, especially WriteNulls and NullAsDefaultValue, handle a null Map field. Done means the behavior is clarified or corrected so the relevant serialization case produces the agreed JSON output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.