【发布时间】:2015-06-30 09:08:43
【问题描述】:
将 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上使用枚举,就像here所说的那样)。
【问题讨论】:
-
编写一个自定义的 json 反序列化器,并在提供的字符串和所需的 int 值之间进行更改。
-
我当然知道自定义反序列化器和@JsonCreator。只是在寻找更“优雅”的解决方案。
-
为什么使用 Integer 而不是 int ?装箱使您的代码效率甚至低于枚举。
标签: android json jackson android-support-library