|
| 1 | +package com.fasterxml.jackson.dataformat.avro; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 6 | +import com.fasterxml.jackson.core.JsonParser; |
| 7 | +import com.fasterxml.jackson.databind.BeanProperty; |
| 8 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 9 | +import com.fasterxml.jackson.databind.JavaType; |
| 10 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 11 | +import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; |
| 12 | +import com.fasterxml.jackson.databind.jsontype.TypeIdResolver; |
| 13 | +import com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase; |
| 14 | +import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaHelper; |
| 15 | + |
| 16 | +public class AvroTypeDeserializer extends TypeDeserializerBase { |
| 17 | + |
| 18 | + protected AvroTypeDeserializer(JavaType baseType, TypeIdResolver idRes, String typePropertyName, boolean typeIdVisible, |
| 19 | + JavaType defaultImpl) { |
| 20 | + super(baseType, idRes, typePropertyName, typeIdVisible, defaultImpl); |
| 21 | + } |
| 22 | + |
| 23 | + protected AvroTypeDeserializer(TypeDeserializerBase src, BeanProperty property) { |
| 24 | + super(src, property); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public TypeDeserializer forProperty(BeanProperty prop) { |
| 29 | + return new AvroTypeDeserializer(this, prop); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public JsonTypeInfo.As getTypeInclusion() { |
| 34 | + // Don't do any restructuring of the incoming JSON tokens |
| 35 | + return JsonTypeInfo.As.EXISTING_PROPERTY; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Object deserializeTypedFromObject(JsonParser p, DeserializationContext ctxt) throws IOException { |
| 40 | + return deserializeTypedFromAny(p, ctxt); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Object deserializeTypedFromArray(JsonParser p, DeserializationContext ctxt) throws IOException { |
| 45 | + return deserializeTypedFromAny(p, ctxt); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Object deserializeTypedFromScalar(JsonParser p, DeserializationContext ctxt) throws IOException { |
| 50 | + return deserializeTypedFromAny(p, ctxt); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Object deserializeTypedFromAny(JsonParser p, DeserializationContext ctxt) throws IOException { |
| 55 | + if (p.getTypeId() == null && getDefaultImpl() == null) { |
| 56 | + JsonDeserializer<Object> deser = _findDeserializer(ctxt, AvroSchemaHelper.getTypeId(_baseType)); |
| 57 | + if (deser == null) { |
| 58 | + ctxt.reportInputMismatch(_baseType, "No (native) type id found when one was expected for polymorphic type handling"); |
| 59 | + return null; |
| 60 | + } |
| 61 | + return deser.deserialize(p, ctxt); |
| 62 | + } |
| 63 | + return _deserializeWithNativeTypeId(p, ctxt, p.getTypeId()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected JavaType _handleUnknownTypeId(DeserializationContext ctxt, String typeId) |
| 68 | + throws IOException { |
| 69 | + if (ctxt.hasValueDeserializerFor(_baseType, null)) { |
| 70 | + return _baseType; |
| 71 | + } |
| 72 | + return super._handleUnknownTypeId(ctxt, typeId); |
| 73 | + } |
| 74 | +} |
0 commit comments