Example usage for com.fasterxml.jackson.databind DeserializationContext getActiveView

List of usage examples for com.fasterxml.jackson.databind DeserializationContext getActiveView

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind DeserializationContext getActiveView.

Prototype

public final Class<?> getActiveView() 

Source Link

Usage

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();//  w ww. j  av a2s  . c o  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;
}

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();//  w w w . j a  v a2 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

/**
 * Method called when there are declared "unwrapped" properties
 * which need special handling/* w  ww .ja  va  2  s .  com*/
 */
@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

/**
 * 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 w w w  .j  a  v a 2s.  c  om*/
@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 www. ja v a  2s  .  c o  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;
}