Skip to content

Commit e93ab34

Browse files
committed
Minor refactoring after #148 to streamline handling
1 parent 8b35502 commit e93ab34

File tree

5 files changed

+24
-65
lines changed

5 files changed

+24
-65
lines changed

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java

-3
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ protected InstantDeserializer<T> withDateFormat(DateTimeFormatter dtf) {
173173

174174
@Override
175175
protected InstantDeserializer<T> withLeniency(Boolean leniency) {
176-
if (_isLenient == !Boolean.FALSE.equals(leniency)) {
177-
return this;
178-
}
179176
return new InstantDeserializer<T>(this, _formatter, leniency);
180177
}
181178

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310DateTimeDeserializerBase.java

+17-54
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
import com.fasterxml.jackson.annotation.JsonFormat;
1010
import com.fasterxml.jackson.annotation.JsonFormat.Feature;
1111
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
12+
1213
import com.fasterxml.jackson.core.JsonParser;
13-
import com.fasterxml.jackson.core.JsonToken;
14+
1415
import com.fasterxml.jackson.databind.BeanProperty;
1516
import com.fasterxml.jackson.databind.DeserializationContext;
1617
import com.fasterxml.jackson.databind.JsonDeserializer;
1718
import com.fasterxml.jackson.databind.JsonMappingException;
1819
import com.fasterxml.jackson.databind.MapperFeature;
1920
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
20-
import com.fasterxml.jackson.databind.util.ClassUtil;
2121

2222
@SuppressWarnings("serial")
2323
public abstract class JSR310DateTimeDeserializerBase<T>
@@ -26,21 +26,6 @@ public abstract class JSR310DateTimeDeserializerBase<T>
2626
{
2727
protected final DateTimeFormatter _formatter;
2828

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-
4429
/**
4530
* Setting that indicates the {@Link JsonFormat.Shape} specified for this deserializer
4631
* as a {@link JsonFormat.Shape} annotation on property or class, or due to per-type
@@ -57,17 +42,15 @@ public abstract class JSR310DateTimeDeserializerBase<T>
5742
protected JSR310DateTimeDeserializerBase(Class<T> supportedType, DateTimeFormatter f) {
5843
super(supportedType);
5944
_formatter = f;
60-
_isLenient = true;
6145
_shape = null;
6246
}
6347

6448
/**
6549
* @since 2.11
6650
*/
6751
public JSR310DateTimeDeserializerBase(Class<T> supportedType, DateTimeFormatter f, Boolean leniency) {
68-
super(supportedType);
52+
super(supportedType, leniency);
6953
_formatter = f;
70-
_isLenient = !Boolean.FALSE.equals(leniency);
7154
_shape = null;
7255
}
7356

@@ -78,7 +61,6 @@ protected JSR310DateTimeDeserializerBase(JSR310DateTimeDeserializerBase<T> base,
7861
DateTimeFormatter f) {
7962
super(base);
8063
_formatter = f;
81-
_isLenient = base._isLenient;
8264
_shape = base._shape;
8365
}
8466

@@ -87,43 +69,48 @@ protected JSR310DateTimeDeserializerBase(JSR310DateTimeDeserializerBase<T> base,
8769
*/
8870
protected JSR310DateTimeDeserializerBase(JSR310DateTimeDeserializerBase<T> base,
8971
Boolean leniency) {
90-
super(base);
72+
super(base, leniency);
9173
_formatter = base._formatter;
92-
_isLenient = !Boolean.FALSE.equals(leniency);
9374
_shape = base._shape;
9475
}
9576

9677
/**
9778
* @since 2.11
9879
*/
9980
protected JSR310DateTimeDeserializerBase(JSR310DateTimeDeserializerBase<T> base,
100-
Shape shape) {
81+
Shape shape) {
10182
super(base);
10283
_formatter = base._formatter;
10384
_shape = shape;
104-
_isLenient = base._isLenient;
10585
}
10686

10787
protected abstract JSR310DateTimeDeserializerBase<T> withDateFormat(DateTimeFormatter dtf);
10888

10989
/**
11090
* @since 2.10
11191
*/
92+
@Override
11293
protected abstract JSR310DateTimeDeserializerBase<T> withLeniency(Boolean leniency);
11394

11495
/**
11596
* @since 2.11
11697
*/
11798
protected abstract JSR310DateTimeDeserializerBase<T> withShape(Shape shape);
11899

119-
120100
@Override
121101
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
122102
BeanProperty property) throws JsonMappingException
123103
{
124104
JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
125105
JSR310DateTimeDeserializerBase<?> deser = this;
126106
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+
}
127114
if (format.hasPattern()) {
128115
final String pattern = format.getPattern();
129116
final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
@@ -139,25 +126,19 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
139126
df = builder.toFormatter(locale);
140127
}
141128

142-
if (format.hasLenient() && !format.isLenient()) {
129+
// [#148]: allow strict parsing
130+
if (!deser.isLenient()) {
143131
df = df.withResolverStyle(ResolverStyle.STRICT);
144132
}
145133

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
147135
//a time zone picked up from JsonFormat annotation, otherwise serialization might not work
148136
if (format.hasTimeZone()) {
149137
df = df.withZone(format.getTimeZone().toZoneId());
150138
}
151139
deser = deser.withDateFormat(df);
152140
}
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
161142
//a shape picked up from JsonFormat annotation, to decide if the value is EpochSeconds
162143
JsonFormat.Shape shape = format.getShape();
163144
if (shape != null && shape != _shape) {
@@ -168,15 +149,6 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
168149
return deser;
169150
}
170151

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-
180152
private boolean acceptCaseInsensitiveValues(DeserializationContext ctxt, JsonFormat.Value format)
181153
{
182154
Boolean enabled = format.getFeature( Feature.ACCEPT_CASE_INSENSITIVE_VALUES);
@@ -193,13 +165,4 @@ protected void _throwNoNumericTimestampNeedTimeZone(JsonParser p, Deserializatio
193165
"raw timestamp (%d) not allowed for `%s`: need additional information such as an offset or time-zone (see class Javadocs)",
194166
p.getNumberValue(), handledType().getName());
195167
}
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-
}
205168
}

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310DeserializerBase.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected JSR310DeserializerBase(Class<T> supportedType,
7070

7171
protected JSR310DeserializerBase(JSR310DeserializerBase<T> base) {
7272
super(base);
73-
_isLenient = true;
73+
_isLenient = base._isLenient;
7474
}
7575

7676
protected JSR310DeserializerBase(JSR310DeserializerBase<T> base, Boolean leniency) {
@@ -155,7 +155,6 @@ protected <R> R _handleUnexpectedToken(DeserializationContext context,
155155
}
156156
}
157157

158-
@SuppressWarnings("unchecked")
159158
protected <R> R _handleUnexpectedToken(DeserializationContext context,
160159
JsonParser parser, JsonToken... expTypes) throws JsonMappingException {
161160
return _handleUnexpectedToken(context, parser,

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public void testCustomFormat() throws Exception
317317
@Test(expected = InvalidFormatException.class)
318318
public void testStrictCustomFormat() throws Exception
319319
{
320-
StrictWrapper w = MAPPER.readValue("{\"value\":\"2019-11-31\"}", StrictWrapper.class);
320+
/*StrictWrapper w =*/ MAPPER.readValue("{\"value\":\"2019-11-31\"}", StrictWrapper.class);
321321
}
322322

323323
/*
@@ -379,9 +379,9 @@ public void testDeserializationCaseInsensitiveDisabled_InvalidDate() throws Thro
379379
/*
380380
* Tests for issue 58 - NUMBER_INT should be specified when deserializing
381381
* LocalDate as EpochDays
382+
*
383+
/**********************************************************************
382384
*/
383-
/**********************************************************************
384-
*/
385385
@Test
386386
public void testLenientDeserializeFromNumberInt() throws Exception {
387387
ObjectMapper mapper = newMapper();
@@ -420,7 +420,7 @@ public void testStrictDeserializeFromString() throws Exception
420420
/**********************************************************************
421421
/* Helper methods
422422
/**********************************************************************
423-
*/
423+
*/
424424
private void expectFailure(ObjectReader reader, String json) throws Throwable {
425425
try {
426426
reader.readValue(aposToQuotes(json));

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalTimeDeserTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception {
269269
objectReader.readValue(valueFromEmptyStr);
270270
}
271271

272-
/*
272+
/*
273273
/**********************************************************************
274274
/* Strict JsonFormat tests
275275
/**********************************************************************
@@ -280,7 +280,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception {
280280
@Test(expected = InvalidFormatException.class)
281281
public void testStrictCustomFormatInvalidTime() throws Exception
282282
{
283-
StrictWrapper w = MAPPER.readValue("{\"value\":\"25:45\"}", StrictWrapper.class);
283+
/*StrictWrapper w =*/ MAPPER.readValue("{\"value\":\"25:45\"}", StrictWrapper.class);
284284
}
285285

286286
private void expectFailure(String aposJson) throws Throwable {

0 commit comments

Comments
 (0)