@IntDef Android支持与杰克逊反序列化的公告

loc*_*ost 22 android json jackson android-support-library

使用JacksonAnnotations和Android支持注释.我的POJO是:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Schedule {
    public static final int SUNDAY = 0;
    public static final int MONDAY = 1;
    public static final int TUESDAY = 2;
    public static final int WEDNESDAY = 3;
    public static final int THURSDAY = 4;
    public static final int FRIDAY = 5;
    public static final int SATURDAY = 6;

    private Integer weekday;

    public Schedule() {
    }

    @Weekday
    public Integer getWeekday() {
        return weekday;
    }

    public void setWeekday(@Weekday Integer weekday) {
        this.weekday = weekday;
    }

    @Retention(RetentionPolicy.RUNTIME)
    @IntDef({SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY})
    public @interface Weekday {}
}
Run Code Online (Sandbox Code Playgroud)

从支持我得到对象:

{"schedule":{"weekday":"MONDAY"}}
Run Code Online (Sandbox Code Playgroud)

我想要的是将Weekday映射到常量中定义的整数值.有什么方法可以达到这个目的吗?

更新:主要目的是优化(你应该严格避免在Android上使用枚举像它说在这里).

Uri*_*lit 1

也许我错过了一些东西,但为什么不做这样的事情:

public class Schedule {
    public enum Weekday {
        SUNDAY(0),
        MONDAY(1),
        TUESDAY(2),
        WEDNESDAY(3),
        THURSDAY(4),
        FRIDAY(5),
        SATURDAY(6);

        private final Integer weekday;

        Weekday(Integer weekday) {
            this.weekday = weekday;
        }

        public Integer getWeekday() {
            return weekday;
        }
    }

    private Weekday weekday;

    public Integer getWeekday() {
        return weekday.getWeekday();
    }

    public void setWeekday(Weekday weekday) {
        this.weekday = weekday;
    }

}
Run Code Online (Sandbox Code Playgroud)