Search in sources :

Example 6 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project jackson-databind by FasterXML.

the class BeanDeserializer method deserializeWithUnwrapped.

/*
    /**********************************************************
    /* Handling for cases where we have "unwrapped" values
    /**********************************************************
     */
/**
     * Method called when there are declared "unwrapped" properties
     * which need special handling
     */
@SuppressWarnings("resource")
protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (_delegateDeserializer != null) {
        return _valueInstantiator.createUsingDelegate(ctxt, _delegateDeserializer.deserialize(p, ctxt));
    }
    if (_propertyBasedCreator != null) {
        return deserializeUsingPropertyBasedWithUnwrapped(p, ctxt);
    }
    TokenBuffer tokens = new TokenBuffer(p, ctxt);
    tokens.writeStartObject();
    final Object bean = _valueInstantiator.createUsingDefault(ctxt);
    // [databind#631]: Assign current value, to be accessible by custom serializers
    p.setCurrentValue(bean);
    if (_injectables != null) {
        injectValues(ctxt, bean);
    }
    final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
    String propName = p.hasTokenId(JsonTokenId.ID_FIELD_NAME) ? p.getCurrentName() : null;
    for (; propName != null; propName = p.nextFieldName()) {
        p.nextToken();
        SettableBeanProperty prop = _beanProperties.find(propName);
        if (prop != null) {
            // normal case
            if ((activeView != null) && !prop.visibleInView(activeView)) {
                p.skipChildren();
                continue;
            }
            try {
                prop.deserializeAndSet(p, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
            continue;
        }
        // Things marked as ignorable should not be passed to any setter
        if (_ignorableProps != null && _ignorableProps.contains(propName)) {
            handleIgnoredProperty(p, ctxt, bean, propName);
            continue;
        }
        // how about any setter? We'll get copies but...
        if (_anySetter == null) {
            // but... others should be passed to unwrapped property deserializers
            tokens.writeFieldName(propName);
            tokens.copyCurrentStructure(p);
            continue;
        }
        // Need to copy to a separate buffer first
        TokenBuffer b2 = TokenBuffer.asCopyOfValue(p);
        tokens.writeFieldName(propName);
        tokens.append(b2);
        try {
            _anySetter.deserializeAndSet(b2.asParserOnFirstToken(), ctxt, bean, propName);
        } catch (Exception e) {
            wrapAndThrow(e, bean, propName, ctxt);
        }
    }
    tokens.writeEndObject();
    _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
    return bean;
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) IOException(java.io.IOException)

Example 7 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project jackson-databind by FasterXML.

the class BeanDeserializer method deserializeUsingPropertyBasedWithExternalTypeId.

@SuppressWarnings("resource")
protected Object deserializeUsingPropertyBasedWithExternalTypeId(JsonParser p, DeserializationContext ctxt) throws IOException {
    final ExternalTypeHandler ext = _externalTypeIdHandler.start();
    final PropertyBasedCreator creator = _propertyBasedCreator;
    PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, _objectIdReader);
    TokenBuffer tokens = new TokenBuffer(p, ctxt);
    tokens.writeStartObject();
    JsonToken t = p.getCurrentToken();
    for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
        String propName = p.getCurrentName();
        // to point to value
        p.nextToken();
        // creator property?
        SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
        if (creatorProp != null) {
            //   since it is not the bean
            if (ext.handlePropertyValue(p, ctxt, propName, null)) {
                ;
            } else {
                // Last creator property to set?
                if (buffer.assignParameter(creatorProp, _deserializeWithErrorWrapping(p, ctxt, creatorProp))) {
                    // to move to following FIELD_NAME/END_OBJECT
                    t = p.nextToken();
                    Object bean;
                    try {
                        bean = creator.build(ctxt, buffer);
                    } catch (Exception e) {
                        wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
                        // never gets here
                        continue;
                    }
                    // if so, need to copy all remaining tokens into buffer
                    while (t == JsonToken.FIELD_NAME) {
                        // to skip name
                        p.nextToken();
                        tokens.copyCurrentStructure(p);
                        t = p.nextToken();
                    }
                    if (bean.getClass() != _beanType.getRawClass()) {
                        //   it's too complicated, so bail out
                        return ctxt.reportBadDefinition(_beanType, String.format("Can not create polymorphic instances with external type ids (%s -> %s)", _beanType, bean.getClass()));
                    }
                    return ext.complete(p, ctxt, bean);
                }
            }
            continue;
        }
        // Object Id property?
        if (buffer.readIdProperty(propName)) {
            continue;
        }
        // regular property? needs buffering
        SettableBeanProperty prop = _beanProperties.find(propName);
        if (prop != null) {
            buffer.bufferProperty(prop, prop.deserialize(p, ctxt));
            continue;
        }
        // external type id (or property that depends on it)?
        if (ext.handlePropertyValue(p, ctxt, propName, null)) {
            continue;
        }
        // Things marked as ignorable should not be passed to any setter
        if (_ignorableProps != null && _ignorableProps.contains(propName)) {
            handleIgnoredProperty(p, ctxt, handledType(), propName);
            continue;
        }
        // "any property"?
        if (_anySetter != null) {
            buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
        }
    }
    // We hit END_OBJECT; resolve the pieces:
    try {
        return ext.complete(p, ctxt, buffer, creator);
    } catch (Exception e) {
        return wrapInstantiationProblem(e, ctxt);
    }
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) IOException(java.io.IOException)

Example 8 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project jackson-databind by FasterXML.

the class AsPropertyTypeDeserializer method _deserializeTypedForId.

@SuppressWarnings("resource")
protected Object _deserializeTypedForId(JsonParser p, DeserializationContext ctxt, TokenBuffer tb) throws IOException {
    String typeId = p.getText();
    JsonDeserializer<Object> deser = _findDeserializer(ctxt, typeId);
    if (_typeIdVisible) {
        // need to merge id back in JSON input?
        if (tb == null) {
            tb = new TokenBuffer(p, ctxt);
        }
        tb.writeFieldName(p.getCurrentName());
        tb.writeString(typeId);
    }
    if (tb != null) {
        // need to put back skipped properties?
        // 02-Jul-2016, tatu: Depending on for JsonParserSequence is initialized it may
        //   try to access current token; ensure there isn't one
        p.clearCurrentToken();
        p = JsonParserSequence.createFlattened(false, tb.asParser(p), p);
    }
    // Must point to the next value; tb had no current, jp pointed to VALUE_STRING:
    // to skip past String value
    p.nextToken();
    // deserializer should take care of closing END_OBJECT as well
    return deser.deserialize(p, ctxt);
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer)

Example 9 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project jackson-databind by FasterXML.

the class ExternalTypeHandler method _deserialize.

@SuppressWarnings("resource")
protected final Object _deserialize(JsonParser p, DeserializationContext ctxt, int index, String typeId) throws IOException {
    JsonParser p2 = _tokens[index].asParser(p);
    JsonToken t = p2.nextToken();
    // 29-Sep-2015, tatu: As per [databind#942], nulls need special support
    if (t == JsonToken.VALUE_NULL) {
        return null;
    }
    TokenBuffer merged = new TokenBuffer(p, ctxt);
    merged.writeStartArray();
    merged.writeString(typeId);
    merged.copyCurrentStructure(p2);
    merged.writeEndArray();
    // needs to point to START_OBJECT (or whatever first token is)
    JsonParser mp = merged.asParser(p);
    mp.nextToken();
    return _properties[index].getProperty().deserialize(mp, ctxt);
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer)

Example 10 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project jackson-databind by FasterXML.

the class ExternalTypeHandler method _deserializeAndSet.

@SuppressWarnings("resource")
protected final void _deserializeAndSet(JsonParser p, DeserializationContext ctxt, Object bean, int index, String typeId) throws IOException {
    /* Ok: time to mix type id, value; and we will actually use "wrapper-array"
         * style to ensure we can handle all kinds of JSON constructs.
         */
    JsonParser p2 = _tokens[index].asParser(p);
    JsonToken t = p2.nextToken();
    // 29-Sep-2015, tatu: As per [databind#942], nulls need special support
    if (t == JsonToken.VALUE_NULL) {
        _properties[index].getProperty().set(bean, null);
        return;
    }
    TokenBuffer merged = new TokenBuffer(p, ctxt);
    merged.writeStartArray();
    merged.writeString(typeId);
    merged.copyCurrentStructure(p2);
    merged.writeEndArray();
    // needs to point to START_OBJECT (or whatever first token is)
    JsonParser mp = merged.asParser(p);
    mp.nextToken();
    _properties[index].getProperty().deserializeAndSet(mp, ctxt, bean);
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer)

Aggregations

TokenBuffer (com.fasterxml.jackson.databind.util.TokenBuffer)37 IOException (java.io.IOException)11 JsonParser (com.fasterxml.jackson.core.JsonParser)3 RexLiteral (org.apache.calcite.rex.RexLiteral)3 NlsString (org.apache.calcite.util.NlsString)3 ExtendedJsonOutput (org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput)3 JsonOutput (org.apache.drill.exec.vector.complex.fn.JsonOutput)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 SettableBeanProperty (com.fasterxml.jackson.databind.deser.SettableBeanProperty)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 UUID (java.util.UUID)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1