package com.fasterxml.jackson.dataformat.xml.deser;
import java.io.IOException;
import java.util.*;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.util.JsonParserDelegate;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.std.DelegatingDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.dataformat.xml.util.TypeUtil;
public class WrapperHandlingDeserializer
extends DelegatingDeserializer
{
private static final long serialVersionUID = 1L;
protected final Set<String> _namesToWrap;
protected final JavaType _type;
public WrapperHandlingDeserializer(BeanDeserializerBase delegate) {
this(delegate, null);
}
public WrapperHandlingDeserializer(BeanDeserializerBase delegate, Set<String> namesToWrap)
{
super(delegate);
_namesToWrap = namesToWrap;
_type = delegate.getValueType();
}
@Override
protected JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee0) {
throw new IllegalStateException("Internal error: should never get called");
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
throws JsonMappingException
{
JavaType vt = _type;
if (vt == null) {
vt = ctxt.constructType(_delegatee.handledType());
}
JsonDeserializer<?> del = ctxt.handleSecondaryContextualization(_delegatee, property, vt);
BeanDeserializerBase newDelegatee = _verifyDeserType(del);
Iterator<SettableBeanProperty> it = newDelegatee.properties();
HashSet<String> unwrappedNames = null;
while (it.hasNext()) {
SettableBeanProperty prop = it.next();
JavaType type = prop.getType();
if (!TypeUtil.isIndexedType(type)) {
continue;
}
PropertyName wrapperName = prop.getWrapperName();
if ((wrapperName != null) && (wrapperName != PropertyName.NO_NAME)) {
continue;
}
if (unwrappedNames == null) {
unwrappedNames = new HashSet<String>();
}
unwrappedNames.add(prop.getName());
}
if (unwrappedNames == null) {
return newDelegatee;
}
return new WrapperHandlingDeserializer(newDelegatee, unwrappedNames);
}
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
_configureParser(p);
return _delegatee.deserialize(p, ctxt);
}
@SuppressWarnings("unchecked")
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt,
Object intoValue) throws IOException
{
_configureParser(p);
return ((JsonDeserializer<Object>)_delegatee).deserialize(p, ctxt, intoValue);
}
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer) throws IOException
{
_configureParser(p);
return _delegatee.deserializeWithType(p, ctxt, typeDeserializer);
}
@SuppressWarnings("resource")
protected final void _configureParser(JsonParser p) throws IOException
{
while (p instanceof JsonParserDelegate) {
p = ((JsonParserDelegate) p).delegate();
}
if (p instanceof FromXmlParser) {
((FromXmlParser) p).addVirtualWrapping(_namesToWrap);
}
}
protected BeanDeserializerBase _verifyDeserType(JsonDeserializer<?> deser)
{
if (!(deser instanceof BeanDeserializerBase)) {
throw new IllegalArgumentException("Can not change delegate to be of type "
+deser.getClass().getName());
}
return (BeanDeserializerBase) deser;
}
}