@IdefDef Android支持annonation与杰森反序列化

使用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 {} } 

从支持我得到的对象:

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

我想要的是将Weekday映射到常量中定义的整数值。 有什么办法可以做到这一点?

更新 :主要目的是优化( 你应该严格避免在Android上使用枚举,就像这里所说的那样)。

由于你可以指望在平日里不会失去控制,创build一个静态地图就像是一个非常干净的解决scheme。 提供一个名为fromString@JsonCreator工厂将和Jackson一起工作,并且还可以和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); } ... } 

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

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

如果后端以stringforms返回"MONDAY" ,那么你可能不应该使用@IntDef因为你必须pipe理将"MONDAY"转换为一个整数值。

我会尝试使用@StringDef注释,而不用担心后端和客户端应用程序之间的任何types的转换。 唯一的问题是你需要从你的后端获得可靠的响应,并提前就命名约定达成一致。

如果您将@IntDeftypes@IntDef@StringDef则如下所示:

 @JsonIgnoreProperties(ignoreUnknown = true) public class Schedule { public static final String SUNDAY = "SUNDAY"; public static final String MONDAY = "MONDAY"; public static final String TUESDAY = "TUESDAY"; public static final String WEDNESDAY = "WEDNESDAY"; public static final String THURSDAY = "THURSDAY"; public static final String FRIDAY = "FRIDAY"; public static final String SATURDAY = "SATURDAY"; private String weekday; public Schedule() { } @Weekday public String getWeekday() { return weekday; } public void setWeekday(@Weekday String weekday) { this.weekday = weekday; } @Retention(RetentionPolicy.RUNTIME) @StringDef({SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}) public @interface Weekday {} } 

优雅的解决scheme是使用一个枚举。

有效的解决scheme是编写一个自定义序列化程序,使用switch语句将星期string映射到int,反之亦然。

你也可以在你的序列化程序中使用reflection来访问StringDef注解中的值,但是这会更慢,更不可读。