Jackson ObjectMapper:日期序列化和反序列化的问题 [英] Jackson ObjectMapper : Issues with date serialization and deserialization
问题描述
我想在 Jackson Deserializer
中禁用宽容选项,以严格地反序列化Date字段。
I would like to disable lenient option in Jackson Deserializer
to deserialize Date fields strictly.
基本上,我希望下面的代码抛出异常,而不是
解析33-Aug- 2016
作为2016年9月2日
。
1。 Order.java
package com.test.date;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
public class Order {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MMM-yyyy")
private Date orderDate;
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public String getFormattedDate() {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
return "[ " + sdf.format(getOrderDate()) + " ]";
}
}
2。 TestJackson.java
package com.test.date;
import java.io.IOException;
import java.text.SimpleDateFormat;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestJackson {
public static void main(String[] args) throws JsonParseException,
JsonMappingException, IOException {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
sdf.setLenient(false);
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(sdf);
Order order = mapper.readValue("{\"orderDate\" : \"33-Aug-2016\"}",
Order.class);
System.out.println(order.getFormattedDate());
}
}
输出
[2016年9月2日]
[ 02-Sep-2016 ]
我可以实现自己的Deserializer类来执行此操作,但是我正在寻找一些基于注释或对象映射器设置的方法。
I can implement my own Deserializer class to do this, but I am looking for some annotation based or object mapper settings approach.
更新:
我决定采用自定义反序列化器实现,并发现了另一个问题,但是现在使用了序列化。更新后的代码如下所示:
I decided to go with custom Deserializer implementation and found another issue, but with Serialization now. The updated codes are for as shown below:
1。 Order.java
package com.test.date;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
public class Order {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MMM-yyyy")
@JsonDeserialize(using = DateDeserializer.class)
private Date orderDate;
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
@JsonIgnore
public String getFormattedDate() {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
return "[ " + sdf.format(getOrderDate()) + " ]";
}
}
使用自定义反序列化器,可验证日期完美地工作。但是,同一对象的序列化存在问题。请参见以下内容:
With custom deserializer, validation of dates work perfectly. However, serialization of the same object has issues. Please see below:
2。 TestJackson.java
package com.test.date;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestJackson {
public static void main(String[] args) throws JsonParseException,
JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
Order order = mapper.readValue("{\"orderDate\" : \"22-Aug-2016\"}",
Order.class);
System.out.println(mapper.writeValueAsString(order));
}
}
输出
{ orderDate: 2016年8月21日}
{"orderDate":"21-Aug-2016"}
这一天的差异从何而来?
如果我们使用自定义的 Deserializer
,提供 Serializer
的自定义实现?
Is it mandatory to provide custom implementation of Serializer
if we use a custom Deserializer
?
推荐答案
属性上的 @JsonFormat
注释将覆盖已注册的 SimpleDateFormat
使用 ObjectMapper
。摆脱 @JsonFormat
,Jackson将仅使用提供的 SimpleDateFormat
来解析日期,由于
The @JsonFormat
annotation on the property overrides the SimpleDateFormat
registered with the ObjectMapper
. Get rid of the @JsonFormat
and Jackson will use only the supplied SimpleDateFormat
to parse the date and fail because of the
sdf.setLenient(false);
据我所知, @JsonFormat
您没有可以设置控制宽恕的功能
。
As far as I know, @JsonFormat
does not have a Feature
you can set to control leniency.
这篇关于Jackson ObjectMapper:日期序列化和反序列化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!