alibaba / fastjson2

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

[BUG]WriteNulls导致Long、Double字段序列化为null #3049

Open wdollar opened 1 month ago

wdollar commented 1 month ago

问题描述

使用注解@JSONField(serializeFeatures = {JSONWriter.Feature.WriteNulls})的Long、Double字段在序列化时无论是否有值均输出为null。内部类不受影响。

环境信息

重现步骤

代码

public class Main {
    public static void main(String[] args) {
        Clazz c = new Clazz();
        c.setD(1D);
        System.out.println(JSON.toJSONString(c));

        InnerClazz ic = new InnerClazz();
        ic.setD(1D);
        System.out.println(JSON.toJSONString(ic));
    }

    static class InnerClazz {
        @JSONField(serializeFeatures = {JSONWriter.Feature.WriteNulls})
        private Double d;

        public Double getD() { return d; }
        public void setD(Double d) { this.d = d; }
    }
}

public class Clazz {
    @JSONField(serializeFeatures = {JSONWriter.Feature.WriteNulls})
    private Double d;

    public Double getD() { return d; }
    public void setD(Double d) { this.d = d; }
}

输出为

{"d":null}
{"d":1.0}

期待的正确结果

{"d":1.0}
{"d":1.0}

相关日志输出

(无)

附加信息

(无)

denghejun commented 2 weeks ago

请问在下次release之前,现在如果用了这个版本,有没有什么临时解决方案?

wdollar commented 2 weeks ago

请问在下次release之前,现在如果用了这个版本,有没有什么临时解决方案?

我从业务逻辑上回避了这个场景,你可以用新的ObjectWriterCreatorASM先搞个2.0.53u1什么的临时版本顶上,等正式版发布了再升级

wenshao commented 2 weeks ago

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.54-SNAPSHOT/ 可以先用这个快照版本