Skip to content

Commit f4ea3e3

Browse files
authored
Merge pull request #66 from remal/2.9
Using DeserializationContext.handleWeirdStringValue for DateTimeException
2 parents 4091272 + c051ce1 commit f4ea3e3

28 files changed

+159
-66
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
7070
try {
7171
return Duration.parse(string);
7272
} catch (DateTimeException e) {
73-
return _rethrowDateTimeException(parser, context, e, string);
73+
return _handleDateTimeException(context, e, string);
7474
}
7575
case JsonTokenId.ID_EMBEDDED_OBJECT:
7676
// 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded
@@ -80,7 +80,7 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
8080
case JsonTokenId.ID_START_ARRAY:
8181
return _deserializeFromArray(parser, context);
8282
}
83-
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING,
83+
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING,
8484
JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT);
8585
}
8686
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public T deserialize(JsonParser parser, DeserializationContext context) throws I
209209
return adjust.apply(value, this.getZone(context));
210210
}
211211
} catch (DateTimeException e) {
212-
value = _rethrowDateTimeException(parser, context, e, string);
212+
value = _handleDateTimeException(context, e, string);
213213
}
214214
return value;
215215
}
@@ -222,7 +222,7 @@ public T deserialize(JsonParser parser, DeserializationContext context) throws I
222222
case JsonTokenId.ID_START_ARRAY:
223223
return _deserializeFromArray(parser, context);
224224
}
225-
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING,
225+
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING,
226226
JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT);
227227
}
228228

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

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.time.DateTimeException;
21-
import java.time.format.DateTimeParseException;
2221
import java.util.Arrays;
2322

2423
import com.fasterxml.jackson.core.JsonParser;
@@ -38,7 +37,7 @@
3837
abstract class JSR310DeserializerBase<T> extends StdScalarDeserializer<T>
3938
{
4039
private static final long serialVersionUID = 1L;
41-
40+
4241
protected JSR310DeserializerBase(Class<T> supportedType)
4342
{
4443
super(supportedType);
@@ -73,35 +72,55 @@ protected <BOGUS> BOGUS _reportWrongToken(JsonParser parser, DeserializationCont
7372
handledType().getName());
7473
}
7574

76-
protected <BOGUS> BOGUS _rethrowDateTimeException(JsonParser p, DeserializationContext context,
77-
DateTimeException e0, String value) throws JsonMappingException
75+
@SuppressWarnings("unchecked")
76+
protected <R> R _handleDateTimeException(DeserializationContext context,
77+
DateTimeException e0, String value) throws JsonMappingException
7878
{
79-
JsonMappingException e;
80-
if (e0 instanceof DateTimeParseException) {
81-
e = context.weirdStringException(value, handledType(), e0.getMessage());
79+
try {
80+
return (R) context.handleWeirdStringValue(handledType(), value,
81+
"Failed to deserialize %s: (%s) %s",
82+
handledType().getName(), e0.getClass().getName(), e0.getMessage());
83+
84+
} catch (JsonMappingException e) {
8285
e.initCause(e0);
8386
throw e;
84-
}
85-
if (e0 instanceof DateTimeException) {
86-
String msg = e0.getMessage();
87-
// 26-Mar-2017, tatu: Let's add some more logic to try to find likely format(ting)
88-
// issues
89-
if (msg.contains("invalid format")) {
90-
e = context.weirdStringException(value, handledType(), e0.getMessage());
87+
} catch (IOException e) {
88+
if (null == e.getCause()) {
9189
e.initCause(e0);
92-
throw e;
9390
}
91+
throw JsonMappingException.fromUnexpectedIOE(e);
92+
}
93+
}
94+
95+
@SuppressWarnings("unchecked")
96+
protected <R> R _handleUnexpectedToken(DeserializationContext context,
97+
JsonParser parser, String message, Object... args) throws JsonMappingException {
98+
try {
99+
return (R) context.handleUnexpectedToken(handledType(), parser.getCurrentToken(),
100+
parser, message, args);
101+
102+
} catch (JsonMappingException e) {
103+
throw e;
104+
} catch (IOException e) {
105+
throw JsonMappingException.fromUnexpectedIOE(e);
94106
}
95-
return context.reportInputMismatch(handledType(),
96-
"Failed to deserialize %s: (%s) %s",
97-
handledType().getName(), e0.getClass().getName(), e0.getMessage());
107+
}
108+
109+
@SuppressWarnings("unchecked")
110+
protected <R> R _handleUnexpectedToken(DeserializationContext context,
111+
JsonParser parser, JsonToken... expTypes) throws JsonMappingException {
112+
return _handleUnexpectedToken(context, parser,
113+
"Unexpected token (%s), expected one of %s for %s value",
114+
parser.currentToken(),
115+
Arrays.asList(expTypes),
116+
handledType().getName());
98117
}
99118

100119
/**
101120
* Helper method used to peel off spurious wrappings of DateTimeException
102121
*
103122
* @param e DateTimeException to peel
104-
*
123+
*
105124
* @return DateTimeException that does not have another DateTimeException as its cause.
106125
*/
107126
protected DateTimeException _peelDTE(DateTimeException e) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public Object deserialize(JsonParser parser, DeserializationContext context) thr
8989
return ZoneOffset.of(string);
9090
}
9191
} catch (DateTimeException e) {
92-
_rethrowDateTimeException(parser, context, e, string);
92+
return _handleDateTimeException(context, e, string);
9393
}
9494
}
9595
if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public LocalDate deserialize(JsonParser parser, DeserializationContext context)
8080
}
8181
return LocalDate.parse(string, format);
8282
} catch (DateTimeException e) {
83-
_rethrowDateTimeException(parser, context, e, string);
83+
return _handleDateTimeException(context, e, string);
8484
}
8585
}
8686
if (parser.isExpectedStartArrayToken()) {
@@ -118,7 +118,6 @@ public LocalDate deserialize(JsonParser parser, DeserializationContext context)
118118
if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
119119
return LocalDate.ofEpochDay(parser.getLongValue());
120120
}
121-
throw context.wrongTokenException(parser, handledType(), JsonToken.VALUE_STRING,
122-
"Expected array or string.");
121+
return _handleUnexpectedToken(context, parser, "Expected array or string.");
123122
}
124123
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public LocalDateTime deserialize(JsonParser parser, DeserializationContext conte
8181

8282
return LocalDateTime.parse(string, _formatter);
8383
} catch (DateTimeException e) {
84-
_rethrowDateTimeException(parser, context, e, string);
84+
return _handleDateTimeException(context, e, string);
8585
}
8686
}
8787
if (parser.isExpectedStartArrayToken()) {
@@ -138,7 +138,6 @@ public LocalDateTime deserialize(JsonParser parser, DeserializationContext conte
138138
if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
139139
_throwNoNumericTimestampNeedTimeZone(parser, context);
140140
}
141-
throw context.wrongTokenException(parser, handledType(), JsonToken.VALUE_STRING,
142-
"Expected array or string.");
141+
return _handleUnexpectedToken(context, parser, "Expected array or string.");
143142
}
144143
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public LocalTime deserialize(JsonParser parser, DeserializationContext context)
7171
}
7272
return LocalTime.parse(string, format);
7373
} catch (DateTimeException e) {
74-
_rethrowDateTimeException(parser, context, e, string);
74+
return _handleDateTimeException(context, e, string);
7575
}
7676
}
7777
if (parser.isExpectedStartArrayToken()) {
@@ -127,7 +127,6 @@ public LocalTime deserialize(JsonParser parser, DeserializationContext context)
127127
if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
128128
_throwNoNumericTimestampNeedTimeZone(parser, context);
129129
}
130-
throw context.wrongTokenException(parser, handledType(), JsonToken.START_ARRAY,
131-
"Expected array or string.");
130+
return _handleUnexpectedToken(context, parser, "Expected array or string.");
132131
}
133132
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public MonthDay deserialize(JsonParser parser, DeserializationContext context) t
3939
}
4040
return MonthDay.parse(string, _formatter);
4141
} catch (DateTimeException e) {
42-
_rethrowDateTimeException(parser, context, e, string);
42+
return _handleDateTimeException(context, e, string);
4343
}
4444
}
4545
if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
@@ -48,6 +48,6 @@ public MonthDay deserialize(JsonParser parser, DeserializationContext context) t
4848
if (parser.hasToken(JsonToken.START_ARRAY)){
4949
return _deserializeFromArray(parser, context);
5050
}
51-
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
51+
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
5252
}
5353
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public OffsetTime deserialize(JsonParser parser, DeserializationContext context)
6060
try {
6161
return OffsetTime.parse(string, _formatter);
6262
} catch (DateTimeException e) {
63-
_rethrowDateTimeException(parser, context, e, string);
63+
return _handleDateTimeException(context, e, string);
6464
}
6565
}
6666
if (!parser.isExpectedStartArrayToken()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Year deserialize(JsonParser parser, DeserializationContext context) throw
6161
}
6262
return Year.parse(string, _formatter);
6363
} catch (DateTimeException e) {
64-
_rethrowDateTimeException(parser, context, e, string);
64+
return _handleDateTimeException(context, e, string);
6565
}
6666
}
6767
if (t == JsonToken.VALUE_NUMBER_INT) {
@@ -73,6 +73,6 @@ public Year deserialize(JsonParser parser, DeserializationContext context) throw
7373
if (parser.hasToken(JsonToken.START_ARRAY)){
7474
return _deserializeFromArray(parser, context);
7575
}
76-
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
76+
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
7777
}
7878
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public YearMonth deserialize(JsonParser parser, DeserializationContext context)
6666
try {
6767
return YearMonth.parse(string, _formatter);
6868
} catch (DateTimeException e) {
69-
_rethrowDateTimeException(parser, context, e, string);
69+
return _handleDateTimeException(context, e, string);
7070
}
7171
}
7272
if (parser.isExpectedStartArrayToken()) {
@@ -102,6 +102,6 @@ public YearMonth deserialize(JsonParser parser, DeserializationContext context)
102102
if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
103103
return (YearMonth) parser.getEmbeddedObject();
104104
}
105-
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING, JsonToken.START_ARRAY);
105+
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.START_ARRAY);
106106
}
107107
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected Duration deserialize(String key, DeserializationContext ctxt) throws I
1919
try {
2020
return Duration.parse(key);
2121
} catch (DateTimeException e) {
22-
return _rethrowDateTimeException(ctxt, Duration.class, e, key);
22+
return _handleDateTimeException(ctxt, Duration.class, e, key);
2323
}
2424
}
2525
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected Instant deserialize(String key, DeserializationContext ctxt) throws IO
2020
try {
2121
return DateTimeFormatter.ISO_INSTANT.parse(key, Instant::from);
2222
} catch (DateTimeException e) {
23-
return _rethrowDateTimeException(ctxt, Instant.class, e, key);
23+
return _handleDateTimeException(ctxt, Instant.class, e, key);
2424
}
2525
}
2626
}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,24 @@ public final Object deserializeKey(String key, DeserializationContext ctxt)
2424

2525
protected abstract Object deserialize(String key, DeserializationContext ctxt)
2626
throws IOException;
27-
28-
protected <T> T _rethrowDateTimeException(DeserializationContext ctxt,
29-
Class<?> type, DateTimeException e0, String value) throws IOException
27+
28+
@SuppressWarnings("unchecked")
29+
protected <T> T _handleDateTimeException(DeserializationContext ctxt,
30+
Class<?> type, DateTimeException e0, String value) throws IOException
3031
{
31-
JsonMappingException e;
32-
if (e0 instanceof DateTimeParseException) {
33-
e = ctxt.weirdStringException(value, type, e0.getMessage());
32+
try {
33+
return (T) ctxt.handleWeirdStringValue(type, value,
34+
"Failed to deserialize %s: (%s) %s",
35+
type.getName(), e0.getClass().getName(), e0.getMessage());
36+
37+
} catch (JsonMappingException e) {
3438
e.initCause(e0);
35-
} else {
36-
e = JsonMappingException.from(ctxt,
37-
String.format("Failed to deserialize %s: (%s) %s",
38-
type.getName(), e0.getClass().getName(), e0.getMessage()), e0);
39+
throw e;
40+
} catch (IOException e) {
41+
if (null == e.getCause()) {
42+
e.initCause(e0);
43+
}
44+
throw JsonMappingException.fromUnexpectedIOE(e);
3945
}
40-
throw e;
4146
}
42-
}
47+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected LocalDate deserialize(String key, DeserializationContext ctxt) throws
2020
try {
2121
return LocalDate.parse(key, DateTimeFormatter.ISO_LOCAL_DATE);
2222
} catch (DateTimeException e) {
23-
return _rethrowDateTimeException(ctxt, LocalDate.class, e, key);
23+
return _handleDateTimeException(ctxt, LocalDate.class, e, key);
2424
}
2525
}
2626
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected LocalDateTime deserialize(String key, DeserializationContext ctxt) thr
2020
try {
2121
return LocalDateTime.parse(key, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
2222
} catch (DateTimeException e) {
23-
return _rethrowDateTimeException(ctxt, LocalDateTime.class, e, key);
23+
return _handleDateTimeException(ctxt, LocalDateTime.class, e, key);
2424
}
2525
}
2626

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected LocalTime deserialize(String key, DeserializationContext ctxt) throws
2020
try {
2121
return LocalTime.parse(key, DateTimeFormatter.ISO_LOCAL_TIME);
2222
} catch (DateTimeException e) {
23-
return _rethrowDateTimeException(ctxt, LocalTime.class, e, key);
23+
return _handleDateTimeException(ctxt, LocalTime.class, e, key);
2424
}
2525
}
2626

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected MonthDay deserialize(String key, DeserializationContext ctxt) throws I
3232
try {
3333
return MonthDay.parse(key, PARSER);
3434
} catch (DateTimeException e) {
35-
return _rethrowDateTimeException(ctxt, MonthDay.class, e, key);
35+
return _handleDateTimeException(ctxt, MonthDay.class, e, key);
3636
}
3737
}
3838
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected OffsetDateTime deserialize(String key, DeserializationContext ctxt) th
2020
try {
2121
return OffsetDateTime.parse(key, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
2222
} catch (DateTimeException e) {
23-
return _rethrowDateTimeException(ctxt, OffsetDateTime.class, e, key);
23+
return _handleDateTimeException(ctxt, OffsetDateTime.class, e, key);
2424
}
2525
}
2626
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected OffsetTime deserialize(String key, DeserializationContext ctxt) throws
2020
try {
2121
return OffsetTime.parse(key, DateTimeFormatter.ISO_OFFSET_TIME);
2222
} catch (DateTimeException e) {
23-
return _rethrowDateTimeException(ctxt, OffsetTime.class, e, key);
23+
return _handleDateTimeException(ctxt, OffsetTime.class, e, key);
2424
}
2525
}
2626

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected Period deserialize(String key, DeserializationContext ctxt) throws IOE
1919
try {
2020
return Period.parse(key);
2121
} catch (DateTimeException e) {
22-
return _rethrowDateTimeException(ctxt, Period.class, e, key);
22+
return _handleDateTimeException(ctxt, Period.class, e, key);
2323
}
2424
}
2525
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected Year deserialize(String key, DeserializationContext ctxt) throws IOExc
3131
try {
3232
return Year.parse(key, FORMATTER);
3333
} catch (DateTimeException e) {
34-
return _rethrowDateTimeException(ctxt, Year.class, e, key);
34+
return _handleDateTimeException(ctxt, Year.class, e, key);
3535
}
3636
}
3737
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected YearMonth deserialize(String key, DeserializationContext ctxt) throws
3232
try {
3333
return YearMonth.parse(key, FORMATTER);
3434
} catch (DateTimeException e) {
35-
return _rethrowDateTimeException(ctxt, YearMonth.class, e, key);
35+
return _handleDateTimeException(ctxt, YearMonth.class, e, key);
3636
}
3737
}
3838
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected Object deserialize(String key, DeserializationContext ctxt) throws IOE
1919
try {
2020
return ZoneId.of(key);
2121
} catch (DateTimeException e) {
22-
return _rethrowDateTimeException(ctxt, ZoneId.class, e, key);
22+
return _handleDateTimeException(ctxt, ZoneId.class, e, key);
2323
}
2424
}
2525
}

0 commit comments

Comments
 (0)