com.datastax.oss.driver.shaded.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer Maven / Gradle / Ivy
The newest version!
package com.datastax.oss.driver.shaded.fasterxml.jackson.databind.jsontype.impl;
import java.io.IOException;
import com.datastax.oss.driver.shaded.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.datastax.oss.driver.shaded.fasterxml.jackson.core.JsonParser;
import com.datastax.oss.driver.shaded.fasterxml.jackson.core.JsonToken;
import com.datastax.oss.driver.shaded.fasterxml.jackson.core.util.JsonParserSequence;
import com.datastax.oss.driver.shaded.fasterxml.jackson.databind.*;
import com.datastax.oss.driver.shaded.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.datastax.oss.driver.shaded.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.datastax.oss.driver.shaded.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
*/
@SuppressWarnings("resource")
public class AsPropertyTypeDeserializer extends AsArrayTypeDeserializer
{
private static final long serialVersionUID = 1L;
protected final As _inclusion;
// @since 2.12.2 (see [databind#3055]
protected final String _msgForMissingId = (_property == null)
? String.format("missing type id property '%s'", _typePropertyName)
: String.format("missing type id property '%s' (for POJO property '%s')", _typePropertyName, _property.getName());
/**
* @since 2.8
*/
public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl)
{
this(bt, idRes, typePropertyName, typeIdVisible, defaultImpl, As.PROPERTY);
}
/**
* @since 2.8
*/
public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl,
As inclusion)
{
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
_inclusion = inclusion;
}
public AsPropertyTypeDeserializer(AsPropertyTypeDeserializer src, BeanProperty property) {
super(src, property);
_inclusion = src._inclusion;
}
@Override
public TypeDeserializer forProperty(BeanProperty prop) {
return (prop == _property) ? this : new AsPropertyTypeDeserializer(this, prop);
}
@Override
public As getTypeInclusion() { return _inclusion; }
/**
* This is the trickiest thing to handle, since property we are looking
* for may be anywhere...
*/
@Override
public Object deserializeTypedFromObject(JsonParser p, DeserializationContext ctxt) throws IOException
{
// 02-Aug-2013, tatu: May need to use native type ids
if (p.canReadTypeId()) {
Object typeId = p.getTypeId();
if (typeId != null) {
return _deserializeWithNativeTypeId(p, ctxt, typeId);
}
}
// but first, sanity check to ensure we have START_OBJECT or FIELD_NAME
JsonToken t = p.currentToken();
if (t == JsonToken.START_OBJECT) {
t = p.nextToken();
} else if (/*t == JsonToken.START_ARRAY ||*/ t != JsonToken.FIELD_NAME) {
/* 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(p, ctxt, null, _msgForMissingId);
}
// Ok, let's try to find the property. But first, need token buffer...
TokenBuffer tb = null;
boolean ignoreCase = ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
final String name = p.currentName();
p.nextToken(); // to point to the value
if (name.equals(_typePropertyName)
|| (ignoreCase && name.equalsIgnoreCase(_typePropertyName))) { // gotcha!
// 09-Sep-2021, tatu: [databind#3271]: Avoid converting null to "null"
String typeId = p.getValueAsString();
if (typeId != null) {
return _deserializeTypedForId(p, ctxt, tb, typeId);
}
}
if (tb == null) {
tb = ctxt.bufferForInputBuffering(p);
}
tb.writeFieldName(name);
tb.copyCurrentStructure(p);
}
return _deserializeTypedUsingDefaultImpl(p, ctxt, tb, _msgForMissingId);
}
protected Object _deserializeTypedForId(JsonParser p, DeserializationContext ctxt,
TokenBuffer tb, String typeId) throws IOException
{
JsonDeserializer