Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant