差異處
這裏顯示兩個版本的差異處。
下次修改 Both sides next revision | |||
java:jackson:annotation:jsonserialize:convertdate [2016/03/10 23:48] tony 建立 |
java:jackson:annotation:jsonserialize:convertdate [2016/03/10 23:53] tony |
||
---|---|---|---|
行 72: | 行 72: | ||
} | } | ||
</code> | </code> | ||
+ | ==== Convert to Json ==== | ||
+ | 簡單寫一個測試案例,去驗證序列化後再反序列化的內容會一致: | ||
+ | <code java> | ||
+ | @Test | ||
+ | public void testDateWithAnnotation() throws Exception { | ||
+ | ObjectMapper mapper = new ObjectMapper(); | ||
+ | |||
+ | Date d = new Date(); | ||
+ | Event e = new Event(); | ||
+ | e.setDate(d); | ||
+ | |||
+ | String ret = mapper.writeValueAsString(e); | ||
+ | System.out.println(ret); | ||
+ | Event newEvent = mapper.readValue(ret, Event.class); | ||
+ | assertEquals(d.getTime(), newEvent.getDate().getTime()); | ||
+ | } | ||
+ | </code> | ||
+ | output: | ||
+ | <code> | ||
+ | {"date":"2016-03-10T15:40:11.869+0000","message":null} | ||
+ | </code> | ||
+ | |||
===== Reference ===== | ===== Reference ===== | ||
* [[http://stackoverflow.com/questions/5471379/ways-to-convert-unix-linux-time-to-windows-time|ways-to-convert-unix-linux-time-to-windows-time]] | * [[http://stackoverflow.com/questions/5471379/ways-to-convert-unix-linux-time-to-windows-time|ways-to-convert-unix-linux-time-to-windows-time]] |