Search in sources :

Example 6 with TypeDeserializer

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

the class MapDeserializer 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 {
    KeyDeserializer keyDeser = _keyDeserializer;
    if (keyDeser == null) {
        keyDeser = ctxt.findKeyDeserializer(_containerType.getKeyType(), property);
    } else {
        if (keyDeser instanceof ContextualKeyDeserializer) {
            keyDeser = ((ContextualKeyDeserializer) keyDeser).createContextual(ctxt, property);
        }
    }
    JsonDeserializer<?> valueDeser = _valueDeserializer;
    // [databind#125]: May have a content converter
    if (property != null) {
        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);
    }
    TypeDeserializer vtd = _valueTypeDeserializer;
    if (vtd != null) {
        vtd = vtd.forProperty(property);
    }
    Set<String> ignored = _ignorableProperties;
    AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
    if (intr != null && property != null) {
        AnnotatedMember member = property.getMember();
        if (member != null) {
            JsonIgnoreProperties.Value ignorals = intr.findPropertyIgnorals(member);
            if (ignorals != null) {
                Set<String> ignoresToAdd = ignorals.findIgnoredForDeserialization();
                if (!ignoresToAdd.isEmpty()) {
                    ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
                    for (String str : ignoresToAdd) {
                        ignored.add(str);
                    }
                }
            }
        }
    }
    return withResolved(keyDeser, vtd, valueDeser, findContentNullProvider(ctxt, property, valueDeser), ignored);
}
Also used : AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties)

Example 7 with TypeDeserializer

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

the class MapDeserializer method _readAndBind.

/*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
protected final void _readAndBind(JsonParser p, DeserializationContext ctxt, Map<Object, Object> result) throws IOException {
    final KeyDeserializer keyDes = _keyDeserializer;
    final JsonDeserializer<Object> valueDes = _valueDeserializer;
    final TypeDeserializer typeDeser = _valueTypeDeserializer;
    MapReferringAccumulator referringAccumulator = null;
    boolean useObjectId = valueDes.getObjectIdReader() != null;
    if (useObjectId) {
        referringAccumulator = new MapReferringAccumulator(_containerType.getContentType().getRawClass(), result);
    }
    String keyStr;
    if (p.isExpectedStartObjectToken()) {
        keyStr = p.nextFieldName();
    } else {
        JsonToken t = p.getCurrentToken();
        if (t == JsonToken.END_OBJECT) {
            return;
        }
        if (t != JsonToken.FIELD_NAME) {
            ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
        }
        keyStr = p.getCurrentName();
    }
    for (; keyStr != null; keyStr = p.nextFieldName()) {
        Object key = keyDes.deserializeKey(keyStr, ctxt);
        // And then the value...
        JsonToken t = p.nextToken();
        if (_ignorableProperties != null && _ignorableProperties.contains(keyStr)) {
            p.skipChildren();
            continue;
        }
        try {
            // Note: must handle null explicitly here; value deserializers won't
            Object value;
            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);
            }
            if (useObjectId) {
                referringAccumulator.put(key, value);
            } else {
                result.put(key, value);
            }
        } catch (UnresolvedForwardReference reference) {
            handleUnresolvedReference(ctxt, referringAccumulator, key, reference);
        } catch (Exception e) {
            wrapAndThrow(e, result, keyStr);
        }
    }
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) IOException(java.io.IOException)

Example 8 with TypeDeserializer

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

the class MapEntryDeserializer method deserialize.

/*
    /**********************************************************
    /* JsonDeserializer API
    /**********************************************************
     */
@SuppressWarnings("unchecked")
@Override
public Map.Entry<Object, Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    // Ok: must point to START_OBJECT, FIELD_NAME or END_OBJECT
    JsonToken t = p.getCurrentToken();
    if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
        // slightly redundant (since String was passed above), but
        return _deserializeFromEmpty(p, ctxt);
    }
    if (t == JsonToken.START_OBJECT) {
        t = p.nextToken();
    }
    if (t != JsonToken.FIELD_NAME) {
        if (t == JsonToken.END_OBJECT) {
            return ctxt.reportInputMismatch(this, "Can not deserialize a Map.Entry out of empty JSON Object");
        }
        return (Map.Entry<Object, Object>) ctxt.handleUnexpectedToken(handledType(), p);
    }
    final KeyDeserializer keyDes = _keyDeserializer;
    final JsonDeserializer<Object> valueDes = _valueDeserializer;
    final TypeDeserializer typeDeser = _valueTypeDeserializer;
    final String keyStr = p.getCurrentName();
    Object key = keyDes.deserializeKey(keyStr, ctxt);
    Object value = null;
    // And then the value...
    t = p.nextToken();
    try {
        // Note: must handle null explicitly here; value deserializers won't
        if (t == JsonToken.VALUE_NULL) {
            value = valueDes.getNullValue(ctxt);
        } else if (typeDeser == null) {
            value = valueDes.deserialize(p, ctxt);
        } else {
            value = valueDes.deserializeWithType(p, ctxt, typeDeser);
        }
    } catch (Exception e) {
        wrapAndThrow(e, Map.Entry.class, keyStr);
    }
    // Close, but also verify that we reached the END_OBJECT
    t = p.nextToken();
    if (t != JsonToken.END_OBJECT) {
        if (t == JsonToken.FIELD_NAME) {
            // most likely
            ctxt.reportInputMismatch(this, "Problem binding JSON into Map.Entry: more than one entry in JSON (second field: '%s')", p.getCurrentName());
        } else {
            // how would this occur?
            ctxt.reportInputMismatch(this, "Problem binding JSON into Map.Entry: unexpected content after JSON Object entry: " + t);
        }
        return null;
    }
    return new AbstractMap.SimpleEntry<Object, Object>(key, value);
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) IOException(java.io.IOException)

Example 9 with TypeDeserializer

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

the class ObjectArrayDeserializer method deserialize.

/*
    /**********************************************************
    /* JsonDeserializer API
    /**********************************************************
     */
@Override
public Object[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    // Ok: must point to START_ARRAY (or equivalent)
    if (!p.isExpectedStartArrayToken()) {
        return handleNonArray(p, ctxt);
    }
    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    Object[] chunk = buffer.resetAndStart();
    int ix = 0;
    JsonToken t;
    final TypeDeserializer typeDeser = _elementTypeDeserializer;
    try {
        while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
            // Note: must handle null explicitly here; value deserializers won't
            Object value;
            if (t == JsonToken.VALUE_NULL) {
                if (_skipNullValues) {
                    continue;
                }
                value = _nullProvider.getNullValue(ctxt);
            } else if (typeDeser == null) {
                value = _elementDeserializer.deserialize(p, ctxt);
            } else {
                value = _elementDeserializer.deserializeWithType(p, ctxt, typeDeser);
            }
            if (ix >= chunk.length) {
                chunk = buffer.appendCompletedChunk(chunk);
                ix = 0;
            }
            chunk[ix++] = value;
        }
    } catch (Exception e) {
        throw JsonMappingException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
    }
    Object[] result;
    if (_untyped) {
        result = buffer.completeAndClearBuffer(chunk, ix);
    } else {
        result = buffer.completeAndClearBuffer(chunk, ix, _elementClass);
    }
    ctxt.returnObjectBuffer(buffer);
    return result;
}
Also used : ObjectBuffer(com.fasterxml.jackson.databind.util.ObjectBuffer) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) IOException(java.io.IOException)

Example 10 with TypeDeserializer

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

the class ObjectArrayDeserializer method deserialize.

// since 2.9
@Override
public Object[] deserialize(JsonParser p, DeserializationContext ctxt, Object[] intoValue) throws IOException {
    if (!p.isExpectedStartArrayToken()) {
        Object[] arr = handleNonArray(p, ctxt);
        if (arr == null) {
            return intoValue;
        }
        final int offset = intoValue.length;
        Object[] result = new Object[offset + arr.length];
        System.arraycopy(intoValue, 0, result, 0, offset);
        System.arraycopy(arr, 0, result, offset, arr.length);
        return result;
    }
    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    int ix = intoValue.length;
    Object[] chunk = buffer.resetAndStart(intoValue, ix);
    JsonToken t;
    final TypeDeserializer typeDeser = _elementTypeDeserializer;
    try {
        while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
            Object value;
            if (t == JsonToken.VALUE_NULL) {
                if (_skipNullValues) {
                    continue;
                }
                value = _nullProvider.getNullValue(ctxt);
            } else if (typeDeser == null) {
                value = _elementDeserializer.deserialize(p, ctxt);
            } else {
                value = _elementDeserializer.deserializeWithType(p, ctxt, typeDeser);
            }
            if (ix >= chunk.length) {
                chunk = buffer.appendCompletedChunk(chunk);
                ix = 0;
            }
            chunk[ix++] = value;
        }
    } catch (Exception e) {
        throw JsonMappingException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
    }
    Object[] result;
    if (_untyped) {
        result = buffer.completeAndClearBuffer(chunk, ix);
    } else {
        result = buffer.completeAndClearBuffer(chunk, ix, _elementClass);
    }
    ctxt.returnObjectBuffer(buffer);
    return result;
}
Also used : ObjectBuffer(com.fasterxml.jackson.databind.util.ObjectBuffer) 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