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

Enum使用ObjectSerializer自定义序列化类不起作用 #607

Closed
wenshao opened this issue May 4, 2016 · 0 comments
Closed

Enum使用ObjectSerializer自定义序列化类不起作用 #607

wenshao opened this issue May 4, 2016 · 0 comments
Milestone

Comments

@wenshao
Copy link
Member

wenshao commented May 4, 2016

在1.2.11版本中中,Enum自定义序列化不起作用

public void test_serialize() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.put(ResultCode.class, new ResultCodeSerilaizer());

    Result result = new Result();
    result.code = ResultCode.SIGN_ERROR;
    String json = JSON.toJSONString(result, config);
    Assert.assertEquals("{\"code\":17}", json); // 1.2.11 failed. {"code":"SIGN_ERROR"}
}

public static class Result {
    public ResultCode code;
}

public static enum ResultCode {
                        SUCCESS(1), 
                        ERROR(-1), 
                        UNKOWN_ERROR(999), 
                        LOGIN_FAILURE(8), 
                        INVALID_ARGUMENT(0),
                        SIGN_ERROR(17);

    private final int value;

    ResultCode(int value){
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

public static class ResultCodeSerilaizer implements ObjectSerializer {
    public void write(JSONSerializer serializer, 
                      Object object, 
                      Object fieldName, 
                      Type fieldType,
                      int features) throws IOException {
        serializer.write(((ResultCode) object).value); 
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant