Skip to content

[BUG]序列化包含枚举字段的对象为JSONObject时,字段的值为枚举对象,而不是字符串,哪怕使用了JSONWriter.Feature.WriteEnumUsingToString #3464

Open
@oyealex

Description

@oyealex

问题描述

序列化包含枚举字段的对象为JSONObject时,字段的值为枚举对象,而不是字符串,哪怕使用了JSONWriter.Feature.WriteEnumUsingToString

环境信息

请填写以下信息:

  • OS信息: [Win 10]
  • JDK信息: [Openjdk 8]
  • 版本信息:[Fastjson2 2.0.56]

重现步骤

示例代码:

public static void main(String[] args) {
    Data data = new Data(TimeUnit.MINUTES);
    JSONObject json = (JSONObject) JSON.toJSON(data, JSONWriter.Feature.WriteEnumsUsingName);
    System.out.println(json.get("unit").getClass());
}

@Getter
@AllArgsConstructor
private static class Data {
    private TimeUnit unit;
}

输出结果:

class java.util.concurrent.TimeUnit$5

期待的正确结果

预期应输出class java.lang.String,fastjson默认输出的就是string,属于表现不一致,会导致应用的类型兼容问题

相关日志输出

附加信息

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions