在从对象解析到json和逆向分析时,我有最糟糕的时间来处理纳秒。我创造了最简单的杰克逊的使用,我无法得到纳秒。下面是我的演示。fasterxml FAQ中有三个与我的案例相关的重要语句。前两个给了我做工作的诀窍,第三个告诉我不要使用 sql.Date ,但是 sql.timestamp 是 sql.Date 的“儿子”。
sql.Date
sql.timestamp
这些问题是:
首先, mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS,true) 和 @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSSSSS") 完全不起作用。我可以放置 false , true ,甚至不使用 mapper.configure ,使用或不使用 @JsonFormat ,结果都是一样的,
mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS,true)
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSSSSS")
false
true
mapper.configure
@JsonFormat
其次,如果我只尝试反序列化,我的意思是,在 user.json 中输入值 mapper.readValue ,然后只运行 mapper.readValue ,我将得到值 2015-01-07 11:44:22.452 ,所以我忽略了确切的值,因为Jacskon舍入了。
user.json
mapper.readValue
2015-01-07 11:44:22.452
来自 http://wiki.fasterxml.com/JacksonFAQDateHandling
1- Feature.WRITE_DATES_AS_TIMESTAMPS,false);它禁用时间戳(数字)的使用,而是使用ISO-8601 -compliant表示法,它的输出类似于"1970-01-01T00:00:00.000+0000“。
2-您可以通过传递一个java.text.DateFormat来配置格式化
3-请永远不要使用java.sql.Date!
// pojo
package com.jim.core; import java.sql.Timestamp; import com.fasterxml.jackson.annotation.JsonFormat; public class User { @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSSSSS") private Timestamp tsFirstTry; private Timestamp tsSecondTry; @Override public String toString() { return "User [tsFirstTry=" + tsFirstTry + ", tsSecondTry=" + tsSecondTry + "]"; //getters and setters }
//主修班
package com.jim.core; import java.io.File; import java.io.IOException; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class Main { public static void main(String[] args) { User user = new User(); user.setTsFirstTry(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date()))); user.setTsSecondTry(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date()))); System.out.println("firstTryValue = "+ Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date()))); System.out.println("secondTryValue = "+ Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date()))); ObjectMapper mapper = new ObjectMapper(); try { //mapper.setTimeZone(TimeZone.getTimeZone("UTC")); //mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS,true); //mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS,true); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS")); //Serialization - saving the created objects in the sequence of bytes. mapper.writeValue(new File("c:\\temp\\user.json"), user); System.out.println("Serialized Outcome = " + mapper.writeValueAsString(user)); //Deserialization - Retrieving those saved bytes into the form of original object. user = mapper.readValue(new File("c:\\temp\\user.json"), User.class); System.out.println("Deserialized Outcome = " + user); } catch (IOException e) { e.printStackTrace(); }
//pom (仅相关部分)
<properties> <java-version>1.6</java-version> <jackson.databind-version>2.2.3</jackson.databind-version> </properties> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.databind-version}</version>