Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.std;
  
  
  
Basic serializer that can serialize non-primitive arrays.
 
 public class ObjectArrayDeserializer
     extends ContainerDeserializerBase<Object[]>
     implements ContextualDeserializer
 {
     // // Configuration
 
    
Full generic type of the array being deserialized
 
     protected final ArrayType _arrayType;
    
    
Flag that indicates whether the component type is Object or not. Used for minor optimization when constructing result.
 
     protected final boolean _untyped;

    
Type of contained elements: needed for constructing actual result array
 
     protected final Class<?> _elementClass;

    
Element deserializer
 
     protected JsonDeserializer<Object_elementDeserializer;

    
If element instances have polymorphic type information, this is the type deserializer that can handle it
 
     protected final TypeDeserializer _elementTypeDeserializer;
 
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
     
     public ObjectArrayDeserializer(ArrayType arrayType,
             JsonDeserializer<ObjectelemDeserTypeDeserializer elemTypeDeser)
     {
         super(Object[].class);
          = arrayType;
          = arrayType.getContentType().getRawClass();
          = ( == Object.class);
          = elemDeser;
          = elemTypeDeser;
     }

    
Overridable fluent-factory method used to create contextual instances
 
     @SuppressWarnings("unchecked")
             JsonDeserializer<?> elemDeser)
     {
         if ((elemDeser == ) && (elemTypeDeser == )) {
             return this;
         }
         return new ObjectArrayDeserializer(,
                 (JsonDeserializer<Object>) elemDeserelemTypeDeser);
     }
 
 //    @Override
             BeanProperty propertythrows JsonMappingException
     {
         JsonDeserializer<?> deser = ;
         if (deser == null) {
             deser = ctxt.findContextualValueDeserializer(.getContentType(), property);
         } else { // if directly assigned, probably not yet contextual, so:
             if (deser instanceof ContextualDeserializer) {
                 deser = ((ContextualDeserializerdeser).createContextual(ctxtproperty);
             }
         }
         TypeDeserializer elemTypeDeser = ;
         if (elemTypeDeser != null) {
             elemTypeDeser = elemTypeDeser.forProperty(property);
         }
        return withDeserializer(elemTypeDeserdeser);
    }
    
    /*
    /**********************************************************
    /* ContainerDeserializerBase API
    /**********************************************************
     */
    @Override
    public JavaType getContentType() {
        return .getContentType();
    }
    @Override
        return ;
    }
    
    /*
    /**********************************************************
    /* JsonDeserializer API
    /**********************************************************
     */
    
    @Override
    public Object[] deserialize(JsonParser jpDeserializationContext ctxt)
        throws IOExceptionJsonProcessingException
    {
        // Ok: must point to START_ARRAY (or equivalent)
        if (!jp.isExpectedStartArrayToken()) {
            return handleNonArray(jpctxt);
        }
        final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
        Object[] chunk = buffer.resetAndStart();
        int ix = 0;
        JsonToken t;
        final TypeDeserializer typeDeser = ;
        while ((t = jp.nextToken()) != .) {
            // Note: must handle null explicitly here; value deserializers won't
            Object value;
            
            if (t == .) {
                value = null;
            } else if (typeDeser == null) {
                value = .deserialize(jpctxt);
            } else {
                value = .deserializeWithType(jpctxttypeDeser);
            }
            if (ix >= chunk.length) {
                chunk = buffer.appendCompletedChunk(chunk);
                ix = 0;
            }
            chunk[ix++] = value;
        }
        Object[] result;
        if () {
            result = buffer.completeAndClearBuffer(chunkix);
        } else {
            result = buffer.completeAndClearBuffer(chunkix);
        }
        ctxt.returnObjectBuffer(buffer);
        return result;
    }
    @Override
            TypeDeserializer typeDeserializer)
        throws IOExceptionJsonProcessingException
    {
        /* Should there be separate handling for base64 stuff?
         * for now this should be enough:
         */
        return (Object[]) typeDeserializer.deserializeTypedFromArray(jpctxt);
    }
    
    /*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
    
        throws IOExceptionJsonProcessingException
    {
        // First same as what PrimitiveArrayDeserializers.ByteDeser does:
        byte[] b = jp.getBinaryValue(ctxt.getBase64Variant());
        // But then need to convert to wrappers
        Byte[] result = new Byte[b.length];
        for (int i = 0, len = b.lengthi < len; ++i) {
            result[i] = Byte.valueOf(b[i]);
        }
        return result;
    }
    private final Object[] handleNonArray(JsonParser jpDeserializationContext ctxt)
        throws IOExceptionJsonProcessingException
    {
        // [JACKSON-620] Empty String can become null...
        if ((jp.getCurrentToken() == .)
                && ctxt.isEnabled(.)) {
            String str = jp.getText();
            if (str.length() == 0) {
                return null;
            }
        }
        
        // Can we do implicit coercion to a single-element array still?
            /* 04-Oct-2009, tatu: One exception; byte arrays are generally
             *   serialized as base64, so that should be handled
             */
            if (jp.getCurrentToken() == .
                &&  == Byte.class) {
                return deserializeFromBase64(jpctxt);
            }
            throw ctxt.mappingException(.getRawClass());
        }
        JsonToken t = jp.getCurrentToken();
        Object value;
        
        if (t == .) {
            value = null;
        } else if ( == null) {
            value = .deserialize(jpctxt);
        } else {
            value = .deserializeWithType(jpctxt);
        }
        // Ok: bit tricky, since we may want T[], not just Object[]
        Object[] result;
        if () {
            result = new Object[1];
        } else {
            result = (Object[]) Array.newInstance(, 1);
        }
        result[0] = value;
        return result;
    }
New to GrepCode? Check out our FAQ X