Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
try {
return Duration.parse(string);
} catch (DateTimeException e) {
return _rethrowDateTimeException(parser, context, e, string);
return _handleDateTimeException(context, e, string);
}
case JsonTokenId.ID_EMBEDDED_OBJECT:
// 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded
Expand All @@ -80,7 +80,7 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
case JsonTokenId.ID_START_ARRAY:
return _deserializeFromArray(parser, context);
}
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING,
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING,
JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public T deserialize(JsonParser parser, DeserializationContext context) throws I
return adjust.apply(value, this.getZone(context));
}
} catch (DateTimeException e) {
value = _rethrowDateTimeException(parser, context, e, string);
value = _handleDateTimeException(context, e, string);
}
return value;
}
Expand All @@ -222,7 +222,7 @@ public T deserialize(JsonParser parser, DeserializationContext context) throws I
case JsonTokenId.ID_START_ARRAY:
return _deserializeFromArray(parser, context);
}
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING,
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING,
JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.time.DateTimeException;
import java.time.format.DateTimeParseException;
import java.util.Arrays;

import com.fasterxml.jackson.core.JsonParser;
Expand All @@ -38,7 +37,7 @@
abstract class JSR310DeserializerBase<T> extends StdScalarDeserializer<T>
{
private static final long serialVersionUID = 1L;

protected JSR310DeserializerBase(Class<T> supportedType)
{
super(supportedType);
Expand Down Expand Up @@ -73,35 +72,55 @@ protected <BOGUS> BOGUS _reportWrongToken(JsonParser parser, DeserializationCont
handledType().getName());
}

protected <BOGUS> BOGUS _rethrowDateTimeException(JsonParser p, DeserializationContext context,
DateTimeException e0, String value) throws JsonMappingException
@SuppressWarnings("unchecked")
protected <R> R _handleDateTimeException(DeserializationContext context,
DateTimeException e0, String value) throws JsonMappingException
{
JsonMappingException e;
if (e0 instanceof DateTimeParseException) {
e = context.weirdStringException(value, handledType(), e0.getMessage());
try {
return (R) context.handleWeirdStringValue(handledType(), value,
"Failed to deserialize %s: (%s) %s",
handledType().getName(), e0.getClass().getName(), e0.getMessage());

} catch (JsonMappingException e) {
e.initCause(e0);
throw e;
}
if (e0 instanceof DateTimeException) {
String msg = e0.getMessage();
// 26-Mar-2017, tatu: Let's add some more logic to try to find likely format(ting)
// issues
if (msg.contains("invalid format")) {
e = context.weirdStringException(value, handledType(), e0.getMessage());
} catch (IOException e) {
if (null == e.getCause()) {
e.initCause(e0);
throw e;
}
throw JsonMappingException.fromUnexpectedIOE(e);
}
}

@SuppressWarnings("unchecked")
protected <R> R _handleUnexpectedToken(DeserializationContext context,
JsonParser parser, String message, Object... args) throws JsonMappingException {
try {
return (R) context.handleUnexpectedToken(handledType(), parser.getCurrentToken(),
parser, message, args);

} catch (JsonMappingException e) {
throw e;
} catch (IOException e) {
throw JsonMappingException.fromUnexpectedIOE(e);
}
return context.reportInputMismatch(handledType(),
"Failed to deserialize %s: (%s) %s",
handledType().getName(), e0.getClass().getName(), e0.getMessage());
}

@SuppressWarnings("unchecked")
protected <R> R _handleUnexpectedToken(DeserializationContext context,
JsonParser parser, JsonToken... expTypes) throws JsonMappingException {
return _handleUnexpectedToken(context, parser,
"Unexpected token (%s), expected one of %s for %s value",
parser.currentToken(),
Arrays.asList(expTypes),
handledType().getName());
}

/**
* Helper method used to peel off spurious wrappings of DateTimeException
*
* @param e DateTimeException to peel
*
*
* @return DateTimeException that does not have another DateTimeException as its cause.
*/
protected DateTimeException _peelDTE(DateTimeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Object deserialize(JsonParser parser, DeserializationContext context) thr
return ZoneOffset.of(string);
}
} catch (DateTimeException e) {
_rethrowDateTimeException(parser, context, e, string);
return _handleDateTimeException(context, e, string);
}
}
if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public LocalDate deserialize(JsonParser parser, DeserializationContext context)
}
return LocalDate.parse(string, format);
} catch (DateTimeException e) {
_rethrowDateTimeException(parser, context, e, string);
return _handleDateTimeException(context, e, string);
}
}
if (parser.isExpectedStartArrayToken()) {
Expand Down Expand Up @@ -118,7 +118,6 @@ public LocalDate deserialize(JsonParser parser, DeserializationContext context)
if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
return LocalDate.ofEpochDay(parser.getLongValue());
}
throw context.wrongTokenException(parser, handledType(), JsonToken.VALUE_STRING,
"Expected array or string.");
return _handleUnexpectedToken(context, parser, "Expected array or string.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public LocalDateTime deserialize(JsonParser parser, DeserializationContext conte

return LocalDateTime.parse(string, _formatter);
} catch (DateTimeException e) {
_rethrowDateTimeException(parser, context, e, string);
return _handleDateTimeException(context, e, string);
}
}
if (parser.isExpectedStartArrayToken()) {
Expand Down Expand Up @@ -138,7 +138,6 @@ public LocalDateTime deserialize(JsonParser parser, DeserializationContext conte
if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
_throwNoNumericTimestampNeedTimeZone(parser, context);
}
throw context.wrongTokenException(parser, handledType(), JsonToken.VALUE_STRING,
"Expected array or string.");
return _handleUnexpectedToken(context, parser, "Expected array or string.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public LocalTime deserialize(JsonParser parser, DeserializationContext context)
}
return LocalTime.parse(string, format);
} catch (DateTimeException e) {
_rethrowDateTimeException(parser, context, e, string);
return _handleDateTimeException(context, e, string);
}
}
if (parser.isExpectedStartArrayToken()) {
Expand Down Expand Up @@ -127,7 +127,6 @@ public LocalTime deserialize(JsonParser parser, DeserializationContext context)
if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
_throwNoNumericTimestampNeedTimeZone(parser, context);
}
throw context.wrongTokenException(parser, handledType(), JsonToken.START_ARRAY,
"Expected array or string.");
return _handleUnexpectedToken(context, parser, "Expected array or string.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public MonthDay deserialize(JsonParser parser, DeserializationContext context) t
}
return MonthDay.parse(string, _formatter);
} catch (DateTimeException e) {
_rethrowDateTimeException(parser, context, e, string);
return _handleDateTimeException(context, e, string);
}
}
if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
Expand All @@ -48,6 +48,6 @@ public MonthDay deserialize(JsonParser parser, DeserializationContext context) t
if (parser.hasToken(JsonToken.START_ARRAY)){
return _deserializeFromArray(parser, context);
}
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public OffsetTime deserialize(JsonParser parser, DeserializationContext context)
try {
return OffsetTime.parse(string, _formatter);
} catch (DateTimeException e) {
_rethrowDateTimeException(parser, context, e, string);
return _handleDateTimeException(context, e, string);
}
}
if (!parser.isExpectedStartArrayToken()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Year deserialize(JsonParser parser, DeserializationContext context) throw
}
return Year.parse(string, _formatter);
} catch (DateTimeException e) {
_rethrowDateTimeException(parser, context, e, string);
return _handleDateTimeException(context, e, string);
}
}
if (t == JsonToken.VALUE_NUMBER_INT) {
Expand All @@ -73,6 +73,6 @@ public Year deserialize(JsonParser parser, DeserializationContext context) throw
if (parser.hasToken(JsonToken.START_ARRAY)){
return _deserializeFromArray(parser, context);
}
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public YearMonth deserialize(JsonParser parser, DeserializationContext context)
try {
return YearMonth.parse(string, _formatter);
} catch (DateTimeException e) {
_rethrowDateTimeException(parser, context, e, string);
return _handleDateTimeException(context, e, string);
}
}
if (parser.isExpectedStartArrayToken()) {
Expand Down Expand Up @@ -102,6 +102,6 @@ public YearMonth deserialize(JsonParser parser, DeserializationContext context)
if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
return (YearMonth) parser.getEmbeddedObject();
}
return _reportWrongToken(parser, context, JsonToken.VALUE_STRING, JsonToken.START_ARRAY);
return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.START_ARRAY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected Duration deserialize(String key, DeserializationContext ctxt) throws I
try {
return Duration.parse(key);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, Duration.class, e, key);
return _handleDateTimeException(ctxt, Duration.class, e, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected Instant deserialize(String key, DeserializationContext ctxt) throws IO
try {
return DateTimeFormatter.ISO_INSTANT.parse(key, Instant::from);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, Instant.class, e, key);
return _handleDateTimeException(ctxt, Instant.class, e, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@ public final Object deserializeKey(String key, DeserializationContext ctxt)

protected abstract Object deserialize(String key, DeserializationContext ctxt)
throws IOException;

protected <T> T _rethrowDateTimeException(DeserializationContext ctxt,
Class<?> type, DateTimeException e0, String value) throws IOException

@SuppressWarnings("unchecked")
protected <T> T _handleDateTimeException(DeserializationContext ctxt,
Class<?> type, DateTimeException e0, String value) throws IOException
{
JsonMappingException e;
if (e0 instanceof DateTimeParseException) {
e = ctxt.weirdStringException(value, type, e0.getMessage());
try {
return (T) ctxt.handleWeirdStringValue(type, value,
"Failed to deserialize %s: (%s) %s",
type.getName(), e0.getClass().getName(), e0.getMessage());

} catch (JsonMappingException e) {
e.initCause(e0);
} else {
e = JsonMappingException.from(ctxt,
String.format("Failed to deserialize %s: (%s) %s",
type.getName(), e0.getClass().getName(), e0.getMessage()), e0);
throw e;
} catch (IOException e) {
if (null == e.getCause()) {
e.initCause(e0);
}
throw JsonMappingException.fromUnexpectedIOE(e);
}
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected LocalDate deserialize(String key, DeserializationContext ctxt) throws
try {
return LocalDate.parse(key, DateTimeFormatter.ISO_LOCAL_DATE);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, LocalDate.class, e, key);
return _handleDateTimeException(ctxt, LocalDate.class, e, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected LocalDateTime deserialize(String key, DeserializationContext ctxt) thr
try {
return LocalDateTime.parse(key, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, LocalDateTime.class, e, key);
return _handleDateTimeException(ctxt, LocalDateTime.class, e, key);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected LocalTime deserialize(String key, DeserializationContext ctxt) throws
try {
return LocalTime.parse(key, DateTimeFormatter.ISO_LOCAL_TIME);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, LocalTime.class, e, key);
return _handleDateTimeException(ctxt, LocalTime.class, e, key);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected MonthDay deserialize(String key, DeserializationContext ctxt) throws I
try {
return MonthDay.parse(key, PARSER);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, MonthDay.class, e, key);
return _handleDateTimeException(ctxt, MonthDay.class, e, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected OffsetDateTime deserialize(String key, DeserializationContext ctxt) th
try {
return OffsetDateTime.parse(key, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, OffsetDateTime.class, e, key);
return _handleDateTimeException(ctxt, OffsetDateTime.class, e, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected OffsetTime deserialize(String key, DeserializationContext ctxt) throws
try {
return OffsetTime.parse(key, DateTimeFormatter.ISO_OFFSET_TIME);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, OffsetTime.class, e, key);
return _handleDateTimeException(ctxt, OffsetTime.class, e, key);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected Period deserialize(String key, DeserializationContext ctxt) throws IOE
try {
return Period.parse(key);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, Period.class, e, key);
return _handleDateTimeException(ctxt, Period.class, e, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected Year deserialize(String key, DeserializationContext ctxt) throws IOExc
try {
return Year.parse(key, FORMATTER);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, Year.class, e, key);
return _handleDateTimeException(ctxt, Year.class, e, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected YearMonth deserialize(String key, DeserializationContext ctxt) throws
try {
return YearMonth.parse(key, FORMATTER);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, YearMonth.class, e, key);
return _handleDateTimeException(ctxt, YearMonth.class, e, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected Object deserialize(String key, DeserializationContext ctxt) throws IOE
try {
return ZoneId.of(key);
} catch (DateTimeException e) {
return _rethrowDateTimeException(ctxt, ZoneId.class, e, key);
return _handleDateTimeException(ctxt, ZoneId.class, e, key);
}
}
}
Loading