1 package com.fasterxml.jackson.databind.ser.std;
2
3 import java.io.IOException;
4 import java.text.DateFormat;
5 import java.util.*;
6
7 import com.fasterxml.jackson.core.*;
8 import com.fasterxml.jackson.databind.*;
9 import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
10
11 /**
12  * For efficiency, we will serialize Dates as longs, instead of
13  * potentially more readable Strings.
14  */

15 @JacksonStdImpl
16 @SuppressWarnings("serial")
17 public class DateSerializer
18     extends DateTimeSerializerBase<Date>
19 {
20     /**
21      * Default instance that is used when no contextual configuration
22      * is needed.
23      */

24     public static final DateSerializer instance = new DateSerializer();
25     
26     public DateSerializer() {
27         this(nullnull);
28     }
29         
30     public DateSerializer(Boolean useTimestamp, DateFormat customFormat) {
31         super(Date.class, useTimestamp, customFormat);
32     }
33
34     @Override
35     public DateSerializer withFormat(Boolean timestamp, DateFormat customFormat) {
36         return new DateSerializer(timestamp, customFormat);
37     }
38
39     @Override
40     protected long _timestamp(Date value) {
41         return (value == null) ? 0L : value.getTime();
42     }
43
44     @Override
45     public void serialize(Date value, JsonGenerator g, SerializerProvider provider) throws IOException
46     {
47         if (_asTimestamp(provider)) {
48             g.writeNumber(_timestamp(value));
49             return;
50         }
51         _serializeAsString(value, g, provider);
52     }
53 }
54