LocalDateTimeDeserializer.java
package tools.jackson.datatype.joda.deser;
import org.joda.time.IllegalFieldValueException;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormatter;
import tools.jackson.core.*;
import tools.jackson.core.io.NumberInput;
import tools.jackson.databind.DeserializationContext;
import tools.jackson.datatype.joda.cfg.FormatConfig;
import tools.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
public class LocalDateTimeDeserializer
extends JodaDateDeserializerBase<LocalDateTime>
{
public LocalDateTimeDeserializer() {
this(FormatConfig.DEFAULT_LOCAL_DATETIME_PARSER);
}
public LocalDateTimeDeserializer(JacksonJodaDateFormat format) {
super(LocalDateTime.class, format);
}
@Override
public JodaDateDeserializerBase<?> withFormat(JacksonJodaDateFormat format) {
return new LocalDateTimeDeserializer(format);
}
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt)
throws JacksonException
{
switch (p.currentTokenId()) {
case JsonTokenId.ID_NUMBER_INT:
return _fromTimestamp(ctxt, p.getLongValue());
case JsonTokenId.ID_STRING:
return _fromString(p, ctxt, p.getString());
case JsonTokenId.ID_START_OBJECT:
// 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML)
String str = ctxt.extractScalarFromObject(p, this, handledType());
// 17-May-2025, tatu: [databind#4656] need to check for `null`
if (str != null) {
return _fromString(p, ctxt, str);
}
// fall through
default:
}
if (p.isExpectedStartArrayToken()) {
return _fromArray(p, ctxt);
}
return (LocalDateTime) ctxt.handleUnexpectedToken(getValueType(ctxt), p.currentToken(), p,
"expected String, Number or JSON Array");
}
// @since 2.12
protected LocalDateTime _fromString(final JsonParser p, final DeserializationContext ctxt,
String value)
throws JacksonException
{
value = value.trim();
if (value.isEmpty()) {
return _fromEmptyString(p, ctxt, value);
}
// 14-Jul-2020: [datatype-joda#117] Should allow use of "Timestamp as String" for
// some textual formats
if (ctxt.isEnabled(StreamReadCapability.UNTYPED_SCALARS)
&& _isValidTimestampString(value)) {
return _fromTimestamp(ctxt, NumberInput.parseLong(value), value);
}
final DateTimeFormatter parser = _format.createParser(ctxt);
// Joda does not verify year range: out-of-range year may wrap around
return _parseWithJoda(ctxt, value, v -> {
final LocalDateTime dt = parser.parseLocalDateTime(v);
_verifyYear(dt.getYear());
return dt;
});
}
protected LocalDateTime _fromArray(final JsonParser p, final DeserializationContext ctxt)
throws JacksonException
{
// [yyyy,mm,dd,hh,MM,ss,ms?] (or Strings)
final String msg = "expected 6 or 7 int values for LocalDateTime";
int year = _nextArrayInt(p, ctxt, msg);
int month = _nextArrayInt(p, ctxt, msg);
int day = _nextArrayInt(p, ctxt, msg);
int hour = _nextArrayInt(p, ctxt, msg);
int minute = _nextArrayInt(p, ctxt, msg);
int second = _nextArrayInt(p, ctxt, msg);
// milliseconds are optional
int millisecond = 0;
if (p.nextToken() != JsonToken.END_ARRAY) {
millisecond = _parseIntPrimitive(p, ctxt);
if (p.nextToken() != JsonToken.END_ARRAY) {
throw ctxt.wrongTokenException(p, getValueType(ctxt), JsonToken.END_ARRAY,
"after LocalDateTime ints");
}
}
try {
_verifyYear(year);
return new LocalDateTime(year, month, day, hour, minute, second, millisecond);
} catch (IllegalFieldValueException e) {
return _handleInvalidArrayValue(ctxt, e);
}
}
protected LocalDateTime _fromTimestamp(DeserializationContext ctxt, long ts) {
return _fromTimestamp(ctxt, ts, null);
}
// @since 3.3
protected LocalDateTime _fromTimestamp(DeserializationContext ctxt, long ts, String text) {
// [datatype-joda#199]: zone offset addition for edge-of-long timestamp can
// overflow (or result fall outside year range); do not let raw Joda exception escape
return _fromTimestamp(ctxt, ts, text, LocalDateTime::new);
}
}