9
9
import com .fasterxml .jackson .annotation .JsonFormat ;
10
10
import com .fasterxml .jackson .annotation .JsonFormat .Feature ;
11
11
import com .fasterxml .jackson .annotation .JsonFormat .Shape ;
12
+
12
13
import com .fasterxml .jackson .core .JsonParser ;
13
- import com . fasterxml . jackson . core . JsonToken ;
14
+
14
15
import com .fasterxml .jackson .databind .BeanProperty ;
15
16
import com .fasterxml .jackson .databind .DeserializationContext ;
16
17
import com .fasterxml .jackson .databind .JsonDeserializer ;
17
18
import com .fasterxml .jackson .databind .JsonMappingException ;
18
19
import com .fasterxml .jackson .databind .MapperFeature ;
19
20
import com .fasterxml .jackson .databind .deser .ContextualDeserializer ;
20
- import com .fasterxml .jackson .databind .util .ClassUtil ;
21
21
22
22
@ SuppressWarnings ("serial" )
23
23
public abstract class JSR310DateTimeDeserializerBase <T >
@@ -26,21 +26,6 @@ public abstract class JSR310DateTimeDeserializerBase<T>
26
26
{
27
27
protected final DateTimeFormatter _formatter ;
28
28
29
- /**
30
- * Flag that indicates what leniency setting is enabled for this deserializer (either
31
- * due {@link JsonFormat} annotation on property or class, or due to per-type
32
- * "config override", or from global settings): leniency/strictness has effect
33
- * on accepting some non-default input value representations (such as integer values
34
- * for dates).
35
- *<p>
36
- * Note that global default setting is for leniency to be enabled, for Jackson 2.x,
37
- * and has to be explicitly change to force strict handling: this is to keep backwards
38
- * compatibility with earlier versions.
39
- *
40
- * @since 2.10
41
- */
42
- protected final boolean _isLenient ;
43
-
44
29
/**
45
30
* Setting that indicates the {@Link JsonFormat.Shape} specified for this deserializer
46
31
* as a {@link JsonFormat.Shape} annotation on property or class, or due to per-type
@@ -57,17 +42,15 @@ public abstract class JSR310DateTimeDeserializerBase<T>
57
42
protected JSR310DateTimeDeserializerBase (Class <T > supportedType , DateTimeFormatter f ) {
58
43
super (supportedType );
59
44
_formatter = f ;
60
- _isLenient = true ;
61
45
_shape = null ;
62
46
}
63
47
64
48
/**
65
49
* @since 2.11
66
50
*/
67
51
public JSR310DateTimeDeserializerBase (Class <T > supportedType , DateTimeFormatter f , Boolean leniency ) {
68
- super (supportedType );
52
+ super (supportedType , leniency );
69
53
_formatter = f ;
70
- _isLenient = !Boolean .FALSE .equals (leniency );
71
54
_shape = null ;
72
55
}
73
56
@@ -78,7 +61,6 @@ protected JSR310DateTimeDeserializerBase(JSR310DateTimeDeserializerBase<T> base,
78
61
DateTimeFormatter f ) {
79
62
super (base );
80
63
_formatter = f ;
81
- _isLenient = base ._isLenient ;
82
64
_shape = base ._shape ;
83
65
}
84
66
@@ -87,43 +69,48 @@ protected JSR310DateTimeDeserializerBase(JSR310DateTimeDeserializerBase<T> base,
87
69
*/
88
70
protected JSR310DateTimeDeserializerBase (JSR310DateTimeDeserializerBase <T > base ,
89
71
Boolean leniency ) {
90
- super (base );
72
+ super (base , leniency );
91
73
_formatter = base ._formatter ;
92
- _isLenient = !Boolean .FALSE .equals (leniency );
93
74
_shape = base ._shape ;
94
75
}
95
76
96
77
/**
97
78
* @since 2.11
98
79
*/
99
80
protected JSR310DateTimeDeserializerBase (JSR310DateTimeDeserializerBase <T > base ,
100
- Shape shape ) {
81
+ Shape shape ) {
101
82
super (base );
102
83
_formatter = base ._formatter ;
103
84
_shape = shape ;
104
- _isLenient = base ._isLenient ;
105
85
}
106
86
107
87
protected abstract JSR310DateTimeDeserializerBase <T > withDateFormat (DateTimeFormatter dtf );
108
88
109
89
/**
110
90
* @since 2.10
111
91
*/
92
+ @ Override
112
93
protected abstract JSR310DateTimeDeserializerBase <T > withLeniency (Boolean leniency );
113
94
114
95
/**
115
96
* @since 2.11
116
97
*/
117
98
protected abstract JSR310DateTimeDeserializerBase <T > withShape (Shape shape );
118
99
119
-
120
100
@ Override
121
101
public JsonDeserializer <?> createContextual (DeserializationContext ctxt ,
122
102
BeanProperty property ) throws JsonMappingException
123
103
{
124
104
JsonFormat .Value format = findFormatOverrides (ctxt , property , handledType ());
125
105
JSR310DateTimeDeserializerBase <?> deser = this ;
126
106
if (format != null ) {
107
+ // 17-Aug-2019, tatu: For 2.10 let's start considering leniency/strictness too
108
+ if (format .hasLenient ()) {
109
+ Boolean leniency = format .getLenient ();
110
+ if (leniency != null ) {
111
+ deser = deser .withLeniency (leniency );
112
+ }
113
+ }
127
114
if (format .hasPattern ()) {
128
115
final String pattern = format .getPattern ();
129
116
final Locale locale = format .hasLocale () ? format .getLocale () : ctxt .getLocale ();
@@ -139,25 +126,19 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
139
126
df = builder .toFormatter (locale );
140
127
}
141
128
142
- if (format .hasLenient () && !format .isLenient ()) {
129
+ // [#148]: allow strict parsing
130
+ if (!deser .isLenient ()) {
143
131
df = df .withResolverStyle (ResolverStyle .STRICT );
144
132
}
145
133
146
- //Issue #69: For instant serializers/deserializers we need to configure the formatter with
134
+ // [ #69] : For instant serializers/deserializers we need to configure the formatter with
147
135
//a time zone picked up from JsonFormat annotation, otherwise serialization might not work
148
136
if (format .hasTimeZone ()) {
149
137
df = df .withZone (format .getTimeZone ().toZoneId ());
150
138
}
151
139
deser = deser .withDateFormat (df );
152
140
}
153
- // 17-Aug-2019, tatu: For 2.10 let's start considering leniency/strictness too
154
- if (format .hasLenient ()) {
155
- Boolean leniency = format .getLenient ();
156
- if (leniency != null ) {
157
- deser = deser .withLeniency (leniency );
158
- }
159
- }
160
- //Issue #58: For LocalDate deserializers we need to configure the formatter with
141
+ // [#58]: For LocalDate deserializers we need to configure the formatter with
161
142
//a shape picked up from JsonFormat annotation, to decide if the value is EpochSeconds
162
143
JsonFormat .Shape shape = format .getShape ();
163
144
if (shape != null && shape != _shape ) {
@@ -168,15 +149,6 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
168
149
return deser ;
169
150
}
170
151
171
- /**
172
- * @return {@code true} if lenient handling is enabled; {code false} if not (strict mode)
173
- *
174
- * @since 2.10
175
- */
176
- protected boolean isLenient () {
177
- return _isLenient ;
178
- }
179
-
180
152
private boolean acceptCaseInsensitiveValues (DeserializationContext ctxt , JsonFormat .Value format )
181
153
{
182
154
Boolean enabled = format .getFeature ( Feature .ACCEPT_CASE_INSENSITIVE_VALUES );
@@ -193,13 +165,4 @@ protected void _throwNoNumericTimestampNeedTimeZone(JsonParser p, Deserializatio
193
165
"raw timestamp (%d) not allowed for `%s`: need additional information such as an offset or time-zone (see class Javadocs)" ,
194
166
p .getNumberValue (), handledType ().getName ());
195
167
}
196
-
197
- @ SuppressWarnings ("unchecked" )
198
- protected T _failForNotLenient (JsonParser p , DeserializationContext ctxt ,
199
- JsonToken expToken ) throws IOException
200
- {
201
- return (T ) ctxt .handleUnexpectedToken (handledType (), expToken , p ,
202
- "Cannot deserialize instance of %s out of %s token: not allowed because 'strict' mode set for property or type (enable 'lenient' handling to allow)" ,
203
- ClassUtil .nameOf (handledType ()), p .currentToken ());
204
- }
205
168
}
0 commit comments