Example usage for com.fasterxml.jackson.databind.deser.impl ExternalTypeHandler handlePropertyValue

List of usage examples for com.fasterxml.jackson.databind.deser.impl ExternalTypeHandler handlePropertyValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.deser.impl ExternalTypeHandler handlePropertyValue.

Prototype

public boolean handlePropertyValue(JsonParser paramJsonParser,
            DeserializationContext paramDeserializationContext, String paramString, Object paramObject) 

Source Link

Usage

From source file:com.github.shyiko.jackson.module.advice.AdvisedBeanDeserializer.java

protected Object deserializeUsingPropertyBasedWithExternalTypeId(JsonParser jp, DeserializationContext ctxt)
        throws IOException {
    final ExternalTypeHandler ext = _externalTypeIdHandler.start();
    final PropertyBasedCreator creator = _propertyBasedCreator;
    PropertyValueBuffer buffer = creator.startBuilding(jp, ctxt, _objectIdReader);

    TokenBuffer tokens = new TokenBuffer(jp);
    tokens.writeStartObject();/*from  w w w.  j  av  a 2 s . c  o  m*/

    JsonToken t = jp.getCurrentToken();
    for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
        String propName = jp.getCurrentName();
        jp.nextToken(); // to point to value
        // creator property?
        SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
        if (creatorProp != null) {
            // first: let's check to see if this might be part of value with external type id:
            if (!ext.handlePropertyValue(jp, ctxt, propName, buffer)) {
                // Last creator property to set?
                Object value = creatorProp.deserialize(jp, ctxt);
                if (buffer.assignParameter(creatorProp.getCreatorIndex(), value)) {
                    t = jp.nextToken(); // to move to following FIELD_NAME/END_OBJECT
                    Object bean;
                    try {
                        bean = creator.build(ctxt, buffer);
                    } catch (Exception e) {
                        wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
                        continue; // never gets here
                    }
                    // if so, need to copy all remaining tokens into buffer
                    while (t == JsonToken.FIELD_NAME) {
                        jp.nextToken(); // to skip name
                        tokens.copyCurrentStructure(jp);
                        t = jp.nextToken();
                    }
                    if (bean.getClass() != _beanType.getRawClass()) {
                        // !!! 08-Jul-2011, tatu: Could probably support; but for now
                        //   it's too complicated, so bail out
                        throw ctxt
                                .mappingException("Can not create polymorphic instances with unwrapped values");
                    }
                    return ext.complete(jp, 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(jp, ctxt));
            continue;
        }
        // external type id (or property that depends on it)?
        if (ext.handlePropertyValue(jp, ctxt, propName, null)) {
            continue;
        }
        /* As per [JACKSON-313], things marked as ignorable should not be
         * passed to any setter
         */
        if (_ignorableProps != null && _ignorableProps.contains(propName)) {
            handleIgnoredProperty(jp, ctxt, handledType(), propName);
            continue;
        }
        // "any property"?
        if (_anySetter != null) {
            buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(jp, ctxt));
        }
    }

    // We hit END_OBJECT; resolve the pieces:
    try {
        return ext.complete(jp, ctxt, buffer, creator);
    } catch (Exception e) {
        wrapInstantiationProblem(e, ctxt);
        return null; // never gets here
    }
}

From source file:com.github.shyiko.jackson.module.advice.AdvisedBeanDeserializer.java

protected Object deserializeWithExternalTypeId(JsonParser jp, DeserializationContext ctxt, Object bean)
        throws IOException {
    final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
    final ExternalTypeHandler ext = _externalTypeIdHandler.start();
    beanDeserializerAdvice.before(bean, jp, ctxt);
    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        String propName = jp.getCurrentName();
        jp.nextToken();/*www.  j a v a 2s  .co m*/

        if (beanDeserializerAdvice.intercept(bean, propName, jp, ctxt)) {
            continue;
        }

        SettableBeanProperty prop = _beanProperties.find(propName);
        if (prop != null) { // normal case
            // [JACKSON-831]: may have property AND be used as external type id:
            if (jp.getCurrentToken().isScalarValue()) {
                ext.handleTypePropertyValue(jp, ctxt, propName, bean);
            }
            if (activeView != null && !prop.visibleInView(activeView)) {
                jp.skipChildren();
                continue;
            }
            try {
                prop.deserializeAndSet(jp, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
            continue;
        }
        // ignorable things should be ignored
        if (_ignorableProps != null && _ignorableProps.contains(propName)) {
            handleIgnoredProperty(jp, ctxt, bean, propName);
            continue;
        }
        // but others are likely to be part of external type id thingy...
        if (ext.handlePropertyValue(jp, ctxt, propName, bean)) {
            continue;
        }
        // if not, the usual fallback handling:
        if (_anySetter != null) {
            try {
                _anySetter.deserializeAndSet(jp, ctxt, bean, propName);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
            continue;
        }
        // Unknown: let's call handler method
        handleUnknownProperty(jp, ctxt, bean, propName);
    }
    // and when we get this far, let's try finalizing the deal:
    ext.complete(jp, ctxt, bean);
    beanDeserializerAdvice.after(bean, jp, ctxt);
    return bean;
}