[英]Jackson custom date serializer
我需要為類'日期序列化設置格式。 我有傑克遜的版本,沒有@JsonFormat。 這就是為什么我寫自定義類:
public class CDJsonDateSerializer extends JsonSerializer<Date>{
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String dateString = dateFormat.format(date);
jsonGenerator.writeString(dateString);
}
}
並用它:
@JsonSerialize(using = CDJsonDateSerializer.class)
private Date startDate;
但是,我有另一個具有不同日期格式的字段,我不想為序列化創建另一個類。 我可以將所有需要的格式(如常量)添加到CDJsonDateSerializer類中,並使用注釋@JsonSerialize
設置所需的格式嗎? 像這樣的東西:
@JsonSerialize(using = CDJsonDateSerializer.class, CDJsonDateSerializer.FIRST_FORMAT)
。
以下回答:
它經過一些修正后才有效。 我改變了在createContextual方法中獲取注釋的方法:
@Override
public JsonSerializer createContextual(SerializationConfig serializationConfig, BeanProperty beanProperty) {
return new CustomDateSerializer(beanProperty.getAnnotation(JsonDateFormat.class).value());
}
我已經將@JacksonAnnotation添加到我創建的新注釋JsonDateFormat中:
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonDateFormat {
String value();
}
如果您不能使用Jackson 2中的@JsonFormat ,我建議您引入自己的自定義注釋,其中包含格式字段。 然后,您的serailizer應實現ContextualSerializer
接口以訪問注釋值。
以下是傑克遜1.9.X的一個例子:
public class JacksonDateFormat {
@Retention(RetentionPolicy.RUNTIME)
public static @interface MyJsonFormat {
String value();
}
public static class Bean {
@MyJsonFormat("dd.MM.yyyy") @JsonSerialize(using = MyDateSerializer.class)
public final Date date1;
@MyJsonFormat("yyyy-MM-dd") @JsonSerialize(using = MyDateSerializer.class)
public final Date date2;
public Bean(final Date date1, final Date date2) {
this.date1 = date1;
this.date2 = date2;
}
}
public static class MyDateSerializer extends JsonSerializer<Date>
implements ContextualSerializer {
private final String format;
private MyDateSerializer(final String format) {this.format = format;}
public MyDateSerializer() {this.format = null;}
@Override
public void serialize(
final Date value, final JsonGenerator jgen, final SerializerProvider provider)
throws IOException {
jgen.writeString(new SimpleDateFormat(format).format(value));
}
@Override
public JsonSerializer createContextual(
final SerializationConfig serializationConfig, final BeanProperty beanProperty)
throws JsonMappingException {
final AnnotatedElement annotated = beanProperty.getMember().getAnnotated();
return new MyDateSerializer(annotated.getAnnotation(MyJsonFormat.class).value());
}
}
public static void main(String[] args) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
final Bean value = new Bean(new Date(), new Date());
System.out.println(mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(value));
}
}
輸出:
{
"date1" : "02.12.2014",
"date2" : "2014-12-02"
}
如果您可以訪問ObjectMapper
,則可以為所有Date
類型注冊自定義序列化程序,因此您需要更長時間來放置@JsonSerialize
注釋。
這是一個例子:
final ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule("", Version.unknownVersion());
module.addSerializer(Date.class, new MyDateSerializer(null));
mapper.registerModule(module);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.