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]枚举转换异常 #1355

Closed
zhuangjiaju opened this issue Apr 13, 2023 · 3 comments
Closed

[BUG]枚举转换异常 #1355

zhuangjiaju opened this issue Apr 13, 2023 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@zhuangjiaju
Copy link

zhuangjiaju commented Apr 13, 2023

问题描述

简要描述您碰到的问题。

枚举

public enum TestEnum {
    E1,E2;
}

对象

@Data
@Builder
public class TestDTO {
    private TestEnum testEnum;
}

测试代码

  @Test
    public void test() {
        log.info("dto:{}", JSON.toJSONString(TestDTO.builder().testEnum(TestEnum.E1).build()));
        log.info("enum:{}", JSON.toJSONString(TestEnum.E1));
    }

输出

// 就是这里有问题 居然输出了 0 ,希望输出的是枚举的.name()
11:06:49.315 [main] INFO com.alibaba.spring.test.fastjson.FastjsonTest - dto:{"testEnum":0}
11:06:49.334 [main] INFO com.alibaba.spring.test.fastjson.FastjsonTest - enum:"E1"

fastjson没有问题 fastjson2有问题,期望 输出:
dto:{"testEnum":"E1"}

环境信息

请填写以下信息:

  • OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:2.0.27

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。
期望:

  • 在枚举的情况下都是调用.name() 都是输出 "E1" ,而不是 在对象内输出0 ,纯枚举输出"E1"
  • 在fastjson里面都是出"E1"的,所以没问题。 建议 1 和 2的默认值一样,用户体验更好。

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

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

@zhuangjiaju zhuangjiaju added the bug Something isn't working label Apr 13, 2023
@wenshao
Copy link
Member

wenshao commented Apr 13, 2023

使用JSONWriter.Feature.WriteEnumsUsingName,比如:

JSON.toJSONString(obj, JSONWriter.Feature.WriteEnumsUsingName);

@wenshao wenshao added this to the 2.0.28 milestone Apr 13, 2023
@wenshao wenshao added the fixed label Apr 13, 2023
@wenshao
Copy link
Member

wenshao commented Apr 13, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.28-SNAPSHOT/
问题已修复,请帮忙有2.0.8-SNAPSHOT版本验证,2.0.28版本预计会在周末发布

@wenshao
Copy link
Member

wenshao commented Apr 15, 2023

@wenshao wenshao closed this as completed Apr 15, 2023
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

2 participants