Jackson - deserialize one base enums

前端 未结 3 1556
说谎
说谎 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:35

    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. 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. Maybe you should fix your code to reflect that, and make sure that {status:0} means Status.Active.

提交回复
热议问题