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.deser;
18
19 import com.fasterxml.jackson.annotation.JsonFormat;
20 import com.fasterxml.jackson.core.JsonParser;
21 import com.fasterxml.jackson.core.JsonToken;
22 import com.fasterxml.jackson.databind.DeserializationContext;
23
24 import java.io.IOException;
25 import java.time.DateTimeException;
26 import java.time.Year;
27 import java.time.format.DateTimeFormatter;
28
29 /**
30  * Deserializer for Java 8 temporal {@link Year}s.
31  *
32  * @author Nick Williams
33  */

34 public class YearDeserializer extends JSR310DateTimeDeserializerBase<Year>
35 {
36     private static final long serialVersionUID = 1L;
37
38     public static final YearDeserializer INSTANCE = new YearDeserializer();
39
40     private YearDeserializer()
41     {
42         this(null);
43     }
44
45     public YearDeserializer(DateTimeFormatter formatter) {
46         super(Year.class, formatter);
47     }
48
49     @Override
50     protected YearDeserializer withDateFormat(DateTimeFormatter dtf) {
51         return new YearDeserializer(dtf);
52     }
53
54     // !!! TODO: lenient vs strict?
55     @Override
56     protected YearDeserializer withLeniency(Boolean leniency) {
57         return this;
58     }
59
60     @Override
61     protected YearDeserializer withShape(JsonFormat.Shape shape) { return this; }
62
63     @Override
64     public Year deserialize(JsonParser parser, DeserializationContext context) throws IOException
65     {
66         JsonToken t = parser.getCurrentToken();
67         if (t == JsonToken.VALUE_STRING) {
68             String string = parser.getValueAsString().trim();
69             try {
70                 if (_formatter == null) {
71                     return Year.parse(string);
72                 }
73                 return Year.parse(string, _formatter);
74             } catch (DateTimeException e) {
75                 return _handleDateTimeException(context, e, string);
76             }
77         }
78         if (t == JsonToken.VALUE_NUMBER_INT) {
79             return Year.of(parser.getIntValue());
80         }
81         if (t == JsonToken.VALUE_EMBEDDED_OBJECT) {
82             return (Year) parser.getEmbeddedObject();
83         }
84         if (parser.hasToken(JsonToken.START_ARRAY)){
85             return _deserializeFromArray(parser, context);
86         }
87         return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
88     }
89 }
90