1 package com.fasterxml.jackson.databind.ser.std;
2
3 import java.io.IOException;
4 import java.text.DateFormat;
5 import java.util.Calendar;
6
7 import com.fasterxml.jackson.core.*;
8 import com.fasterxml.jackson.databind.SerializerProvider;
9 import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
10
11 /**
12  * Standard serializer for {@link java.util.Calendar}.
13  * As with other time/date types, is configurable to produce timestamps
14  * (standard Java 64-bit timestamp) or textual formats (usually ISO-8601).
15  */

16 @JacksonStdImpl
17 @SuppressWarnings("serial")
18 public class CalendarSerializer
19     extends DateTimeSerializerBase<Calendar>
20 {
21     public static final CalendarSerializer instance = new CalendarSerializer();
22
23     public CalendarSerializer() { this(nullnull); }
24
25     public CalendarSerializer(Boolean useTimestamp, DateFormat customFormat) {
26         super(Calendar.class, useTimestamp, customFormat);
27     }
28
29     @Override
30     public CalendarSerializer withFormat(Boolean timestamp, DateFormat customFormat) {
31         return new CalendarSerializer(timestamp, customFormat);
32     }
33
34     @Override
35     protected long _timestamp(Calendar value) {
36         return (value == null) ? 0L : value.getTimeInMillis();
37     }
38
39     @Override
40     public void serialize(Calendar value, JsonGenerator g, SerializerProvider provider) throws IOException
41     {
42         if (_asTimestamp(provider)) {
43             g.writeNumber(_timestamp(value));
44             return;
45         }
46         _serializeAsString(value.getTime(), g, provider);
47     }
48 }
49