package com.fasterxml.jackson.datatype.jsr310.deser;
import java.io.IOException;
import java.time.DateTimeException;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
public class JSR310StringParsableDeserializer
extends JSR310DeserializerBase<Object>
implements ContextualDeserializer
{
private static final long serialVersionUID = 1L;
protected final static int TYPE_PERIOD = 1;
protected final static int TYPE_ZONE_ID = 2;
protected final static int TYPE_ZONE_OFFSET = 3;
public static final JsonDeserializer<Period> PERIOD =
createDeserializer(Period.class, TYPE_PERIOD);
public static final JsonDeserializer<ZoneId> ZONE_ID =
createDeserializer(ZoneId.class, TYPE_ZONE_ID);
public static final JsonDeserializer<ZoneOffset> ZONE_OFFSET =
createDeserializer(ZoneOffset.class, TYPE_ZONE_OFFSET);
protected final int _typeSelector;
@SuppressWarnings("unchecked")
protected JSR310StringParsableDeserializer(Class<?> supportedType, int typeSelector)
{
super((Class<Object>)supportedType);
_typeSelector = typeSelector;
}
protected JSR310StringParsableDeserializer(JSR310StringParsableDeserializer base, Boolean leniency) {
super(base, leniency);
_typeSelector = base._typeSelector;
}
@SuppressWarnings("unchecked")
protected static <T> JsonDeserializer<T> createDeserializer(Class<T> type, int typeId) {
return (JsonDeserializer<T>) new JSR310StringParsableDeserializer(type, typeId);
}
@Override
protected JSR310StringParsableDeserializer withLeniency(Boolean leniency) {
if (_isLenient == !Boolean.FALSE.equals(leniency)) {
return this;
}
return new JSR310StringParsableDeserializer(this, leniency);
}
@Override
public Object deserialize(JsonParser p, DeserializationContext context) throws IOException
{
String string = p.getValueAsString();
if (string != null) {
string = string.trim();
if (string.length() == 0) {
if (!isLenient()) {
return _failForNotLenient(p, context, JsonToken.VALUE_STRING);
}
return null;
}
try {
switch (_typeSelector) {
case TYPE_PERIOD:
return Period.parse(string);
case TYPE_ZONE_ID:
return ZoneId.of(string);
case TYPE_ZONE_OFFSET:
return ZoneOffset.of(string);
}
} catch (DateTimeException e) {
return _handleDateTimeException(context, e, string);
}
}
if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
return p.getEmbeddedObject();
}
if (p.hasToken(JsonToken.START_ARRAY)){
return _deserializeFromArray(p, context);
}
throw context.wrongTokenException(p, handledType(), JsonToken.VALUE_STRING, null);
}
@Override
public Object deserializeWithType(JsonParser parser, DeserializationContext context,
TypeDeserializer deserializer)
throws IOException
{
JsonToken t = parser.getCurrentToken();
if ((t != null) && t.isScalarValue()) {
return deserialize(parser, context);
}
return deserializer.deserializeTypedFromAny(parser, context);
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
JSR310StringParsableDeserializer deser = this;
if (format != null) {
if (format.hasLenient()) {
Boolean leniency = format.getLenient();
if (leniency != null) {
deser = this.withLeniency(leniency);
}
}
}
return deser;
}
}