16
16
17
17
package com .fasterxml .jackson .datatype .jsr310 .deser ;
18
18
19
+ import com .fasterxml .jackson .annotation .JsonFormat ;
19
20
import com .fasterxml .jackson .core .JsonParser ;
20
21
import com .fasterxml .jackson .core .JsonToken ;
21
22
import com .fasterxml .jackson .core .JsonTokenId ;
23
+ import com .fasterxml .jackson .databind .BeanProperty ;
22
24
import com .fasterxml .jackson .databind .DeserializationContext ;
23
25
import com .fasterxml .jackson .databind .DeserializationFeature ;
26
+ import com .fasterxml .jackson .databind .JsonDeserializer ;
27
+ import com .fasterxml .jackson .databind .JsonMappingException ;
28
+ import com .fasterxml .jackson .databind .deser .ContextualDeserializer ;
24
29
import com .fasterxml .jackson .datatype .jsr310 .DecimalUtils ;
25
30
26
31
import java .io .IOException ;
27
32
import java .math .BigDecimal ;
28
33
import java .time .DateTimeException ;
29
34
import java .time .Duration ;
30
35
36
+
31
37
/**
32
38
* Deserializer for Java 8 temporal {@link Duration}s.
33
39
*
34
40
* @author Nick Williams
35
41
* @since 2.2.0
36
42
*/
37
- public class DurationDeserializer extends JSR310DeserializerBase <Duration >
43
+ public class DurationDeserializer extends JSR310DeserializerBase <Duration > implements ContextualDeserializer
38
44
{
39
45
private static final long serialVersionUID = 1L ;
40
46
@@ -45,6 +51,18 @@ private DurationDeserializer()
45
51
super (Duration .class );
46
52
}
47
53
54
+ /**
55
+ * Since 2.11
56
+ */
57
+ protected DurationDeserializer (DurationDeserializer base , Boolean leniency ) {
58
+ super (base , leniency );
59
+ }
60
+
61
+ @ Override
62
+ protected DurationDeserializer withLeniency (Boolean leniency ) {
63
+ return new DurationDeserializer (this , leniency );
64
+ }
65
+
48
66
@ Override
49
67
public Duration deserialize (JsonParser parser , DeserializationContext context ) throws IOException
50
68
{
@@ -63,6 +81,9 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
63
81
case JsonTokenId .ID_STRING :
64
82
String string = parser .getText ().trim ();
65
83
if (string .length () == 0 ) {
84
+ if (!isLenient ()) {
85
+ return _failForNotLenient (parser , context , JsonToken .VALUE_STRING );
86
+ }
66
87
return null ;
67
88
}
68
89
try {
@@ -81,4 +102,21 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
81
102
return _handleUnexpectedToken (context , parser , JsonToken .VALUE_STRING ,
82
103
JsonToken .VALUE_NUMBER_INT , JsonToken .VALUE_NUMBER_FLOAT );
83
104
}
105
+
106
+ @ Override
107
+ public JsonDeserializer <?> createContextual (DeserializationContext ctxt ,
108
+ BeanProperty property ) throws JsonMappingException
109
+ {
110
+ JsonFormat .Value format = findFormatOverrides (ctxt , property , handledType ());
111
+ DurationDeserializer deser = this ;
112
+ if (format != null ) {
113
+ if (format .hasLenient ()) {
114
+ Boolean leniency = format .getLenient ();
115
+ if (leniency != null ) {
116
+ deser = deser .withLeniency (leniency );
117
+ }
118
+ }
119
+ }
120
+ return deser ;
121
+ }
84
122
}
0 commit comments