View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2014-3-1 下午1:48:05
4    */
5   package gboat2.base.bridge.util.json;
6   
7   import java.io.IOException;
8   import java.util.Date;
9   
10  import com.fasterxml.jackson.core.JsonParser;
11  import com.fasterxml.jackson.core.JsonProcessingException;
12  import com.fasterxml.jackson.databind.DeserializationContext;
13  import com.fasterxml.jackson.databind.JsonDeserializer;
14  
15  /**
16   * 自定义 jackson 反序列化实现类:将 Unix Timestamp 值(从 1970 年 1 月 1 日到现在的秒数)反序列化为
17   * {@link java.util.Date} 对象。
18   * 
19   * <pre>
20   * <code>
21   * public class Example {
22   *     <b>&#64;JsonDeserialize(using = UnixTimestampDeserializer.class)</b>
23   *     private Date createTime;
24   *     // get, set
25   * }
26   * </code>
27   * </pre>
28   * 
29   * @date 2014-3-1
30   * @author <a href="mailto:[email protected]">何明旺</a>
31   * @since 2.1.2
32   */
33  public class UnixTimestampDeserializer extends JsonDeserializer<Date> {
34  
35      @Override
36      public Date deserialize(JsonParser jp, DeserializationContext ctxt)
37              throws IOException, JsonProcessingException {
38          return new Date(Math.round(jp.getValueAsDouble() * 1000));
39      }
40  }