alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.79k stars 495 forks source link

[BUG]WriteNulls序列化时丢失部分null值字段 #2952

Closed wdollar closed 2 months ago

wdollar commented 2 months ago

问题描述

在部分字段上使用@JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls)注解,序列化时无法正常序列化null值。

环境信息

重现步骤

代码如下

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;

import lombok.Data;

@Data
public class C {
    @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls)
    private Long l;

    @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls)
    private Double d;

    @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls)
    private Integer i;

    @JSONField(serializeFeatures = JSONWriter.Feature.WriteNulls)
    private String s;

    public static void main(String[] args) {
        System.out.println(JSON.toJSONString(new C()));
    }
}

输出结果

{"i":null,"s":null}

期待的正确结果

{"l":null,"d":null,"i":null,"s":null}

相关日志输出

附加信息

wenshao commented 2 months ago

https://github.com/alibaba/fastjson2/releases/tag/2.0.53 问题已修复,请用新版本