Example usage for com.fasterxml.jackson.databind.deser SettableBeanProperty deserializeAndSet

List of usage examples for com.fasterxml.jackson.databind.deser SettableBeanProperty deserializeAndSet

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.deser SettableBeanProperty deserializeAndSet.

Prototype

public abstract void deserializeAndSet(JsonParser paramJsonParser,
            DeserializationContext paramDeserializationContext, Object paramObject);

Source Link

Usage

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

/**
 * Streamlined version that is only used when no "special"
 * features are enabled.//from   ww  w .  jav a 2s.com
 */
private Object vanillaDeserialize(JsonParser jp, DeserializationContext ctxt, JsonToken t) throws IOException {
    final Object bean = _valueInstantiator.createUsingDefault(ctxt);
    beanDeserializerAdvice.before(bean, jp, ctxt);
    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        String propName = jp.getCurrentName();
        // Skip field name:
        jp.nextToken();

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

        SettableBeanProperty prop = _beanProperties.find(propName);
        if (prop != null) { // normal case
            try {
                prop.deserializeAndSet(jp, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
        } else {
            handleUnknownVanilla(jp, ctxt, bean, propName);
        }
    }
    beanDeserializerAdvice.after(bean, jp, ctxt);
    return bean;
}

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

/**
 * Secondary deserialization method, called in cases where POJO
 * instance is created as part of deserialization, potentially
 * after collecting some or all of the properties to set.
 *///from www. j a va2 s  .  c  o m
@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt, Object bean) throws IOException {
    if (_injectables != null) {
        injectValues(ctxt, bean);
    }
    if (_unwrappedPropertyHandler != null) {
        return deserializeWithUnwrapped(jp, ctxt, bean);
    }
    if (_externalTypeIdHandler != null) {
        return deserializeWithExternalTypeId(jp, ctxt, bean);
    }
    JsonToken t = jp.getCurrentToken();
    // 23-Mar-2010, tatu: In some cases, we start with full JSON object too...
    if (t == JsonToken.START_OBJECT) {
        t = jp.nextToken();
    }
    if (_needViewProcesing) {
        Class<?> view = ctxt.getActiveView();
        if (view != null) {
            return deserializeWithView(jp, ctxt, bean, view);
        }
    }
    beanDeserializerAdvice.before(bean, jp, ctxt);
    for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
        String propName = jp.getCurrentName();
        // Skip field name:
        jp.nextToken();

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

        SettableBeanProperty prop = _beanProperties.find(propName);

        if (prop != null) { // normal case
            try {
                prop.deserializeAndSet(jp, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
            continue;
        }
        handleUnknownVanilla(jp, ctxt, bean, propName);
    }
    beanDeserializerAdvice.after(bean, jp, ctxt);
    return bean;
}

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

/**
 * General version used when handling needs more advanced
 * features./*from  w ww. j ava2  s .  co m*/
 */
@Override
public Object deserializeFromObject(JsonParser jp, DeserializationContext ctxt) throws IOException {
    if (_nonStandardCreation) {
        if (_unwrappedPropertyHandler != null) {
            return deserializeWithUnwrapped(jp, ctxt);
        }
        if (_externalTypeIdHandler != null) {
            return deserializeWithExternalTypeId(jp, ctxt);
        }
        return deserializeFromObjectUsingNonDefault(jp, ctxt);
    }
    final Object bean = _valueInstantiator.createUsingDefault(ctxt);
    if (jp.canReadObjectId()) {
        Object id = jp.getObjectId();
        if (id != null) {
            _handleTypedObjectId(jp, ctxt, bean, id);
        }
    }
    if (_injectables != null) {
        injectValues(ctxt, bean);
    }
    if (_needViewProcesing) {
        Class<?> view = ctxt.getActiveView();
        if (view != null) {
            return deserializeWithView(jp, ctxt, bean, view);
        }
    }
    beanDeserializerAdvice.before(bean, jp, ctxt);
    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        String propName = jp.getCurrentName();
        // Skip field name:
        jp.nextToken();

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

        SettableBeanProperty prop = _beanProperties.find(propName);
        if (prop != null) { // normal case
            try {
                prop.deserializeAndSet(jp, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
            continue;
        }
        handleUnknownVanilla(jp, ctxt, bean, propName);
    }
    beanDeserializerAdvice.after(bean, jp, ctxt);
    return bean;
}

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

protected Object deserializeWithView(JsonParser jp, DeserializationContext ctxt, Object bean,
        Class<?> activeView) throws IOException {
    beanDeserializerAdvice.before(bean, jp, ctxt);
    JsonToken t = jp.getCurrentToken();/*  w  w  w .j  av  a2 s.com*/
    for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
        String propName = jp.getCurrentName();
        // Skip field name:
        jp.nextToken();

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

        SettableBeanProperty prop = _beanProperties.find(propName);
        if (prop != null) {
            if (!prop.visibleInView(activeView)) {
                jp.skipChildren();
                continue;
            }
            try {
                prop.deserializeAndSet(jp, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
            continue;
        }
        handleUnknownVanilla(jp, ctxt, bean, propName);
    }
    beanDeserializerAdvice.after(bean, jp, ctxt);
    return bean;
}

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

/**
 * Method called when there are declared "unwrapped" properties
 * which need special handling/* www. j av  a2s. c  o m*/
 */
@SuppressWarnings("resource")
protected Object deserializeWithUnwrapped(JsonParser jp, DeserializationContext ctxt) throws IOException {
    if (_delegateDeserializer != null) {
        return _valueInstantiator.createUsingDelegate(ctxt, _delegateDeserializer.deserialize(jp, ctxt));
    }
    if (_propertyBasedCreator != null) {
        return deserializeUsingPropertyBasedWithUnwrapped(jp, ctxt);
    }
    TokenBuffer tokens = new TokenBuffer(jp);
    tokens.writeStartObject();
    final Object bean = _valueInstantiator.createUsingDefault(ctxt);

    if (_injectables != null) {
        injectValues(ctxt, bean);
    }
    final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
    beanDeserializerAdvice.before(bean, jp, ctxt);
    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
        String propName = jp.getCurrentName();
        jp.nextToken();

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

        SettableBeanProperty prop = _beanProperties.find(propName);
        if (prop != null) { // normal case
            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 should be passed to unwrapped property deserializers
        tokens.writeFieldName(propName);
        tokens.copyCurrentStructure(jp);
        // how about any setter? We'll get copies but...
        if (_anySetter != null) {
            try {
                _anySetter.deserializeAndSet(jp, ctxt, bean, propName);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
            continue;
        }
    }
    tokens.writeEndObject();
    _unwrappedPropertyHandler.processUnwrapped(jp, ctxt, bean, tokens);
    beanDeserializerAdvice.after(bean, jp, ctxt);
    return bean;
}

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

@SuppressWarnings("resource")
protected Object deserializeWithUnwrapped(JsonParser jp, DeserializationContext ctxt, Object bean)
        throws IOException {
    JsonToken t = jp.getCurrentToken();//from w  w  w . ja  v  a 2  s  . c om
    if (t == JsonToken.START_OBJECT) {
        t = jp.nextToken();
    }
    TokenBuffer tokens = new TokenBuffer(jp);
    tokens.writeStartObject();
    final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
    beanDeserializerAdvice.before(bean, jp, ctxt);
    for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
        String propName = jp.getCurrentName();

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

        SettableBeanProperty prop = _beanProperties.find(propName);
        jp.nextToken();
        if (prop != null) { // normal case
            if (activeView != null && !prop.visibleInView(activeView)) {
                jp.skipChildren();
                continue;
            }
            try {
                prop.deserializeAndSet(jp, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, propName, ctxt);
            }
            continue;
        }
        if (_ignorableProps != null && _ignorableProps.contains(propName)) {
            handleIgnoredProperty(jp, ctxt, bean, propName);
            continue;
        }
        // but... others should be passed to unwrapped property deserializers
        tokens.writeFieldName(propName);
        tokens.copyCurrentStructure(jp);
        // how about any setter? We'll get copies but...
        if (_anySetter != null) {
            _anySetter.deserializeAndSet(jp, ctxt, bean, propName);
        }
    }
    tokens.writeEndObject();
    _unwrappedPropertyHandler.processUnwrapped(jp, ctxt, bean, tokens);
    beanDeserializerAdvice.after(bean, jp, ctxt);
    return bean;
}

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 a2 s . 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;
}