Search in sources :

Example 16 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.

the class BeanDeserializerFactory method constructSettableProperty.

/**
     * Method that will construct a regular bean property setter using
     * the given setter method.
     *
     * @return Property constructed, if any; or null to indicate that
     *   there should be no property based on given definitions.
     */
protected SettableBeanProperty constructSettableProperty(DeserializationContext ctxt, BeanDescription beanDesc, BeanPropertyDefinition propDef, JavaType propType0) throws JsonMappingException {
    // need to ensure method is callable (for non-public)
    AnnotatedMember mutator = propDef.getNonConstructorMutator();
    //   Possibly passing creator parameter?
    if (mutator == null) {
        ctxt.reportBadPropertyDefinition(beanDesc, propDef, "No non-constructor mutator available");
    }
    JavaType type = resolveMemberAndTypeAnnotations(ctxt, mutator, propType0);
    // Does the Method specify the deserializer to use? If so, let's use it.
    TypeDeserializer typeDeser = type.getTypeHandler();
    SettableBeanProperty prop;
    if (mutator instanceof AnnotatedMethod) {
        prop = new MethodProperty(propDef, type, typeDeser, beanDesc.getClassAnnotations(), (AnnotatedMethod) mutator);
    } else {
        // 08-Sep-2016, tatu: wonder if we should verify it is `AnnotatedField` to be safe?
        prop = new FieldProperty(propDef, type, typeDeser, beanDesc.getClassAnnotations(), (AnnotatedField) mutator);
    }
    JsonDeserializer<?> deser = findDeserializerFromAnnotation(ctxt, mutator);
    if (deser == null) {
        deser = type.getValueHandler();
    }
    if (deser != null) {
        deser = ctxt.handlePrimaryContextualization(deser, prop, type);
        prop = prop.withValueDeserializer(deser);
    }
    // need to retain name of managed forward references:
    AnnotationIntrospector.ReferenceProperty ref = propDef.findReferenceType();
    if (ref != null && ref.isManagedReference()) {
        prop.setManagedReferenceName(ref.getName());
    }
    ObjectIdInfo objectIdInfo = propDef.findObjectIdInfo();
    if (objectIdInfo != null) {
        prop.setObjectIdInfo(objectIdInfo);
    }
    return prop;
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer)

Example 17 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.

the class CollectionDeserializer method createContextual.

/*
    /**********************************************************
    /* Validation, post-processing (ResolvableDeserializer)
    /**********************************************************
     */
/**
     * Method called to finalize setup of this deserializer,
     * when it is known for which property deserializer is needed
     * for.
     */
@Override
public CollectionDeserializer createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
    // May need to resolve types for delegate-based creators:
    JsonDeserializer<Object> delegateDeser = null;
    if (_valueInstantiator != null) {
        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()));
            }
            delegateDeser = findDeserializer(ctxt, delegateType, property);
        } 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()));
            }
            delegateDeser = findDeserializer(ctxt, delegateType, property);
        }
    }
    // [databind#1043]: allow per-property allow-wrapping of single overrides:
    // 11-Dec-2015, tatu: Should we pass basic `Collection.class`, or more refined? Mostly
    //   comes down to "List vs Collection" I suppose... for now, pass Collection
    Boolean unwrapSingle = findFormatFeature(ctxt, property, Collection.class, JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    // also, often value deserializer is resolved here:
    JsonDeserializer<?> valueDeser = _valueDeserializer;
    // May have a content converter
    valueDeser = findConvertingContentDeserializer(ctxt, property, valueDeser);
    final JavaType vt = _containerType.getContentType();
    if (valueDeser == null) {
        valueDeser = ctxt.findContextualValueDeserializer(vt, property);
    } else {
        // if directly assigned, probably not yet contextual, so:
        valueDeser = ctxt.handleSecondaryContextualization(valueDeser, property, vt);
    }
    // and finally, type deserializer needs context as well
    TypeDeserializer valueTypeDeser = _valueTypeDeserializer;
    if (valueTypeDeser != null) {
        valueTypeDeser = valueTypeDeser.forProperty(property);
    }
    NullValueProvider nuller = findContentNullProvider(ctxt, property, valueDeser);
    if ((unwrapSingle != _unwrapSingle) || (nuller != _nullProvider) || (delegateDeser != _delegateDeserializer) || (valueDeser != _valueDeserializer) || (valueTypeDeser != _valueTypeDeserializer)) {
        return withResolved(delegateDeser, valueDeser, valueTypeDeser, nuller, unwrapSingle);
    }
    return this;
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer)

Example 18 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.

the class CollectionDeserializer method handleNonArray.

/**
     * Helper method called when current token is no START_ARRAY. Will either
     * throw an exception, or try to handle value as if member of implicit
     * array, depending on configuration.
     */
@SuppressWarnings("unchecked")
protected final Collection<Object> handleNonArray(JsonParser p, DeserializationContext ctxt, Collection<Object> result) throws IOException {
    // Implicit arrays from single values?
    boolean canWrap = (_unwrapSingle == Boolean.TRUE) || ((_unwrapSingle == null) && ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY));
    if (!canWrap) {
        return (Collection<Object>) ctxt.handleUnexpectedToken(_containerType.getRawClass(), p);
    }
    JsonDeserializer<Object> valueDes = _valueDeserializer;
    final TypeDeserializer typeDeser = _valueTypeDeserializer;
    JsonToken t = p.getCurrentToken();
    Object value;
    try {
        if (t == JsonToken.VALUE_NULL) {
            // 03-Feb-2017, tatu: Hmmh. I wonder... let's try skipping here, too
            if (_skipNullValues) {
                return result;
            }
            value = _nullProvider.getNullValue(ctxt);
        } else if (typeDeser == null) {
            value = valueDes.deserialize(p, ctxt);
        } else {
            value = valueDes.deserializeWithType(p, ctxt, typeDeser);
        }
    } catch (Exception e) {
        // note: pass Object.class, not Object[].class, as we need element type for error info
        throw JsonMappingException.wrapWithPath(e, Object.class, result.size());
    }
    result.add(value);
    return result;
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) IOException(java.io.IOException)

Example 19 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.

the class EnumMapDeserializer method createContextual.

/**
     * Method called to finalize setup of this deserializer,
     * when it is known for which property deserializer is needed for.
     */
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
    // note: instead of finding key deserializer, with enums we actually
    // work with regular deserializers (less code duplication; but not
    // quite as clean as it ought to be)
    KeyDeserializer keyDeser = _keyDeserializer;
    if (keyDeser == null) {
        keyDeser = ctxt.findKeyDeserializer(_containerType.getKeyType(), property);
    }
    JsonDeserializer<?> valueDeser = _valueDeserializer;
    final JavaType vt = _containerType.getContentType();
    if (valueDeser == null) {
        valueDeser = ctxt.findContextualValueDeserializer(vt, property);
    } else {
        // if directly assigned, probably not yet contextual, so:
        valueDeser = ctxt.handleSecondaryContextualization(valueDeser, property, vt);
    }
    TypeDeserializer vtd = _valueTypeDeserializer;
    if (vtd != null) {
        vtd = vtd.forProperty(property);
    }
    return withResolved(keyDeser, valueDeser, vtd, findContentNullProvider(ctxt, property, valueDeser));
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer)

Example 20 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.

the class MapDeserializer method _deserializeUsingCreator.

@SuppressWarnings("unchecked")
public Map<Object, Object> _deserializeUsingCreator(JsonParser p, DeserializationContext ctxt) throws IOException {
    final PropertyBasedCreator creator = _propertyBasedCreator;
    // null -> no ObjectIdReader for Maps (yet?)
    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()) {
        // to get to value
        JsonToken t = p.nextToken();
        if (_ignorableProperties != null && _ignorableProperties.contains(key)) {
            // and skip it (in case of array/object)
            p.skipChildren();
            continue;
        }
        // creator property?
        SettableBeanProperty prop = creator.findCreatorProperty(key);
        if (prop != null) {
            // Last property to set?
            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;
        }
        // other property? needs buffering
        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);
    }
    // if so, can just construct and leave...
    try {
        return (Map<Object, Object>) creator.build(ctxt, buffer);
    } catch (Exception e) {
        wrapAndThrow(e, _containerType.getRawClass(), key);
        return null;
    }
}
Also used : PropertyValueBuffer(com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer) PropertyBasedCreator(com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) IOException(java.io.IOException)

Aggregations

TypeDeserializer (com.fasterxml.jackson.databind.jsontype.TypeDeserializer)30 IOException (java.io.IOException)9 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)2 ObjectBuffer (com.fasterxml.jackson.databind.util.ObjectBuffer)2 NullValueProvider (com.fasterxml.jackson.databind.deser.NullValueProvider)1 PropertyBasedCreator (com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator)1 PropertyValueBuffer (com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer)1 Referring (com.fasterxml.jackson.databind.deser.impl.ReadableObjectId.Referring)1 TypeWrappedDeserializer (com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer)1 AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)1