1 package org.kuali.common.devops.vagrant.cloud.json;
2
3 import static org.kuali.common.devops.vagrant.cloud.json.Timestamps.newSimpleDateFormatBuilder;
4 import static org.kuali.common.util.base.Precondition.checkNotNull;
5
6 import java.io.IOException;
7 import java.text.ParseException;
8 import java.text.SimpleDateFormat;
9
10 import org.apache.commons.lang3.builder.Builder;
11
12 import com.fasterxml.jackson.core.JsonParser;
13 import com.fasterxml.jackson.core.JsonProcessingException;
14 import com.fasterxml.jackson.databind.DeserializationContext;
15 import com.fasterxml.jackson.databind.JsonDeserializer;
16
17 public class TimestampDeserializer extends JsonDeserializer<Long> {
18
19 public TimestampDeserializer() {
20 this(newSimpleDateFormatBuilder());
21 }
22
23 public TimestampDeserializer(Builder<SimpleDateFormat> builder) {
24 this.builder = checkNotNull(builder, "builder");
25 }
26
27 private final Builder<SimpleDateFormat> builder;
28
29 @Override
30 public Long deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
31 SimpleDateFormat sdf = builder.build();
32 try {
33 return sdf.parse(jp.getText()).getTime();
34 } catch (ParseException e) {
35 throw new IOException(e);
36 }
37 }
38
39 }