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]JSONCreator枚举失效 #829

Closed
yqt2016 opened this issue Oct 11, 2022 · 3 comments
Closed

[BUG]JSONCreator枚举失效 #829

yqt2016 opened this issue Oct 11, 2022 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@yqt2016
Copy link

yqt2016 commented Oct 11, 2022

package com.test.fastjson2;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONCreator;
import com.alibaba.fastjson2.annotation.JSONType;
import lombok.Getter;

import java.util.Optional;

@JSONType(writeEnumAsJavaBean = true)
@Getter
public enum ServiceCode {
    /**
     * SUCCESS
     */
    SUCCESS("00", "SUCCESS"),
    /**
     * SERVER_ERROR
     */
    SERVER_ERROR("01", "ERROR");

    ServiceCode(String code, String msgId) {
        this.code = code;
        this.msgId = msgId;

    }

    private String code;
    private String msgId;
    private String msg;

    public  void setMsg(String msg){
        this.msg=msg;
    }

    @JSONCreator
    public static ServiceCode valueOf( String code, String msg) {
        ServiceCode status;
        switch (code) {
            case "0000":
                status = ServiceCode.SUCCESS;
                break;
            default:
                status = ServiceCode.SERVER_ERROR;
                break;

        }
        Optional.ofNullable(msg).ifPresent(msgVal -> status.setMsg(msgVal));
        return status;
    }

    public static void main(String[] args) {
        ServiceCode statusCode= ServiceCode.SERVER_ERROR;
        statusCode.setMsg("1111");
        String s = JSON.toJSONString(statusCode);
        ServiceCode statusCode1 = JSON.parseObject(s, ServiceCode.class);
        System.out.println(s);
        System.out.println(statusCode1);
    }

@yqt2016 yqt2016 added the bug Something isn't working label Oct 11, 2022
@yqt2016
Copy link
Author

yqt2016 commented Oct 11, 2022

enum使用(writeEnumAsJavaBean = true序列化成为java bean之后,怎么使用jsoncreator的方式反序列化成功?目前使用自定义反序列化类是可以的,但是jsoncreator的方式失败。

@wenshao wenshao added this to the 2.0.16 milestone Oct 12, 2022
@wenshao
Copy link
Member

wenshao commented Oct 12, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.16-SNAPSHOT/
问题已经修复,请用2.0.16-SNAPSHOT帮忙验证,2.0.16版本预计在10月30日前发布

@wenshao wenshao added the fixed label Oct 12, 2022
@wenshao
Copy link
Member

wenshao commented Oct 22, 2022

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

@wenshao wenshao closed this as completed Oct 22, 2022
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