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
*/
36
- public class DurationDeserializer extends JSR310DeserializerBase <Duration >
42
+ public class DurationDeserializer extends JSR310DeserializerBase <Duration > implements ContextualDeserializer
37
43
{
38
44
public static final DurationDeserializer INSTANCE = new DurationDeserializer ();
39
45
@@ -42,6 +48,18 @@ private DurationDeserializer()
42
48
super (Duration .class );
43
49
}
44
50
51
+ /**
52
+ * Since 2.11
53
+ */
54
+ protected DurationDeserializer (DurationDeserializer base , Boolean leniency ) {
55
+ super (base , leniency );
56
+ }
57
+
58
+ @ Override
59
+ protected DurationDeserializer withLeniency (Boolean leniency ) {
60
+ return new DurationDeserializer (this , leniency );
61
+ }
62
+
45
63
@ Override
46
64
public Duration deserialize (JsonParser parser , DeserializationContext context ) throws IOException
47
65
{
@@ -60,6 +78,9 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
60
78
case JsonTokenId .ID_STRING :
61
79
String string = parser .getText ().trim ();
62
80
if (string .length () == 0 ) {
81
+ if (!isLenient ()) {
82
+ return _failForNotLenient (parser , context , JsonToken .VALUE_STRING );
83
+ }
63
84
return null ;
64
85
}
65
86
try {
@@ -78,4 +99,21 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
78
99
return _handleUnexpectedToken (context , parser , JsonToken .VALUE_STRING ,
79
100
JsonToken .VALUE_NUMBER_INT , JsonToken .VALUE_NUMBER_FLOAT );
80
101
}
102
+
103
+ @ Override
104
+ public JsonDeserializer <?> createContextual (DeserializationContext ctxt ,
105
+ BeanProperty property ) throws JsonMappingException
106
+ {
107
+ JsonFormat .Value format = findFormatOverrides (ctxt , property , handledType ());
108
+ DurationDeserializer deser = this ;
109
+ if (format != null ) {
110
+ if (format .hasLenient ()) {
111
+ Boolean leniency = format .getLenient ();
112
+ if (leniency != null ) {
113
+ deser = deser .withLeniency (leniency );
114
+ }
115
+ }
116
+ }
117
+ return deser ;
118
+ }
81
119
}
0 commit comments