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]基本类型在Feature.NotWriteDefaultValue下表现不一致 #3186

Open
JintaoXie opened this issue Nov 28, 2024 · 0 comments
Open

[BUG]基本类型在Feature.NotWriteDefaultValue下表现不一致 #3186

JintaoXie opened this issue Nov 28, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@JintaoXie
Copy link

JintaoXie commented Nov 28, 2024

问题描述

简要描述您碰到的问题。
toJSONString()Feature.NotWriteDefaultValue开启的情况下,float/double浮点数基本类型表现与int/long/boolean等类型表现不一致。
具体表现为:
在value等于默认值时,float/double类型会保留field,value为0.0,int/long/boolean不会保留field。

环境信息

请填写以下信息:

  • OS信息: Windows 10 19045.5131 / Intel(R) Core(TM) i5-9500 CPU @ 3.00GHz / 16.0 GB
  • JDK信息: Openjdk 21.0.2
  • 版本信息:Fastjson2 2.0.53

重现步骤

如何操作可以重现该问题:

  1. 使用 toJSONString() 方法
  2. 输入 List<Entry>,其中Entry class包含不同基本类型成员变量
  3. 结果 [{"doubleV":0.0,"floatV":0.0},{"doubleV":0.0,"floatV":0.0}]
public class Example {

    public static class Entry {
        public int intV;
        public long longV;
        public float floatV;
        public double doubleV;
        public boolean booleanV;
    }

    public static void main(String[] args) {
        List<Entry> entryList = List.of(new Entry(), new Entry());
        String msg = JSON.toJSONString(entryList, JSONWriter.Feature.NotWriteDefaultValue);
        System.out.println(msg);
    }
}

期待的正确结果

期望输出空字符串

相关日志输出

实际输出结果:
image

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@JintaoXie JintaoXie added the bug Something isn't working label Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant