com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-databind Show documentation
Show all versions of jackson-databind Show documentation
General data-binding functionality for Jackson: works on core streaming API
package com.fasterxml.jackson.databind.jsontype.impl;
import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.util.JsonParserSequence;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.util.TokenBuffer;
/**
* Type deserializer used with {@link As#PROPERTY}
* inclusion mechanism.
* Uses regular form (additional key/value entry before actual data)
* when typed object is expressed as JSON Object; otherwise behaves similar to how
* {@link As#WRAPPER_ARRAY} works.
* Latter is used if JSON representation is polymorphic
*
* @author tatu
*/
public class AsPropertyTypeDeserializer extends AsArrayTypeDeserializer
{
private static final long serialVersionUID = 1L;
public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, Class defaultImpl)
{
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
}
public AsPropertyTypeDeserializer(AsPropertyTypeDeserializer src, BeanProperty property) {
super(src, property);
}
@Override
public TypeDeserializer forProperty(BeanProperty prop) {
if (prop == _property) { // usually if it's null
return this;
}
return new AsPropertyTypeDeserializer(this, prop);
}
@Override
public As getTypeInclusion() {
return As.PROPERTY;
}
/**
* This is the trickiest thing to handle, since property we are looking
* for may be anywhere...
*/
@Override
public Object deserializeTypedFromObject(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
// but first, sanity check to ensure we have START_OBJECT or FIELD_NAME
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.START_OBJECT) {
t = jp.nextToken();
} else if (t == JsonToken.START_ARRAY) {
/* This is most likely due to the fact that not all Java types are
* serialized as JSON Objects; so if "as-property" inclusion is requested,
* serialization of things like Lists must be instead handled as if
* "as-wrapper-array" was requested.
* But this can also be due to some custom handling: so, if "defaultImpl"
* is defined, it will be asked to handle this case.
*/
return _deserializeTypedUsingDefaultImpl(jp, ctxt, null);
} else if (t != JsonToken.FIELD_NAME) {
return _deserializeTypedUsingDefaultImpl(jp, ctxt, null);
}
// Ok, let's try to find the property. But first, need token buffer...
TokenBuffer tb = null;
for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
String name = jp.getCurrentName();
jp.nextToken(); // to point to the value
if (_typePropertyName.equals(name)) { // gotcha!
return _deserializeTypedForId(jp, ctxt, tb);
}
if (tb == null) {
tb = new TokenBuffer(null);
}
tb.writeFieldName(name);
tb.copyCurrentStructure(jp);
}
return _deserializeTypedUsingDefaultImpl(jp, ctxt, tb);
}
protected final Object _deserializeTypedForId(JsonParser jp, DeserializationContext ctxt,
TokenBuffer tb)
throws IOException, JsonProcessingException
{
String typeId = jp.getText();
JsonDeserializer