1 /*
2  * Copyright 2013 FasterXML.com
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the license for the specific language governing permissions and
14  * limitations under the license.
15  */

16
17 package com.fasterxml.jackson.datatype.jsr310.ser;
18
19 import com.fasterxml.jackson.annotation.JsonFormat;
20 import java.time.Instant;
21 import java.time.OffsetDateTime;
22 import java.time.ZonedDateTime;
23 import java.time.format.DateTimeFormatter;
24
25 /**
26  * Serializer for Java 8 temporal {@link Instant}s, {@link OffsetDateTime}, and {@link ZonedDateTime}s.
27  *
28  * @author Nick Williams
29  * @since 2.2
30  */

31 public class InstantSerializer extends InstantSerializerBase<Instant>
32 {
33     private static final long serialVersionUID = 1L;
34
35     public static final InstantSerializer INSTANCE = new InstantSerializer();
36
37     protected InstantSerializer() {
38         super(Instant.class, Instant::toEpochMilli, Instant::getEpochSecond, Instant::getNano,
39                 // null -> use 'value.toString()', default format
40                 null);
41     }
42
43     protected InstantSerializer(InstantSerializer base,
44             Boolean useTimestamp, DateTimeFormatter formatter) {
45         this(base, useTimestamp, null, formatter);
46     }
47
48     protected InstantSerializer(InstantSerializer base,
49             Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter) {
50         super(base, useTimestamp, useNanoseconds, formatter);
51     }
52
53     @Override
54     protected JSR310FormattedSerializerBase<Instant> withFormat(Boolean useTimestamp,
55             DateTimeFormatter formatter, JsonFormat.Shape shape) {
56         return new InstantSerializer(this, useTimestamp, formatter);
57     }
58
59     @Override
60     protected JSR310FormattedSerializerBase<?> withFeatures(Boolean writeZoneId, Boolean writeNanoseconds) {
61         return new InstantSerializer(this, _useTimestamp, writeNanoseconds, _formatter);
62     }
63 }
64