Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@IntDef Android support annontation with Jackson deserializing

Using JacksonAnnotations along with Android Support Annotations. My POJO is:

@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 {}
}

From backed i get object:

{"schedule":{"weekday":"MONDAY"}}

What i want is to map Weekday to it's integer value defined in constants. Is there any way i can achieve this?

Update: The main purpose is optimization (You should strictly avoid using enums on Android like it said here).

like image 790
localhost Avatar asked Jun 30 '15 09:06

localhost


2 Answers

Since you can count on weekdays not growing out of control, creating a static map seems like a pretty clean solution. Providing a @JsonCreator factory named fromString will work with Jackson and will also play nicely with JAX-RS:

public class Schedule {
    ...
    private static final ImmutableMap<String, Schedule> WEEKDAY_TO_INT_MAP =
            ImmutableMap.<String, Schedule>builder()
                .put("SUNDAY", withIntWeekday(0))
                .put("MONDAY", withIntWeekday(1))
                .put("TUESDAY", withIntWeekday(2))
                .put("WEDNESDAY", withIntWeekday(3))
                .put("THURSDAY", withIntWeekday(4))
                .put("FRIDAY", withIntWeekday(5))
                .put("SATURDAY", withIntWeekday(6))
                .build();

    public static Schedule withIntWeekday(int weekdayInt) {
        Schedule schedule = new Schedule();
        schedule.setWeekday(weekdayInt);
        return schedule;
    }

    @JsonCreator
    public static Schedule fromString(String weekday) {
        return WEEKDAY_TO_INT_MAP.get(weekday);
    }
    ...
}
like image 121
Sam Berry Avatar answered Nov 02 '22 20:11

Sam Berry


Maybe I am missing something, but why not do something like this:

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

}
like image 37
Uri Shalit Avatar answered Nov 02 '22 19:11

Uri Shalit