Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.impl;
  
  import java.util.HashSet;
  
Variant of com.fasterxml.jackson.databind.deser.BeanDeserializer used for handling deserialization of POJOs when serialized as JSON Arrays, instead of JSON Objects.

Since:
2.1
 
 public class BeanAsArrayDeserializer
     extends BeanDeserializerBase
 {
     private static final long serialVersionUID = 1L;

    
Deserializer we delegate operations that we can not handle.
 
     protected final BeanDeserializerBase _delegate;

    
Properties in order expected to be found in JSON array.
 
     protected final SettableBeanProperty[] _orderedProperties;
     
     /*
     /**********************************************************
     /* Life-cycle, construction, initialization
     /**********************************************************
      */

    
Main constructor used both for creating new instances (by com.fasterxml.jackson.databind.deser.BeanDeserializer.asArrayDeserializer()) and for creating copies with different delegate.
 
     public BeanAsArrayDeserializer(BeanDeserializerBase delegate,
             SettableBeanProperty[] ordered)
     {
         super(delegate);
          = delegate;
          = ordered;
     }
     
     @Override
     {
         /* We can't do much about this; could either replace _delegate
          * with unwrapping instance, or just replace this one. Latter seems
          * more sensible.
          */
         return .unwrappingDeserializer(unwrapper);
     }
 
     @Override
         return new BeanAsArrayDeserializer(.withObjectIdReader(oir),
                 );
     }
 
     @Override
     public BeanAsArrayDeserializer withIgnorableProperties(HashSet<StringignorableProps) {
         return new BeanAsArrayDeserializer(.withIgnorableProperties(ignorableProps),
                 );
     }
 
     @Override
     protected BeanDeserializerBase asArrayDeserializer() {
         return this;
     }
 
     /*
     /**********************************************************
     /* JsonDeserializer implementation
     /**********************************************************
      */
     
     @Override
     public Object deserialize(JsonParser jpDeserializationContext ctxt)
         throws IOExceptionJsonProcessingException
     {
         // Let's delegate just in case we got a JSON Object (could error out, alternatively?)
         if (jp.getCurrentToken() != .) {
             return _deserializeFromNonArray(jpctxt);
         }
         if (!) {
             return _deserializeNonVanilla(jpctxt);
         }
         final Object bean = .createUsingDefault(ctxt);
        final SettableBeanProperty[] props = ;
        int i = 0;
        final int propCount = props.length;
        while (true) {
            if (jp.nextToken() == .) {
                return bean;
            }
            if (i == propCount) {
                break;
            }
            SettableBeanProperty prop = props[i];
            if (prop != null) { // normal case
                try {
                    prop.deserializeAndSet(jpctxtbean);
                } catch (Exception e) {
                    wrapAndThrow(ebeanprop.getName(), ctxt);
                }
            } else { // just skip?
                jp.skipChildren();
            }
            ++i;
        }
        // Ok; extra fields? Let's fail, unless ignoring extra props is fine
        if (!) {
            throw ctxt.mappingException("Unexpected JSON values; expected at most "+propCount+" properties (in JSON Array)");
        }
        // otherwise, skip until end
        while (jp.nextToken() != .) {
            jp.skipChildren();
        }
        return bean;
    }
    @Override
    public Object deserialize(JsonParser jpDeserializationContext ctxtObject bean)
        throws IOExceptionJsonProcessingException
    {
        /* No good way to verify that we have an array... although could I guess
         * check via JsonParser. So let's assume everything is working fine, for now.
         */
        if ( != null) {
            injectValues(ctxtbean);
        }
        final SettableBeanProperty[] props = ;
        int i = 0;
        final int propCount = props.length;
        while (true) {
            if (jp.nextToken() == .) {
                return bean;
            }
            if (i == propCount) {
                break;
            }
            SettableBeanProperty prop = props[i];
            if (prop != null) { // normal case
                try {
                    prop.deserializeAndSet(jpctxtbean);
                } catch (Exception e) {
                    wrapAndThrow(ebeanprop.getName(), ctxt);
                }
            } else { // just skip?
                jp.skipChildren();
            }
            ++i;
        }
        
        // Ok; extra fields? Let's fail, unless ignoring extra props is fine
        if (!) {
            throw ctxt.mappingException("Unexpected JSON values; expected at most "+propCount+" properties (in JSON Array)");
        }
        // otherwise, skip until end
        while (jp.nextToken() != .) {
            jp.skipChildren();
        }
        return bean;
    }
    /*
    /**********************************************************
    /* Helper methods, non-standard creation
    /**********************************************************
     */

    
Alternate deserialization method that has to check many more configuration aspects than the "vanilla" processing.
        throws IOExceptionJsonProcessingException
    {
        if () {
            return _deserializeWithCreator(jpctxt);
        }
        final Object bean = .createUsingDefault(ctxt);
        if ( != null) {
            injectValues(ctxtbean);
        }
        Class<?> activeView =  ? ctxt.getActiveView() : null;
        final SettableBeanProperty[] props = ;
        int i = 0;
        final int propCount = props.length;
        while (true) {
            if (jp.nextToken() == .) {
                return bean;
            }
            if (i == propCount) {
                break;
            }
            SettableBeanProperty prop = props[i];
            ++i;
            if (prop != null) { // normal case
                if (activeView == null || prop.visibleInView(activeView)) {
                    try {
                        prop.deserializeAndSet(jpctxtbean);
                    } catch (Exception e) {
                        wrapAndThrow(ebeanprop.getName(), ctxt);
                    }
                    continue;
                }
            }
            // otherwise, skip it (view-filtered, no prop etc)
            jp.skipChildren();
        }
        // Ok; extra fields? Let's fail, unless ignoring extra props is fine
        if (!) {
            throw ctxt.mappingException("Unexpected JSON values; expected at most "+propCount+" properties (in JSON Array)");
        }
        // otherwise, skip until end
        while (jp.nextToken() != .) {
            jp.skipChildren();
        }
        return bean;
    }
    
        throws IOExceptionJsonProcessingException
    {        
        if ( != null) {
            return .createUsingDelegate(ctxt.deserialize(jpctxt));
        }
        if ( != null) {
            return _deserializeUsingPropertyBased(jpctxt);
        }
        // should only occur for abstract types...
        if (.isAbstract()) {
            throw JsonMappingException.from(jp"Can not instantiate abstract type "+
                    +" (need to add/enable type information?)");
        }
        throw JsonMappingException.from(jp"No suitable constructor found for type "
                ++": can not instantiate from JSON object (need to add/enable type information?)");
    }

    
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.
    protected final Object _deserializeUsingPropertyBased(final JsonParser jpfinal DeserializationContext ctxt)
        throws IOExceptionJsonProcessingException
    {
        final PropertyBasedCreator creator = ;
        PropertyValueBuffer buffer = creator.startBuilding(jpctxt);
        final SettableBeanProperty[] props = ;
        final int propCount = props.length;
        int i = 0;
        Object bean = null;
        
        for (; jp.nextToken() != .; ++i) {
            SettableBeanProperty prop = (i < propCount) ? props[i] : null;
            if (prop == null) { // we get null if there are extra elements; maybe otherwise too?
                jp.skipChildren();
                continue;
            }
            // if we have already constructed POJO, things are simple:
            if (bean != null) {
                try {
                    prop.deserializeAndSet(jpctxtbean);
                } catch (Exception e) {
                    wrapAndThrow(ebeanprop.getName(), ctxt);
                }
                continue;
            }
            final String propName = prop.getName();
            // if not yet, maybe we got a creator property?
            SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
            if (creatorProp != null) {
                // Last creator property to set?
                Object value = creatorProp.deserialize(jpctxt);
                if (buffer.assignParameter(creatorProp.getCreatorIndex(), value)) {
                    try {
                        bean = creator.build(ctxtbuffer);
                    } catch (Exception e) {
                        wrapAndThrow(e.getRawClass(), propNamectxt);
                        continue// never gets here
                    }
                    //  polymorphic?
                    if (bean.getClass() != .getRawClass()) {
                        /* 23-Jul-2012, tatu: Not sure if these could ever be properly
                         *   supported (since ordering of elements may not be guaranteed);
                         *   but make explicitly non-supported for now.
                         */
                        throw ctxt.mappingException("Can not support implicit polymorphic deserialization for POJOs-as-Arrays style: "
                                +"nominal type "+.getRawClass().getName()+", actual type "+bean.getClass().getName());
                    }
                }
                continue;
            }
            // Object Id property?
            if (buffer.readIdProperty(propName)) {
                continue;
            }
            // regular property? needs buffering
            buffer.bufferProperty(propprop.deserialize(jpctxt));
        }
        // In case we didn't quite get all the creator properties, we may have to do this:
        if (bean == null) {
            try {
                bean = creator.build(ctxtbuffer);
            } catch (Exception e) {
                wrapInstantiationProblem(ectxt);
                return null// never gets here
            }
        }
        return bean;
    }
    /*
    /**********************************************************
    /* Helper methods, error reporting
    /**********************************************************
     */
            throws IOExceptionJsonProcessingException
    {
        // Let's start with failure
        throw ctxt.mappingException("Can not deserialize a POJO (of type "+.getRawClass().getName()
                +") from non-Array representation (token: "+jp.getCurrentToken()
                +"): type/property designed to be serialized as JSON Array");
        // in future, may allow use of "standard" POJO serialization as well; if so, do:
        //return _delegate.deserialize(jp, ctxt);
    }
New to GrepCode? Check out our FAQ X