Search in sources :

Example 16 with TokenBuffer

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

the class JsonParserReader method nextTokenBuffer.

/**
 * Reads current value including objects and array as effiecient token buffer.
 * Use of Jackson's own mechanisms is important to preserve custom elements
 * such as special embedded objects in BSON or other data formats.
 * @return {@link TokenBuffer}
 * @throws IOException if error occured
 */
public final TokenBuffer nextTokenBuffer() throws IOException {
    TokenBuffer buffer = new TokenBuffer(parser);
    // if token is consumed, but undelying parser is still sitting on this token, we move forward
    requirePeek();
    buffer.copyCurrentStructure(parser);
    // when we will return to reading from reader, state will be cleared and nextToken after
    clearPeek();
    return buffer;
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer)

Example 17 with TokenBuffer

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

the class BeanDeserializer method _deserializeUsingPropertyBased.

/**
     * Method called to deserialize bean using "property-based creator":
     * this means that a non-default constructor or factory method is
     * called, and then possibly other setters. The trick is that
     * values for creator method need to be buffered, first; and
     * due to non-guaranteed ordering possibly some other properties
     * as well.
     */
@Override
@SuppressWarnings("resource")
protected Object _deserializeUsingPropertyBased(final JsonParser p, final DeserializationContext ctxt) throws IOException {
    final PropertyBasedCreator creator = _propertyBasedCreator;
    PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, _objectIdReader);
    TokenBuffer unknown = null;
    final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
    JsonToken t = p.getCurrentToken();
    List<BeanReferring> referrings = null;
    for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
        String propName = p.getCurrentName();
        // to point to value
        p.nextToken();
        // Object Id property?
        if (buffer.readIdProperty(propName)) {
            continue;
        }
        // creator property?
        SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
        if (creatorProp != null) {
            // Last creator property to set?
            Object value;
            if ((activeView != null) && !creatorProp.visibleInView(activeView)) {
                p.skipChildren();
                continue;
            }
            value = _deserializeWithErrorWrapping(p, ctxt, creatorProp);
            if (buffer.assignParameter(creatorProp, value)) {
                // to move to following FIELD_NAME/END_OBJECT
                p.nextToken();
                Object bean;
                try {
                    bean = creator.build(ctxt, buffer);
                } catch (Exception e) {
                    bean = wrapInstantiationProblem(e, ctxt);
                }
                if (bean == null) {
                    return ctxt.handleInstantiationProblem(handledType(), null, _creatorReturnedNullException());
                }
                // [databind#631]: Assign current value, to be accessible by custom serializers
                p.setCurrentValue(bean);
                //  polymorphic?
                if (bean.getClass() != _beanType.getRawClass()) {
                    return handlePolymorphic(p, ctxt, bean, unknown);
                }
                if (unknown != null) {
                    // nope, just extra unknown stuff...
                    bean = handleUnknownProperties(ctxt, bean, unknown);
                }
                // or just clean?
                return deserialize(p, ctxt, bean);
            }
            continue;
        }
        // regular property? needs buffering
        SettableBeanProperty prop = _beanProperties.find(propName);
        if (prop != null) {
            try {
                buffer.bufferProperty(prop, _deserializeWithErrorWrapping(p, ctxt, prop));
            } catch (UnresolvedForwardReference reference) {
                // 14-Jun-2016, tatu: As per [databind#1261], looks like we need additional
                //    handling of forward references here. Not exactly sure why existing
                //    facilities did not cover, but this does appear to solve the problem
                BeanReferring referring = handleUnresolvedReference(ctxt, prop, buffer, reference);
                if (referrings == null) {
                    referrings = new ArrayList<BeanReferring>();
                }
                referrings.add(referring);
            }
            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) {
            try {
                buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
            } catch (Exception e) {
                wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
            }
            continue;
        }
        // Ok then, let's collect the whole field; name and value
        if (unknown == null) {
            unknown = new TokenBuffer(p, ctxt);
        }
        unknown.writeFieldName(propName);
        unknown.copyCurrentStructure(p);
    }
    // We hit END_OBJECT, so:
    Object bean;
    try {
        bean = creator.build(ctxt, buffer);
    } catch (Exception e) {
        wrapInstantiationProblem(e, ctxt);
        // never gets here
        bean = null;
    }
    if (referrings != null) {
        for (BeanReferring referring : referrings) {
            referring.setBean(bean);
        }
    }
    if (unknown != null) {
        // polymorphic?
        if (bean.getClass() != _beanType.getRawClass()) {
            return handlePolymorphic(null, ctxt, bean, unknown);
        }
        // no, just some extra unknown properties
        return handleUnknownProperties(ctxt, bean, unknown);
    }
    return bean;
}
Also used : IOException(java.io.IOException) TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer)

Example 18 with TokenBuffer

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

the class BeanDeserializer method deserializeWithUnwrapped.

@SuppressWarnings("resource")
protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext ctxt, Object bean) throws IOException {
    JsonToken t = p.getCurrentToken();
    if (t == JsonToken.START_OBJECT) {
        t = p.nextToken();
    }
    TokenBuffer tokens = new TokenBuffer(p, ctxt);
    tokens.writeStartObject();
    final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
    for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
        String propName = p.getCurrentName();
        SettableBeanProperty prop = _beanProperties.find(propName);
        p.nextToken();
        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;
        }
        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);
        } else {
            // 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);
            }
            continue;
        }
    }
    tokens.writeEndObject();
    _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
    return bean;
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) IOException(java.io.IOException)

Example 19 with TokenBuffer

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

the class BuilderBasedDeserializer method deserializeWithUnwrapped.

@SuppressWarnings("resource")
protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext ctxt, Object bean) throws IOException {
    JsonToken t = p.getCurrentToken();
    if (t == JsonToken.START_OBJECT) {
        t = p.nextToken();
    }
    TokenBuffer tokens = new TokenBuffer(p, ctxt);
    tokens.writeStartObject();
    final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
    for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
        String propName = p.getCurrentName();
        SettableBeanProperty prop = _beanProperties.find(propName);
        p.nextToken();
        if (prop != null) {
            // normal case
            if (activeView != null && !prop.visibleInView(activeView)) {
                p.skipChildren();
                continue;
            }
            try {
                bean = prop.deserializeSetAndReturn(p, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
            continue;
        }
        if (_ignorableProps != null && _ignorableProps.contains(propName)) {
            handleIgnoredProperty(p, ctxt, bean, propName);
            continue;
        }
        // but... others should be passed to unwrapped property deserializers
        tokens.writeFieldName(propName);
        tokens.copyCurrentStructure(p);
        // how about any setter? We'll get copies but...
        if (_anySetter != null) {
            _anySetter.deserializeAndSet(p, ctxt, bean, propName);
        }
    }
    tokens.writeEndObject();
    _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
    return bean;
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) IOException(java.io.IOException)

Example 20 with TokenBuffer

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

the class BuilderBasedDeserializer method deserializeUsingPropertyBasedWithUnwrapped.

@SuppressWarnings("resource")
protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p, DeserializationContext ctxt) throws IOException {
    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) {
            // Last creator property to set?
            if (buffer.assignParameter(creatorProp, creatorProp.deserialize(p, ctxt))) {
                // 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();
                }
                tokens.writeEndObject();
                if (bean.getClass() != _beanType.getRawClass()) {
                    // !!! 08-Jul-2011, tatu: Could probably support; but for now
                    //   it's too complicated, so bail out
                    ctxt.reportInputMismatch(creatorProp, "Can not create polymorphic instances with unwrapped values");
                    return null;
                }
                return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
            }
            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;
        }
        if (_ignorableProps != null && _ignorableProps.contains(propName)) {
            handleIgnoredProperty(p, ctxt, handledType(), propName);
            continue;
        }
        tokens.writeFieldName(propName);
        tokens.copyCurrentStructure(p);
        // "any property"?
        if (_anySetter != null) {
            buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
        }
    }
    // We hit END_OBJECT, so:
    Object bean;
    // !!! 15-Feb-2012, tatu: Need to modify creator to use Builder!
    try {
        bean = creator.build(ctxt, buffer);
    } catch (Exception e) {
        return wrapInstantiationProblem(e, ctxt);
    }
    return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) IOException(java.io.IOException)

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