package com.fasterxml.jackson.datatype.joda.deser;
import java.io.IOException;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.ReadableDateTime;
import org.joda.time.ReadableInstant;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
public class DateTimeDeserializer
extends JodaDateDeserializerBase<ReadableInstant>
{
private static final long serialVersionUID = 1L;
public DateTimeDeserializer(Class<?> cls, JacksonJodaDateFormat format) {
super(cls, format);
}
@SuppressWarnings("unchecked")
public static <T extends ReadableInstant> JsonDeserializer<T> forType(Class<T> cls)
{
return (JsonDeserializer<T>) new DateTimeDeserializer(cls,
FormatConfig.DEFAULT_DATETIME_PARSER);
}
@Override
public JodaDateDeserializerBase<?> withFormat(JacksonJodaDateFormat format) {
return new DateTimeDeserializer(_valueClass, format);
}
@Override
public ReadableInstant deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
JsonToken t = p.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT) {
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(ctxt.getTimeZone());
return new DateTime(p.getLongValue(), tz);
}
if (t == JsonToken.VALUE_STRING) {
String str = p.getText().trim();
if (str.length() == 0) {
return null;
}
int ix = str.indexOf('[');
if (ix > 0) {
int ix2 = str.lastIndexOf(']');
String tzId = (ix2 < ix)
? str.substring(ix+1)
: str.substring(ix+1, ix2);
DateTimeZone tz;
try {
tz = DateTimeZone.forID(tzId);
} catch (IllegalArgumentException e) {
ctxt.reportInputMismatch(handledType(), "Unknown DateTimeZone id '%s'", tzId);
tz = null;
}
str = str.substring(0, ix);
DateTime result = _format.createParser(ctxt)
.withZone(tz)
.parseDateTime(str)
;
if (_format.shouldAdjustToContextTimeZone(ctxt)) {
result = result.withZone(_format.getTimeZone());
}
return result;
}
return _format.createParser(ctxt).parseDateTime(str);
}
return _handleNotNumberOrString(p, ctxt);
}
}