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

41 public class YearMonthSerializer extends JSR310FormattedSerializerBase<YearMonth>
42 {
43     private static final long serialVersionUID = 1L;
44
45     public static final YearMonthSerializer INSTANCE = new YearMonthSerializer();
46
47     private YearMonthSerializer() {
48         this(null);
49     }
50
51     public YearMonthSerializer(DateTimeFormatter formatter) {
52         super(YearMonth.class, formatter);
53     }
54
55     private YearMonthSerializer(YearMonthSerializer base, Boolean useTimestamp,
56             DateTimeFormatter formatter) {
57         super(base, useTimestamp, formatter, null);
58     }
59
60     @Override
61     protected YearMonthSerializer withFormat(Boolean useTimestamp, DateTimeFormatter formatter,
62             JsonFormat.Shape shape) {
63         return new YearMonthSerializer(this, useTimestamp, formatter);
64     }
65
66     @Override
67     public void serialize(YearMonth value, JsonGenerator g, SerializerProvider provider) throws IOException
68     {
69         if (useTimestamp(provider)) {
70             g.writeStartArray();
71             _serializeAsArrayContents(value, g, provider);
72             g.writeEndArray();
73             return;
74         }
75         g.writeString((_formatter == null) ? value.toString() : value.format(_formatter));
76     }
77
78     @Override
79     public void serializeWithType(YearMonth value, JsonGenerator g,
80             SerializerProvider provider, TypeSerializer typeSer) throws IOException
81     {
82         WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
83                 typeSer.typeId(value, serializationShape(provider)));
84         // need to write out to avoid double-writing array markers
85         if (typeIdDef.valueShape == JsonToken.START_ARRAY) {
86             _serializeAsArrayContents(value, g, provider);
87         } else {
88             g.writeString((_formatter == null) ? value.toString() : value.format(_formatter));
89         }
90         typeSer.writeTypeSuffix(g, typeIdDef);
91     }
92     
93     protected void _serializeAsArrayContents(YearMonth value, JsonGenerator g,
94             SerializerProvider provider) throws IOException
95     {
96         g.writeNumber(value.getYear());
97         g.writeNumber(value.getMonthValue());
98     }
99
100     @Override
101     protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException
102     {
103         SerializerProvider provider = visitor.getProvider();
104         boolean useTimestamp = (provider != null) && useTimestamp(provider);
105         if (useTimestamp) {
106             super._acceptTimestampVisitor(visitor, typeHint);
107         } else {
108             JsonStringFormatVisitor v2 = visitor.expectStringFormat(typeHint);
109             if (v2 != null) {
110                 v2.format(JsonValueFormat.DATE_TIME);
111             }
112         }
113     }
114
115     @Override // since 2.9
116     protected JsonToken serializationShape(SerializerProvider provider) {
117         return useTimestamp(provider) ? JsonToken.START_ARRAY : JsonToken.VALUE_STRING;
118     }
119 }
120