Jackson - deserialize one base enums

前端 未结 3 1557
说谎
说谎 2021-02-07 07:56

is it possible to deserialize enums, which have a one based index?

enum Status {
  Active,
  Inactive
}

{status:1} means Status.Active, but Jac

3条回答
  •  被撕碎了的回忆
    2021-02-07 08:40

    public enum Status {
    ACTIVE(1),
    INACTIVE(2);
    private final int value;
    Status(int v) {
        value = v;
    }
    @org.codehaus.jackson.annotate.JsonValue
    public int value() {
        return value;
    }  
    @org.codehaus.jackson.annotate.JsonCreator
    public static Status fromValue(int typeCode) {
        for (Status c: Status.values()) {
            if (c.value==typeCode) {
                return c;
            }
        }
        throw new IllegalArgumentException("Invalid Status type code: " + typeCode);        
    
    }}
    

提交回复
热议问题