Search in sources :

Example 6 with SettableBeanProperty

use of com.fasterxml.jackson.databind.deser.SettableBeanProperty in project jackson-databind by FasterXML.

the class ThrowableDeserializer method deserializeFromObject.

/*
    /************************************************************
    /* Overridden methods
    /************************************************************
     */
@Override
public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) throws IOException {
    // 30-Sep-2010, tatu: Need to allow use of @JsonCreator, so:
    if (_propertyBasedCreator != null) {
        // proper @JsonCreator
        return _deserializeUsingPropertyBased(p, ctxt);
    }
    if (_delegateDeserializer != null) {
        return _valueInstantiator.createUsingDelegate(ctxt, _delegateDeserializer.deserialize(p, ctxt));
    }
    if (_beanType.isAbstract()) {
        // for good measure, check this too
        return ctxt.handleMissingInstantiator(handledType(), getValueInstantiator(), p, "abstract type (need to add/enable type information?)");
    }
    boolean hasStringCreator = _valueInstantiator.canCreateFromString();
    boolean hasDefaultCtor = _valueInstantiator.canCreateUsingDefault();
    // and finally, verify we do have single-String arg constructor (if no @JsonCreator)
    if (!hasStringCreator && !hasDefaultCtor) {
        return ctxt.handleMissingInstantiator(handledType(), getValueInstantiator(), p, "Throwable needs a default contructor, a single-String-arg constructor; or explicit @JsonCreator");
    }
    Object throwable = null;
    Object[] pending = null;
    int pendingIx = 0;
    for (; p.getCurrentToken() != JsonToken.END_OBJECT; p.nextToken()) {
        String propName = p.getCurrentName();
        SettableBeanProperty prop = _beanProperties.find(propName);
        // to point to field value
        p.nextToken();
        if (prop != null) {
            // normal case
            if (throwable != null) {
                prop.deserializeAndSet(p, ctxt, throwable);
                continue;
            }
            // nope; need to defer
            if (pending == null) {
                int len = _beanProperties.size();
                pending = new Object[len + len];
            }
            pending[pendingIx++] = prop;
            pending[pendingIx++] = prop.deserialize(p, ctxt);
            continue;
        }
        // Maybe it's "message"?
        if (PROP_NAME_MESSAGE.equals(propName)) {
            if (hasStringCreator) {
                throwable = _valueInstantiator.createFromString(ctxt, p.getText());
                // any pending values?
                if (pending != null) {
                    for (int i = 0, len = pendingIx; i < len; i += 2) {
                        prop = (SettableBeanProperty) pending[i];
                        prop.set(throwable, pending[i + 1]);
                    }
                    pending = null;
                }
                continue;
            }
        }
        // Things marked as ignorable should not be passed to any setter
        if ((_ignorableProps != null) && _ignorableProps.contains(propName)) {
            p.skipChildren();
            continue;
        }
        if (_anySetter != null) {
            _anySetter.deserializeAndSet(p, ctxt, throwable, propName);
            continue;
        }
        // Unknown: let's call handler method
        handleUnknownProperty(p, ctxt, throwable, propName);
    }
    // Sanity check: did we find "message"?
    if (throwable == null) {
        //throw new JsonMappingException("No 'message' property found: could not deserialize "+_beanType);
        if (hasStringCreator) {
            throwable = _valueInstantiator.createFromString(ctxt, null);
        } else {
            throwable = _valueInstantiator.createUsingDefault(ctxt);
        }
        // any pending values?
        if (pending != null) {
            for (int i = 0, len = pendingIx; i < len; i += 2) {
                SettableBeanProperty prop = (SettableBeanProperty) pending[i];
                prop.set(throwable, pending[i + 1]);
            }
        }
    }
    return throwable;
}
Also used : SettableBeanProperty(com.fasterxml.jackson.databind.deser.SettableBeanProperty)

Example 7 with SettableBeanProperty

use of com.fasterxml.jackson.databind.deser.SettableBeanProperty in project jackson-databind by FasterXML.

the class ExternalTypeHandler method complete.

/**
     * Variant called when creation of the POJO involves buffering of creator properties
     * as well as property-based creator.
     */
public Object complete(JsonParser p, DeserializationContext ctxt, PropertyValueBuffer buffer, PropertyBasedCreator creator) throws IOException {
    // first things first: deserialize all data buffered:
    final int len = _properties.length;
    Object[] values = new Object[len];
    for (int i = 0; i < len; ++i) {
        String typeId = _typeIds[i];
        final ExtTypedProperty extProp = _properties[i];
        if (typeId == null) {
            // let's allow missing both type and property (may already have been set, too)
            if (_tokens[i] == null) {
                continue;
            }
            // 26-Oct-2012, tatu: As per [databind#94], must allow use of 'defaultImpl'
            if (!extProp.hasDefaultType()) {
                ctxt.reportInputMismatch(_beanType, "Missing external type id property '%s'", extProp.getTypePropertyName());
            } else {
                typeId = extProp.getDefaultTypeId();
            }
        } else if (_tokens[i] == null) {
            SettableBeanProperty prop = extProp.getProperty();
            ctxt.reportInputMismatch(_beanType, "Missing property '%s' for external type id '%s'", prop.getName(), _properties[i].getTypePropertyName());
        }
        values[i] = _deserialize(p, ctxt, i, typeId);
        final SettableBeanProperty prop = extProp.getProperty();
        // also: if it's creator prop, fill in
        if (prop.getCreatorIndex() >= 0) {
            buffer.assignParameter(prop, values[i]);
            // [databind#999] And maybe there's creator property for type id too?
            SettableBeanProperty typeProp = extProp.getTypeProperty();
            // for now, should only be needed for creator properties, too
            if ((typeProp != null) && (typeProp.getCreatorIndex() >= 0)) {
                buffer.assignParameter(typeProp, typeId);
            }
        }
    }
    Object bean = creator.build(ctxt, buffer);
    // third: assign non-creator properties
    for (int i = 0; i < len; ++i) {
        SettableBeanProperty prop = _properties[i].getProperty();
        if (prop.getCreatorIndex() < 0) {
            prop.set(bean, values[i]);
        }
    }
    return bean;
}
Also used : SettableBeanProperty(com.fasterxml.jackson.databind.deser.SettableBeanProperty)

Example 8 with SettableBeanProperty

use of com.fasterxml.jackson.databind.deser.SettableBeanProperty in project jackson-databind by FasterXML.

the class PropertyBasedCreator method construct.

/**
     * Factory method used for building actual instances to be used with types
     * OTHER than POJOs.
     * resolves deserializers and checks for "null values".
     *
     * @since 2.9
     */
public static PropertyBasedCreator construct(DeserializationContext ctxt, ValueInstantiator valueInstantiator, SettableBeanProperty[] srcCreatorProps, boolean caseInsensitive) throws JsonMappingException {
    final int len = srcCreatorProps.length;
    SettableBeanProperty[] creatorProps = new SettableBeanProperty[len];
    for (int i = 0; i < len; ++i) {
        SettableBeanProperty prop = srcCreatorProps[i];
        if (!prop.hasValueDeserializer()) {
            prop = prop.withValueDeserializer(ctxt.findContextualValueDeserializer(prop.getType(), prop));
        }
        creatorProps[i] = prop;
    }
    return new PropertyBasedCreator(ctxt, valueInstantiator, creatorProps, caseInsensitive, false);
}
Also used : SettableBeanProperty(com.fasterxml.jackson.databind.deser.SettableBeanProperty)

Example 9 with SettableBeanProperty

use of com.fasterxml.jackson.databind.deser.SettableBeanProperty in project jackson-databind by FasterXML.

the class PropertyBasedCreator method construct.

/**
     * Factory method used for building actual instances to be used with POJOS:
     * resolves deserializers, checks for "null values".
     *
     * @since 2.9
     */
public static PropertyBasedCreator construct(DeserializationContext ctxt, ValueInstantiator valueInstantiator, SettableBeanProperty[] srcCreatorProps, BeanPropertyMap allProperties) throws JsonMappingException {
    final int len = srcCreatorProps.length;
    SettableBeanProperty[] creatorProps = new SettableBeanProperty[len];
    for (int i = 0; i < len; ++i) {
        SettableBeanProperty prop = srcCreatorProps[i];
        if (!prop.hasValueDeserializer()) {
            prop = prop.withValueDeserializer(ctxt.findContextualValueDeserializer(prop.getType(), prop));
        }
        creatorProps[i] = prop;
    }
    return new PropertyBasedCreator(ctxt, valueInstantiator, creatorProps, allProperties.isCaseInsensitive(), allProperties.hasAliases());
}
Also used : SettableBeanProperty(com.fasterxml.jackson.databind.deser.SettableBeanProperty)

Example 10 with SettableBeanProperty

use of com.fasterxml.jackson.databind.deser.SettableBeanProperty in project jackson-databind by FasterXML.

the class PropertyValueBuffer method getParameters.

/**
     * Method called to do necessary post-processing such as injection of values
     * and verification of values for required properties,
     * after either {@link #assignParameter(SettableBeanProperty, Object)}
     * returns <code>true</code> (to indicate all creator properties are found), or when
     * then whole JSON Object has been processed,
     */
public Object[] getParameters(SettableBeanProperty[] props) throws JsonMappingException {
    // quick check to see if anything else is needed
    if (_paramsNeeded > 0) {
        if (_paramsSeenBig == null) {
            int mask = _paramsSeen;
            // really matter for common cases
            for (int ix = 0, len = _creatorParameters.length; ix < len; ++ix, mask >>= 1) {
                if ((mask & 1) == 0) {
                    _creatorParameters[ix] = _findMissing(props[ix]);
                }
            }
        } else {
            final int len = _creatorParameters.length;
            for (int ix = 0; (ix = _paramsSeenBig.nextClearBit(ix)) < len; ++ix) {
                _creatorParameters[ix] = _findMissing(props[ix]);
            }
        }
    }
    if (_context.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
        for (int ix = 0; ix < props.length; ++ix) {
            if (_creatorParameters[ix] == null) {
                SettableBeanProperty prop = props[ix];
                _context.reportInputMismatch(prop.getType(), "Null value for creator property '%s' (index %d); DeserializationFeature.FAIL_ON_NULL_FOR_CREATOR_PARAMETERS enabled", prop.getName(), props[ix].getCreatorIndex());
            }
        }
    }
    return _creatorParameters;
}
Also used : SettableBeanProperty(com.fasterxml.jackson.databind.deser.SettableBeanProperty)

Aggregations

SettableBeanProperty (com.fasterxml.jackson.databind.deser.SettableBeanProperty)21 PropertyValueBuffer (com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer)2 JsonToken (com.fasterxml.jackson.core.JsonToken)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 BeanPropertyMap (com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap)1 ObjectIdValueProperty (com.fasterxml.jackson.databind.deser.impl.ObjectIdValueProperty)1 PropertyBasedCreator (com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator)1 ReadableObjectId (com.fasterxml.jackson.databind.deser.impl.ReadableObjectId)1 TokenBuffer (com.fasterxml.jackson.databind.util.TokenBuffer)1 IOException (java.io.IOException)1