package com.facebook.presto.jdbc.internal.jackson.datatype.guava.deser;
import java.io.IOException;
import com.facebook.presto.jdbc.internal.jackson.core.*;
import com.facebook.presto.jdbc.internal.jackson.databind.*;
import com.facebook.presto.jdbc.internal.jackson.databind.deser.ContextualDeserializer;
import com.facebook.presto.jdbc.internal.jackson.databind.jsontype.TypeDeserializer;
import com.facebook.presto.jdbc.internal.jackson.databind.type.MapType;
public abstract class GuavaMapDeserializer<T>
extends JsonDeserializer<T>
implements ContextualDeserializer
{
protected final MapType _mapType;
/**
* Key deserializer used, if not null. If null, String from JSON
* content is used as is.
*/
protected KeyDeserializer _keyDeserializer;
/**
* Value deserializer.
*/
protected JsonDeserializer<?> _valueDeserializer;
/**
* If value instances have polymorphic type information, this
* is the type deserializer that can handle it
*/
protected final TypeDeserializer _typeDeserializerForValue;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
protected GuavaMapDeserializer(MapType type, KeyDeserializer keyDeser,
TypeDeserializer typeDeser, JsonDeserializer<?> deser)
{
_mapType = type;
_keyDeserializer = keyDeser;
_typeDeserializerForValue = typeDeser;
_valueDeserializer = deser;
}
/**
* Overridable fluent factory method used for creating contextual
* instances.
*/
public abstract GuavaMapDeserializer<T> withResolved(KeyDeserializer keyDeser,
TypeDeserializer typeDeser, JsonDeserializer<?> valueDeser);
/*
/**********************************************************
/* Validation, post-processing
/**********************************************************
*/
/**
* Method called to finalize setup of this deserializer,
* after deserializer itself has been registered. This
* is needed to handle recursive and transitive dependencies.
*/
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
KeyDeserializer keyDeser = _keyDeserializer;
JsonDeserializer<?> deser = _valueDeserializer;
TypeDeserializer typeDeser = _typeDeserializerForValue;
// Do we need any contextualization?
if ((keyDeser != null) && (deser != null) && (typeDeser == null)) { // nope
return this;
}
if (keyDeser == null) {
keyDeser = ctxt.findKeyDeserializer(_mapType.getKeyType(), property);
}
if (deser == null) {
deser = ctxt.findContextualValueDeserializer(_mapType.getContentType(), property);
}
if (typeDeser != null) {
typeDeser = typeDeser.forProperty(property);
}
return withResolved(keyDeser, typeDeser, deser);
}
/*
/**********************************************************
/* Deserialization interface
/**********************************************************
*/
/**
* Base implementation that does not assume specific type
* inclusion mechanism. Sub-classes are expected to override
* this method if they are to handle type information.
*/
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
throws IOException, JsonProcessingException
{
return typeDeserializer.deserializeTypedFromArray(jp, ctxt);
}
@Override
public T deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
// Ok: must point to START_OBJECT or FIELD_NAME
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.START_OBJECT) { // If START_OBJECT, move to next; may also be END_OBJECT
t = jp.nextToken();
if (t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
throw ctxt.mappingException(_mapType.getRawClass());
}
} else if (t != JsonToken.FIELD_NAME) {
throw ctxt.mappingException(_mapType.getRawClass());
}
return _deserializeEntries(jp, ctxt);
}
/*
/**********************************************************************
/* Abstract methods for impl classes
/**********************************************************************
*/
protected abstract T _deserializeEntries(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException;
/*
/**********************************************************************
/* Helper methods
/**********************************************************************
*/
}