杰克逊 - 反序列化一个基础枚举

时间:2012-01-09 15:01:03

标签: java json enums jackson

是否可以反序列化具有基于索引的枚举?

enum Status {
  Active,
  Inactive
}

{status:1}表示Status.Active,但杰克逊将其作为Status.Inactive :(

3 个答案:

答案 0 :(得分:46)

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);        

}}

答案 1 :(得分:10)

您可以为枚举创建自定义类型反序列化器:

public enum Status {
    ACTIVE,
    INACTIVE;
    public static Status fromTypeCode(final int typeCode) {
        switch(typeCode) {
        case 1: return ACTIVE;
        case 2: return INACTIVE;
        }
        throw new IllegalArgumentException("Invalid Status type code: " + typeCode);
    }
}

public class StatusDeserializer extends JsonDeserializer<Status> {
    @Override
    public Status deserialize(final JsonParser parser, final DeserializationContext context) throws IOException {
        return Status.fromTypeCode(parser.getValueAsInt());
    }
}

然后,您可以告诉杰克逊使用您的自定义反序列化器来获取属性:

public class WarpDrive {
    private Status status; 
    @JsonDeserialize(using = StatusDeserializer.class)
    public void setStatus(final Status status) {
        this.status = status;
    }
    public Status getStatus() {
        return this.status;
    }
}

您还可以配置Jackson对象映射器,以便为所有出现的目标类型使用自定义反序列化器。请参阅http://wiki.fasterxml.com/JacksonHowToCustomDeserializers

答案 2 :(得分:3)

枚举具有数字序号,从零开始,并按照声明的顺序分配给枚举中的每个值。例如,在您的代码Active中,序数0Inactive具有序数1。您可以在枚举值和序数之间来回切换,如下所示:

// ordinal=0, since Active was declared first in the enum
int ordinal = Status.Active.ordinal();

// enumVal=Active, since 0 is the ordinal corresponding to Active
Status enumVal = Status.values()[0];

显然,序数1对应Inactive(这不是杰克逊问题),如上所述,枚举中的序数是从零开始的。也许您应该修改代码以反映这一点,并确保{status:0}表示Status.Active