package com.fasterxml.jackson.databind.deser.std;
import java.io.IOException;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator;
import com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer;
import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId.Referring;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.util.ArrayBuilders;
@JacksonStdImpl
public class MapDeserializer
extends ContainerDeserializerBase<Map<Object,Object>>
implements ContextualDeserializer, ResolvableDeserializer
{
private static final long serialVersionUID = 1L;
protected final KeyDeserializer _keyDeserializer;
protected boolean _standardStringKey;
protected final JsonDeserializer<Object> _valueDeserializer;
protected final TypeDeserializer _valueTypeDeserializer;
protected final ValueInstantiator _valueInstantiator;
protected JsonDeserializer<Object> _delegateDeserializer;
protected PropertyBasedCreator _propertyBasedCreator;
protected final boolean _hasDefaultCreator;
protected Set<String> _ignorableProperties;
public MapDeserializer(JavaType mapType, ValueInstantiator valueInstantiator,
KeyDeserializer keyDeser, JsonDeserializer<Object> valueDeser,
TypeDeserializer valueTypeDeser)
{
super(mapType, null, null);
_keyDeserializer = keyDeser;
_valueDeserializer = valueDeser;
_valueTypeDeserializer = valueTypeDeser;
_valueInstantiator = valueInstantiator;
_hasDefaultCreator = valueInstantiator.canCreateUsingDefault();
_delegateDeserializer = null;
_propertyBasedCreator = null;
_standardStringKey = _isStdKeyDeser(mapType, keyDeser);
}
protected MapDeserializer(MapDeserializer src)
{
super(src);
_keyDeserializer = src._keyDeserializer;
_valueDeserializer = src._valueDeserializer;
_valueTypeDeserializer = src._valueTypeDeserializer;
_valueInstantiator = src._valueInstantiator;
_propertyBasedCreator = src._propertyBasedCreator;
_delegateDeserializer = src._delegateDeserializer;
_hasDefaultCreator = src._hasDefaultCreator;
_ignorableProperties = src._ignorableProperties;
_standardStringKey = src._standardStringKey;
}
protected MapDeserializer(MapDeserializer src,
KeyDeserializer keyDeser, JsonDeserializer<Object> valueDeser,
TypeDeserializer valueTypeDeser,
NullValueProvider nuller,
Set<String> ignorable)
{
super(src, nuller, src._unwrapSingle);
_keyDeserializer = keyDeser;
_valueDeserializer = valueDeser;
_valueTypeDeserializer = valueTypeDeser;
_valueInstantiator = src._valueInstantiator;
_propertyBasedCreator = src._propertyBasedCreator;
_delegateDeserializer = src._delegateDeserializer;
_hasDefaultCreator = src._hasDefaultCreator;
_ignorableProperties = ignorable;
_standardStringKey = _isStdKeyDeser(_containerType, keyDeser);
}
@SuppressWarnings("unchecked")
protected MapDeserializer withResolved(KeyDeserializer keyDeser,
TypeDeserializer valueTypeDeser, JsonDeserializer<?> valueDeser,
NullValueProvider nuller,
Set<String> ignorable)
{
if ((_keyDeserializer == keyDeser) && (_valueDeserializer == valueDeser)
&& (_valueTypeDeserializer == valueTypeDeser) && (_nullProvider == nuller)
&& (_ignorableProperties == ignorable)) {
return this;
}
return new MapDeserializer(this,
keyDeser, (JsonDeserializer<Object>) valueDeser, valueTypeDeser,
nuller, ignorable);
}
protected final boolean _isStdKeyDeser(JavaType mapType, KeyDeserializer keyDeser)
{
if (keyDeser == null) {
return true;
}
JavaType keyType = mapType.getKeyType();
if (keyType == null) {
return true;
}
Class<?> rawKeyType = keyType.getRawClass();
return ((rawKeyType == String.class || rawKeyType == Object.class)
&& isDefaultKeyDeserializer(keyDeser));
}
public void setIgnorableProperties(String[] ignorable) {
_ignorableProperties = (ignorable == null || ignorable.length == 0) ?
null : ArrayBuilders.arrayToSet(ignorable);
}
public void setIgnorableProperties(Set<String> ignorable) {
_ignorableProperties = (ignorable == null || ignorable.size() == 0) ?
null : ignorable;
}
@Override
public void resolve(DeserializationContext ctxt) throws JsonMappingException
{
if (_valueInstantiator.canCreateUsingDelegate()) {
JavaType delegateType = _valueInstantiator.getDelegateType(ctxt.getConfig());
if (delegateType == null) {
ctxt.reportBadDefinition(_containerType, String.format(
"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'",
_containerType,
_valueInstantiator.getClass().getName()));
}
_delegateDeserializer = findDeserializer(ctxt, delegateType, null);
} else if (_valueInstantiator.canCreateUsingArrayDelegate()) {
JavaType delegateType = _valueInstantiator.getArrayDelegateType(ctxt.getConfig());
if (delegateType == null) {
ctxt.reportBadDefinition(_containerType, String.format(
"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingArrayDelegate()', but null for 'getArrayDelegateType()'",
_containerType,
_valueInstantiator.getClass().getName()));
}
_delegateDeserializer = findDeserializer(ctxt, delegateType, null);
}
if (_valueInstantiator.canCreateFromObjectWith()) {
SettableBeanProperty[] creatorProps = _valueInstantiator.getFromObjectArguments(ctxt.getConfig());
_propertyBasedCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, creatorProps,
ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
}
_standardStringKey = _isStdKeyDeser(_containerType, _keyDeserializer);
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
KeyDeserializer keyDeser = _keyDeserializer;
if (keyDeser == null) {
keyDeser = ctxt.findKeyDeserializer(_containerType.getKeyType(), property);
} else {
if (keyDeser instanceof ContextualKeyDeserializer) {
keyDeser = ((ContextualKeyDeserializer) keyDeser).createContextual(ctxt, property);
}
}
JsonDeserializer<?> valueDeser = _valueDeserializer;
if (property != null) {
valueDeser = findConvertingContentDeserializer(ctxt, property, valueDeser);
}
final JavaType vt = _containerType.getContentType();
if (valueDeser == null) {
valueDeser = ctxt.findContextualValueDeserializer(vt, property);
} else {
valueDeser = ctxt.handleSecondaryContextualization(valueDeser, property, vt);
}
TypeDeserializer vtd = _valueTypeDeserializer;
if (vtd != null) {
vtd = vtd.forProperty(property);
}
Set<String> ignored = _ignorableProperties;
AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
if (_neitherNull(intr, property)) {
AnnotatedMember member = property.getMember();
if (member != null) {
JsonIgnoreProperties.Value ignorals = intr.findPropertyIgnorals(member);
if (ignorals != null) {
Set<String> ignoresToAdd = ignorals.findIgnoredForDeserialization();
if (!ignoresToAdd.isEmpty()) {
ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
for (String str : ignoresToAdd) {
ignored.add(str);
}
}
}
}
}
return withResolved(keyDeser, vtd, valueDeser,
findContentNullProvider(ctxt, property, valueDeser), ignored);
}
@Override
public JsonDeserializer<Object> getContentDeserializer() {
return _valueDeserializer;
}
@Override
public ValueInstantiator getValueInstantiator() {
return _valueInstantiator;
}
@Override
public boolean isCachable() {
return (_valueDeserializer == null)
&& (_keyDeserializer == null)
&& (_valueTypeDeserializer == null)
&& (_ignorableProperties == null);
}
@Override
@SuppressWarnings("unchecked")
public Map<Object,Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
if (_propertyBasedCreator != null) {
return _deserializeUsingCreator(p, ctxt);
}
if (_delegateDeserializer != null) {
return (Map<Object,Object>) _valueInstantiator.createUsingDelegate(ctxt,
_delegateDeserializer.deserialize(p, ctxt));
}
if (!_hasDefaultCreator) {
return (Map<Object,Object> ) ctxt.handleMissingInstantiator(getMapClass(),
getValueInstantiator(), p,
"no default constructor found");
}
JsonToken t = p.getCurrentToken();
if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
if (t == JsonToken.VALUE_STRING) {
return (Map<Object,Object>) _valueInstantiator.createFromString(ctxt, p.getText());
}
return _deserializeFromEmpty(p, ctxt);
}
final Map<Object,Object> result = (Map<Object,Object>) _valueInstantiator.createUsingDefault(ctxt);
if (_standardStringKey) {
_readAndBindStringKeyMap(p, ctxt, result);
return result;
}
_readAndBind(p, ctxt, result);
return result;
}
@SuppressWarnings("unchecked")
@Override
public Map<Object,Object> deserialize(JsonParser p, DeserializationContext ctxt,
Map<Object,Object> result)
throws IOException
{
p.setCurrentValue(result);
JsonToken t = p.getCurrentToken();
if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME) {
return (Map<Object,Object>) ctxt.handleUnexpectedToken(getMapClass(), p);
}
if (_standardStringKey) {
_readAndUpdateStringKeyMap(p, ctxt, result);
return result;
}
_readAndUpdate(p, ctxt, result);
return result;
}
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
throws IOException
{
return typeDeserializer.deserializeTypedFromObject(p, ctxt);
}
@SuppressWarnings("unchecked")
public final Class<?> getMapClass() { return (Class<Map<Object,Object>>) _containerType.getRawClass(); }
@Override public JavaType getValueType() { return _containerType; }
protected final void _readAndBind(JsonParser p, DeserializationContext ctxt,
Map<Object,Object> result) throws IOException
{
final KeyDeserializer keyDes = _keyDeserializer;
final JsonDeserializer<Object> valueDes = _valueDeserializer;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
MapReferringAccumulator referringAccumulator = null;
boolean useObjectId = valueDes.getObjectIdReader() != null;
if (useObjectId) {
referringAccumulator = new MapReferringAccumulator(_containerType.getContentType().getRawClass(),
result);
}
String keyStr;
if (p.isExpectedStartObjectToken()) {
keyStr = p.nextFieldName();
} else {
JsonToken t = p.getCurrentToken();
if (t != JsonToken.FIELD_NAME) {
if (t == JsonToken.END_OBJECT) {
return;
}
ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
}
keyStr = p.getCurrentName();
}
for (; keyStr != null; keyStr = p.nextFieldName()) {
Object key = keyDes.deserializeKey(keyStr, ctxt);
JsonToken t = p.nextToken();
if (_ignorableProperties != null && _ignorableProperties.contains(keyStr)) {
p.skipChildren();
continue;
}
try {
Object value;
if (t == JsonToken.VALUE_NULL) {
if (_skipNullValues) {
continue;
}
value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
if (useObjectId) {
referringAccumulator.put(key, value);
} else {
result.put(key, value);
}
} catch (UnresolvedForwardReference reference) {
handleUnresolvedReference(ctxt, referringAccumulator, key, reference);
} catch (Exception e) {
wrapAndThrow(e, result, keyStr);
}
}
}
protected final void _readAndBindStringKeyMap(JsonParser p, DeserializationContext ctxt,
Map<Object,Object> result) throws IOException
{
final JsonDeserializer<Object> valueDes = _valueDeserializer;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
MapReferringAccumulator referringAccumulator = null;
boolean useObjectId = (valueDes.getObjectIdReader() != null);
if (useObjectId) {
referringAccumulator = new MapReferringAccumulator(_containerType.getContentType().getRawClass(), result);
}
String key;
if (p.isExpectedStartObjectToken()) {
key = p.nextFieldName();
} else {
JsonToken t = p.getCurrentToken();
if (t == JsonToken.END_OBJECT) {
return;
}
if (t != JsonToken.FIELD_NAME) {
ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
}
key = p.getCurrentName();
}
for (; key != null; key = p.nextFieldName()) {
JsonToken t = p.nextToken();
if (_ignorableProperties != null && _ignorableProperties.contains(key)) {
p.skipChildren();
continue;
}
try {
Object value;
if (t == JsonToken.VALUE_NULL) {
if (_skipNullValues) {
continue;
}
value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
if (useObjectId) {
referringAccumulator.put(key, value);
} else {
result.put(key, value);
}
} catch (UnresolvedForwardReference reference) {
handleUnresolvedReference(ctxt, referringAccumulator, key, reference);
} catch (Exception e) {
wrapAndThrow(e, result, key);
}
}
}
@SuppressWarnings("unchecked")
public Map<Object,Object> _deserializeUsingCreator(JsonParser p, DeserializationContext ctxt) throws IOException
{
final PropertyBasedCreator creator = _propertyBasedCreator;
PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, null);
final JsonDeserializer<Object> valueDes = _valueDeserializer;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
String key;
if (p.isExpectedStartObjectToken()) {
key = p.nextFieldName();
} else if (p.hasToken(JsonToken.FIELD_NAME)) {
key = p.getCurrentName();
} else {
key = null;
}
for (; key != null; key = p.nextFieldName()) {
JsonToken t = p.nextToken();
if (_ignorableProperties != null && _ignorableProperties.contains(key)) {
p.skipChildren();
continue;
}
SettableBeanProperty prop = creator.findCreatorProperty(key);
if (prop != null) {
if (buffer.assignParameter(prop, prop.deserialize(p, ctxt))) {
p.nextToken();
Map<Object,Object> result;
try {
result = (Map<Object,Object>)creator.build(ctxt, buffer);
} catch (Exception e) {
return wrapAndThrow(e, _containerType.getRawClass(), key);
}
_readAndBind(p, ctxt, result);
return result;
}
continue;
}
Object actualKey = _keyDeserializer.deserializeKey(key, ctxt);
Object value;
try {
if (t == JsonToken.VALUE_NULL) {
if (_skipNullValues) {
continue;
}
value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
} catch (Exception e) {
wrapAndThrow(e, _containerType.getRawClass(), key);
return null;
}
buffer.bufferMapProperty(actualKey, value);
}
try {
return (Map<Object,Object>)creator.build(ctxt, buffer);
} catch (Exception e) {
wrapAndThrow(e, _containerType.getRawClass(), key);
return null;
}
}
protected final void _readAndUpdate(JsonParser p, DeserializationContext ctxt,
Map<Object,Object> result) throws IOException
{
final KeyDeserializer keyDes = _keyDeserializer;
final JsonDeserializer<Object> valueDes = _valueDeserializer;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
String keyStr;
if (p.isExpectedStartObjectToken()) {
keyStr = p.nextFieldName();
} else {
JsonToken t = p.getCurrentToken();
if (t == JsonToken.END_OBJECT) {
return;
}
if (t != JsonToken.FIELD_NAME) {
ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
}
keyStr = p.getCurrentName();
}
for (; keyStr != null; keyStr = p.nextFieldName()) {
Object key = keyDes.deserializeKey(keyStr, ctxt);
JsonToken t = p.nextToken();
if (_ignorableProperties != null && _ignorableProperties.contains(keyStr)) {
p.skipChildren();
continue;
}
try {
if (t == JsonToken.VALUE_NULL) {
if (_skipNullValues) {
continue;
}
result.put(key, _nullProvider.getNullValue(ctxt));
continue;
}
Object old = result.get(key);
Object value;
if (old != null) {
if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt, old);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser, old);
}
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
if (value != old) {
result.put(key, value);
}
} catch (Exception e) {
wrapAndThrow(e, result, keyStr);
}
}
}
protected final void _readAndUpdateStringKeyMap(JsonParser p, DeserializationContext ctxt,
Map<Object,Object> result) throws IOException
{
final JsonDeserializer<Object> valueDes = _valueDeserializer;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
String key;
if (p.isExpectedStartObjectToken()) {
key = p.nextFieldName();
} else {
JsonToken t = p.getCurrentToken();
if (t == JsonToken.END_OBJECT) {
return;
}
if (t != JsonToken.FIELD_NAME) {
ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
}
key = p.getCurrentName();
}
for (; key != null; key = p.nextFieldName()) {
JsonToken t = p.nextToken();
if (_ignorableProperties != null && _ignorableProperties.contains(key)) {
p.skipChildren();
continue;
}
try {
if (t == JsonToken.VALUE_NULL) {
if (_skipNullValues) {
continue;
}
result.put(key, _nullProvider.getNullValue(ctxt));
continue;
}
Object old = result.get(key);
Object value;
if (old != null) {
if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt, old);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser, old);
}
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
if (value != old) {
result.put(key, value);
}
} catch (Exception e) {
wrapAndThrow(e, result, key);
}
}
}
private void handleUnresolvedReference(DeserializationContext ctxt,
MapReferringAccumulator accumulator,
Object key, UnresolvedForwardReference reference)
throws JsonMappingException
{
if (accumulator == null) {
ctxt.reportInputMismatch(this,
"Unresolved forward reference but no identity info: "+reference);
}
Referring referring = accumulator.handleUnresolvedReference(reference, key);
reference.getRoid().appendReferring(referring);
}
private final static class MapReferringAccumulator {
private final Class<?> _valueType;
private Map<Object,Object> _result;
private List<MapReferring> _accumulator = new ArrayList<MapReferring>();
public MapReferringAccumulator(Class<?> valueType, Map<Object, Object> result) {
_valueType = valueType;
_result = result;
}
public void put(Object key, Object value)
{
if (_accumulator.isEmpty()) {
_result.put(key, value);
} else {
MapReferring ref = _accumulator.get(_accumulator.size() - 1);
ref.next.put(key, value);
}
}
public Referring handleUnresolvedReference(UnresolvedForwardReference reference, Object key)
{
MapReferring id = new MapReferring(this, reference, _valueType, key);
_accumulator.add(id);
return id;
}
public void resolveForwardReference(Object id, Object value) throws IOException
{
Iterator<MapReferring> iterator = _accumulator.iterator();
Map<Object,Object> previous = _result;
while (iterator.hasNext()) {
MapReferring ref = iterator.next();
if (ref.hasId(id)) {
iterator.remove();
previous.put(ref.key, value);
previous.putAll(ref.next);
return;
}
previous = ref.next;
}
throw new IllegalArgumentException("Trying to resolve a forward reference with id [" + id
+ "] that wasn't previously seen as unresolved.");
}
}
static class MapReferring extends Referring {
private final MapReferringAccumulator _parent;
public final Map<Object, Object> next = new LinkedHashMap<Object, Object>();
public final Object key;
MapReferring(MapReferringAccumulator parent, UnresolvedForwardReference ref,
Class<?> valueType, Object key)
{
super(ref, valueType);
_parent = parent;
this.key = key;
}
@Override
public void handleResolvedForwardReference(Object id, Object value) throws IOException {
_parent.resolveForwardReference(id, value);
}
}
}