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 java.io.IOException;
20 import java.time.MonthDay;
21 import java.time.format.DateTimeFormatter;
22
23 import com.fasterxml.jackson.annotation.JsonFormat;
24
25 import com.fasterxml.jackson.core.JsonGenerator;
26 import com.fasterxml.jackson.core.JsonToken;
27 import com.fasterxml.jackson.core.type.WritableTypeId;
28
29 import com.fasterxml.jackson.databind.SerializerProvider;
30 import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
31
32 /**
33  * Serializer for Java 8 temporal {@link MonthDay}s.
34  *<p>
35  * NOTE: unlike many other date/time type serializers, this serializer will only
36  * use Array notation if explicitly instructed to do so with <code>JsonFormat</code>
37  * (either directly or through per-type defaults) and NOT with global defaults.
38  *
39  * @since 2.7.1
40  */

41 public class MonthDaySerializer extends JSR310FormattedSerializerBase<MonthDay>
42 {
43     private static final long serialVersionUID = 1L;
44
45     public static final MonthDaySerializer INSTANCE = new MonthDaySerializer();
46
47     private MonthDaySerializer() {
48         this(null);
49     }
50
51     public MonthDaySerializer(DateTimeFormatter formatter) {
52         super(MonthDay.class, formatter);
53     }
54
55     private MonthDaySerializer(MonthDaySerializer base, Boolean useTimestamp, DateTimeFormatter formatter) {
56         super(base, useTimestamp, formatter, null);
57     }
58
59     @Override
60     protected MonthDaySerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter, JsonFormat.Shape shape) {
61         return new MonthDaySerializer(this, useTimestamp, formatter);
62     }
63
64     @Override
65     public void serialize(MonthDay value, JsonGenerator g, SerializerProvider provider)
66         throws IOException
67     {
68         if (_useTimestampExplicitOnly(provider)) {
69             g.writeStartArray();
70             _serializeAsArrayContents(value, g, provider);
71             g.writeEndArray();
72         } else {
73             g.writeString((_formatter == null) ? value.toString() : value.format(_formatter));
74         }
75     }
76
77     @Override
78     public void serializeWithType(MonthDay value, JsonGenerator g,
79             SerializerProvider provider, TypeSerializer typeSer) throws IOException
80     {
81         WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
82                 typeSer.typeId(value, serializationShape(provider)));
83         // need to write out to avoid double-writing array markers
84         if (typeIdDef.valueShape == JsonToken.START_ARRAY) {
85             _serializeAsArrayContents(value, g, provider);
86         } else {
87             g.writeString((_formatter == null) ? value.toString() : value.format(_formatter));
88         }
89         typeSer.writeTypeSuffix(g, typeIdDef);
90     }
91     
92     protected void _serializeAsArrayContents(MonthDay value, JsonGenerator g,
93             SerializerProvider provider) throws IOException
94     {
95         g.writeNumber(value.getMonthValue());
96         g.writeNumber(value.getDayOfMonth());
97     }
98
99     @Override // since 2.9
100     protected JsonToken serializationShape(SerializerProvider provider) {
101         return _useTimestampExplicitOnly(provider) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING;
102     }
103 }
104