enum枚举解析相关问题 [BUG]
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
fastjson2在2.0.38版本之后对枚举的解析问题得到了解决,现在我用的是最新版本2.0.39,发现在解析枚举时,支持jackson的注解,比如@JsonValue和@JsonCreator,这在之前的fastjson1好像是不支持的。不清楚这是bug还是特意做的适配,有点迷惑
### 环境信息
*请填写以下信息:*
- OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
- JDK信息: [e.g.:Openjdk 1.8.0_312]
- 版本信息:[e.g.:Fastjson2 2.0.39]
### 重现步骤
*如何操作可以重现该问题:*
运行main方法,在枚举类中断点@JsonCreator的方法getItem,会进入断点方法进行判断查找枚举类
```java
//枚举类
@Getter
@AllArgsConstructor
public enum RecruitWorkSource implements BaseEnum {
work_Online("NQJQXLJH", "dfsdfsdf", ""),
Building_Harbour("T59GKNQJQXLJHMYY", "", ""),
other("other", "其它", "");
@JsonValue
private final String value;
//描述
private final String description;
//推送地址
private final String pushUrl;
@JsonCreator
public static RecruitWorkSource getItem(String code) {
return BaseEnum.getEnum(RecruitWorkSource.class, code);
}
}
```
1. 测试main方法
```java
public static void main(String[] args) throws JsonProcessingException {
//fastjson测试
String jsonString = JSON.toJSONString(RecruitWorkSource.work_Online) ;//返回结果200,也就是@JsonValue注解的字段值
RecruitWorkSource enumsTestDTO1 = JSON.parseObject(jsonString, RecruitWorkSource.class);
RecruitWorkSource enumsTestDTO2 = JSON.parseObject(jsonString,new TypeReference(){});
//Jackson
String json = MyJacksonUtils.toJson(RecruitWorkSource.work_Online);
RecruitWorkSource obj = MyJacksonUtils.toObj(json, RecruitWorkSource.class);
//Jackson原使用
ObjectMapper mapper = new ObjectMapper();
String json1 = mapper.writeValueAsString(RecruitWorkSource.work_Online);
RecruitWorkSource obj2 = mapper.readValue(json1, RecruitWorkSource.class);
System.out.println("11111");
}
```
### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
### 相关日志输出
*请复制并粘贴任何相关的日志输出。*
#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*
Contributor guide
Research direction
Start with the provided main method and the enum using @JsonValue and @JsonCreator, then compare fastjson2 2.0.39 with the Jackson behavior described. The issue does not specify the expected result or identify a test or source file, so the intended enum parsing behavior must be clarified before a fix can be considered complete.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100