简体   繁体   English

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

[英]Jackson - deserialize one base enums

is it possible to deserialize enums, which have a one based index? 是否可以反序列化具有一个索引的枚举?

enum Status {
  Active,
  Inactive
}

{status:1} means Status.Active, but Jackson makes it Status.Inactive :( {status:1}表示Status.Active,但杰克逊使其成为Status.Inactive :(

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

}}

You can create a custom type deserialiser for your enum: 您可以为枚举创建自定义类型反序列化器:

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

You can then tell Jackson to use your custom deserialiser for a property: 然后,您可以告诉Jackson使用您的自定义deserialiser作为属性:

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

You can also configure the Jackson object mapper to use your custom deserialiser for all occurrences of the target type. 您还可以配置Jackson对象映射器,以便为所有出现的目标类型使用自定义反序列化器。 See http://wiki.fasterxml.com/JacksonHowToCustomDeserializers . 请参阅http://wiki.fasterxml.com/JacksonHowToCustomDeserializers

Enums have numeric ordinals , starting in zero, and get assigned to each value in an enumeration in the order in which they were declared. 枚举具有数字序数 ,从零开始,并按照它们被声明的顺序分配给枚举中的每个值。 For example, in your code Active has ordinal 0 and Inactive has ordinal 1 . 例如,在您的代码中, Active序号为0Inactive序号为1 You can go back and forth between the value of an enum and its ordinal, like this: 您可以在枚举值与其序数之间来回切换,如下所示:

// 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];

Clearly the ordinal 1 corresponds to Inactive (it's not a Jackson problem), as explained above, ordinals in an enumeration are zero-based. 显然,序数1对应于Inactive (它不是杰克逊问题),如上所述,枚举中的序数是从零开始的。 Maybe you should fix your code to reflect that, and make sure that {status:0} means Status.Active . 也许你应该修改你的代码以反映这一点,并确保{status:0}表示Status.Active

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM