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
21 import com.fasterxml.jackson.core.JsonGenerator;
22 import com.fasterxml.jackson.core.JsonParser;
23 import com.fasterxml.jackson.core.JsonToken;
24
25 import com.fasterxml.jackson.databind.JavaType;
26 import com.fasterxml.jackson.databind.JsonMappingException;
27 import com.fasterxml.jackson.databind.SerializerProvider;
28 import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
29 import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonIntegerFormatVisitor;
30
31 import java.io.IOException;
32 import java.time.Year;
33 import java.time.format.DateTimeFormatter;
34
35 /**
36  * Serializer for Java 8 temporal {@link Year}s.
37  *
38  * @author Nick Williams
39  * @since 2.2
40  */

41 public class YearSerializer extends JSR310FormattedSerializerBase<Year>
42 {
43     private static final long serialVersionUID = 1L;
44
45     public static final YearSerializer INSTANCE = new YearSerializer();
46
47     protected YearSerializer() {
48         this(null);
49     }
50
51     public YearSerializer(DateTimeFormatter formatter) {
52         super(Year.class, formatter);
53     }
54
55     protected YearSerializer(YearSerializer base, Boolean useTimestamp, DateTimeFormatter formatter) {
56         super(base, useTimestamp, formatter, null);
57     }
58
59     @Override
60     protected YearSerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter, JsonFormat.Shape shape) {
61         return new YearSerializer(this, useTimestamp, formatter);
62     }
63
64     @Override
65     public void serialize(Year year, JsonGenerator generator, SerializerProvider provider) throws IOException
66     {
67         if (useTimestamp(provider)) {
68             generator.writeNumber(year.getValue());
69         } else {
70             String str = (_formatter == null) ? year.toString() : year.format(_formatter);
71             generator.writeString(str);
72         }
73     }
74
75     // Override because we have String/Int, NOT String/Array
76     @Override
77     protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
78             throws JsonMappingException
79     {
80         JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
81         if (v2 != null) {
82             v2.numberType(JsonParser.NumberType.LONG);
83         }
84     }
85
86     @Override // since 2.9
87     protected JsonToken serializationShape(SerializerProvider provider) {
88         return useTimestamp(provider) ? JsonToken.VALUE_NUMBER_INT : JsonToken.VALUE_STRING;
89     }
90 }
91