Search in sources :

Example 1 with PropertyValueBuffer

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

the class EnumMapDeserializer method _deserializeUsingProperties.

public EnumMap<?, ?> _deserializeUsingProperties(JsonParser p, DeserializationContext ctxt) throws IOException {
    final PropertyBasedCreator creator = _propertyBasedCreator;
    // null -> no ObjectIdReader for EnumMaps
    PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, null);
    String keyName;
    if (p.isExpectedStartObjectToken()) {
        keyName = p.nextFieldName();
    } else if (p.hasToken(JsonToken.FIELD_NAME)) {
        keyName = p.getCurrentName();
    } else {
        keyName = null;
    }
    for (; keyName != null; keyName = p.nextFieldName()) {
        // to get to value
        JsonToken t = p.nextToken();
        // creator property?
        SettableBeanProperty prop = creator.findCreatorProperty(keyName);
        if (prop != null) {
            // Last property to set?
            if (buffer.assignParameter(prop, prop.deserialize(p, ctxt))) {
                EnumMap<?, ?> result;
                try {
                    result = (EnumMap<?, ?>) creator.build(ctxt, buffer);
                } catch (Exception e) {
                    return wrapAndThrow(e, _containerType.getRawClass(), keyName);
                }
                return deserialize(p, ctxt, result);
            }
            continue;
        }
        // other property? needs buffering
        // but we need to let key deserializer handle it separately, nonetheless
        Enum<?> key = (Enum<?>) _keyDeserializer.deserializeKey(keyName, ctxt);
        if (key == null) {
            if (!ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) {
                return (EnumMap<?, ?>) ctxt.handleWeirdStringValue(_enumClass, keyName, "value not one of declared Enum instance names for %s", _containerType.getKeyType());
            }
            // 24-Mar-2012, tatu: Null won't work as a key anyway, so let's
            //  just skip the entry then. But we must skip the value as well, if so.
            p.nextToken();
            p.skipChildren();
            continue;
        }
        Object value;
        try {
            if (t == JsonToken.VALUE_NULL) {
                if (_skipNullValues) {
                    continue;
                }
                value = _nullProvider.getNullValue(ctxt);
            } else if (_valueTypeDeserializer == null) {
                value = _valueDeserializer.deserialize(p, ctxt);
            } else {
                value = _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
            }
        } catch (Exception e) {
            wrapAndThrow(e, _containerType.getRawClass(), keyName);
            return null;
        }
        buffer.bufferMapProperty(key, value);
    }
    // if so, can just construct and leave...
    try {
        return (EnumMap<?, ?>) creator.build(ctxt, buffer);
    } catch (Exception e) {
        wrapAndThrow(e, _containerType.getRawClass(), keyName);
        return null;
    }
}
Also used : PropertyValueBuffer(com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer) SettableBeanProperty(com.fasterxml.jackson.databind.deser.SettableBeanProperty) PropertyBasedCreator(com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator) IOException(java.io.IOException)

Example 2 with PropertyValueBuffer

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

the class FactoryBasedEnumDeserializer method deserializeEnumUsingPropertyBased.

// Method to deserialize the Enum using property based methodology
protected Object deserializeEnumUsingPropertyBased(final JsonParser p, final DeserializationContext ctxt, final PropertyBasedCreator creator) throws IOException {
    PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, null);
    JsonToken t = p.getCurrentToken();
    for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
        String propName = p.getCurrentName();
        // to point to value
        p.nextToken();
        SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
        if (creatorProp != null) {
            buffer.assignParameter(creatorProp, _deserializeWithErrorWrapping(p, ctxt, creatorProp));
            continue;
        }
        if (buffer.readIdProperty(propName)) {
            continue;
        }
    }
    return creator.build(ctxt, buffer);
}
Also used : PropertyValueBuffer(com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer) SettableBeanProperty(com.fasterxml.jackson.databind.deser.SettableBeanProperty) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 3 with PropertyValueBuffer

use of com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer 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

PropertyValueBuffer (com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer)3 SettableBeanProperty (com.fasterxml.jackson.databind.deser.SettableBeanProperty)2 PropertyBasedCreator (com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator)2 IOException (java.io.IOException)2 JsonToken (com.fasterxml.jackson.core.JsonToken)1 TypeDeserializer (com.fasterxml.jackson.databind.jsontype.TypeDeserializer)1