Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wdollar opened this issue Oct 8, 2024 · 4 comments
Closed

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

wdollar opened this issue Oct 8, 2024 · 4 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@wdollar
Copy link

wdollar commented Oct 8, 2024

问题描述

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

环境信息

  • OS信息: Win11 23H2 22631.4249
  • JDK信息:Eclipse Adoptium Temurin-17.0.12+7
  • 版本信息:Fastjson2 2.0.53

重现步骤

代码

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
Copy link

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

@wdollar
Copy link
Author

wdollar commented Nov 1, 2024

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

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

@wenshao
Copy link
Member

wenshao commented Nov 2, 2024

@wenshao
Copy link
Member

wenshao commented Jan 12, 2025

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

@wenshao wenshao closed this as completed Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

3 participants