Ale*_*ith 5 java serialization json jackson
我需要为类'日期序列化设置格式.我有杰克逊的版本,没有@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);
}
Run Code Online (Sandbox Code Playgroud)
}
并用它:
@JsonSerialize(using = CDJsonDateSerializer.class)
private Date startDate;
Run Code Online (Sandbox Code Playgroud)
但是,我有另一个具有不同日期格式的字段,我不想为序列化创建另一个类.我可以将所有需要的格式(如常量)添加到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());
}
Run Code Online (Sandbox Code Playgroud)
我已经将@JacksonAnnotation添加到我创建的新注释JsonDateFormat中:
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonDateFormat {
String value();
}
Run Code Online (Sandbox Code Playgroud)
如果您不能使用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));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
{
"date1" : "02.12.2014",
"date2" : "2014-12-02"
}
Run Code Online (Sandbox Code Playgroud)
如果您有权访问,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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22685 次 |
| 最近记录: |