-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathYearDeserializer.java
More file actions
145 lines (132 loc) · 4.95 KB
/
Copy pathYearDeserializer.java
File metadata and controls
145 lines (132 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2013 FasterXML.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license for the specific language governing permissions and
* limitations under the license.
*/
package tools.jackson.databind.ext.javatime.deser;
import java.time.DateTimeException;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.annotation.JsonFormat;
import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonParser;
import tools.jackson.core.JsonToken;
import tools.jackson.core.StreamReadCapability;
import tools.jackson.core.io.NumberInput;
import tools.jackson.databind.DeserializationContext;
import tools.jackson.databind.ext.javatime.DateTimeParseException;
/**
* Deserializer for Java 8 temporal {@link Year}s.
*
* @author Nick Williams
*/
public class YearDeserializer extends JSR310DateTimeDeserializerBase<Year>
{
public static final YearDeserializer INSTANCE = new YearDeserializer();
/**
* NOTE: only {@code public} so that use via annotations (see [modules-java8#202])
* is possible
*/
public YearDeserializer() { // public since 2.12
this(null);
}
public YearDeserializer(DateTimeFormatter formatter) {
super(Year.class, formatter);
}
/**
* Since 2.12
*/
protected YearDeserializer(YearDeserializer base, Boolean leniency) {
super(base, leniency);
}
/**
* Since 2.16
*/
public YearDeserializer(YearDeserializer base,
Boolean leniency,
DateTimeFormatter formatter,
JsonFormat.Shape shape) {
super(base, leniency, formatter, shape);
}
@Override
protected YearDeserializer withDateFormat(DateTimeFormatter dtf) {
return new YearDeserializer(this, _isLenient, dtf, _shape);
}
@Override
protected YearDeserializer withLeniency(Boolean leniency) {
return new YearDeserializer(this, leniency);
}
@Override
public Year deserialize(JsonParser p, DeserializationContext ctxt) throws JacksonException
{
JsonToken t = p.currentToken();
if (t == JsonToken.VALUE_STRING) {
return _fromString(p, ctxt, p.getString());
}
if (t == JsonToken.VALUE_NUMBER_INT) {
return _fromNumber(ctxt, p.getIntValue());
}
if (t == JsonToken.VALUE_EMBEDDED_OBJECT) {
return (Year) p.getEmbeddedObject();
}
// 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML)
if (p.isExpectedStartObjectToken()) {
final String str = ctxt.extractScalarFromObject(p, this, handledType());
// 17-May-2025, tatu: [databind#4656] need to check for `null`
if (str != null) {
return _fromString(p, ctxt, str);
}
// fall through
} else if (p.isExpectedStartArrayToken()){
return _deserializeFromArray(p, ctxt);
}
return _handleUnexpectedToken(ctxt, p, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
}
protected Year _fromString(JsonParser p, DeserializationContext ctxt,
String string0)
throws JacksonException
{
String string = string0.trim();
if (string.length() == 0) {
// 22-Oct-2020, tatu: not sure if we should pass original (to distinguish
// b/w empty and blank); for now don't which will allow blanks to be
// handled like "regular" empty (same as pre-2.12)
return _fromEmptyString(p, ctxt, string);
}
// 30-Sep-2020: Should allow use of "Timestamp as String" for XML/CSV
if (ctxt.isEnabled(StreamReadCapability.UNTYPED_SCALARS)
&& _isValidTimestampString(string)) {
return _fromNumber(ctxt, NumberInput.parseInt(string));
}
try {
if (_formatter == null) {
return Year.parse(string);
}
return Year.parse(string, _formatter);
} catch (DateTimeException e) {
return _handleDateTimeFormatException(ctxt, e, _formatter, string);
}
}
protected Year _fromNumber(DeserializationContext ctxt, int value)
throws JacksonException
{
try {
return Year.of(value);
} catch (DateTimeException e) {
throw DateTimeParseException.from(ctxt.getParser(),
String.format("Failed to deserialize %s from number %d: %s",
Year.class.getName(), value, e.getMessage()),
String.valueOf(value), Year.class, e);
}
}
}