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] enum deserialization not working when using JSONField(value = true) #2154

Closed
crclz opened this issue Jan 5, 2024 · 1 comment
Closed
Labels
bug Something isn't working fixed
Milestone

Comments

@crclz
Copy link

crclz commented Jan 5, 2024

问题描述

参考了 https://alibaba.github.io/fastjson2/annotations_cn.html "1.6 通过JSONField(value = true)配置 ..." 进行enum的解析,但是不成功。

更具体地,如果先进行一次序列化(结果不储存到任何变量),再进行反序列化,则成功;直接进行反序列化,则不成功。可以直接用以下测试用例进行复现。

环境信息

请填写以下信息:

  • OS信息: [e.g.:MacOS 13.5.2 (22G91)]
  • JDK信息: [e.g.:Openjdk 1.8.0_332]
  • 版本信息:[e.g.:Fastjson2 2.0.44]

重现步骤

  • intEnumDeserialize1: success
  • intEnumDeserialize2: failure
package com.bytedance.data.edc.pricecmp.rulecore.domain.services;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class FastJsonTest {

    // failure
    @Test
    public void intEnumDeserialize1() {
        Bean1 bean1 = JSON.parseObject("{\"type\":102}", Bean1.class);
        assertEquals(102, bean1.type.getCode());
    }

    // success
    @Test
    public void intEnumDeserialize2() {
        // add this
        {
            Bean1 bean = new Bean1();
            bean.type = Type.S;
            JSON.toJSONString(bean);
        }

        intEnumDeserialize1();
    }
}

class Bean1 {
    public Type type;
}

enum Type {
    X(101),
    M(102),
    S(103);

    private final int code;

    Type(int code) {
        this.code = code;
    }

    @JSONField(value = true)
    public int getCode() {
        return code;
    }

}

期待的正确结果

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

相关日志输出

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

附加信息

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

@wenshao
Copy link
Member

wenshao commented Jan 7, 2024

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

@wenshao wenshao closed this as completed Jan 7, 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 fixed
Projects
None yet
Development

No branches or pull requests

2 participants