Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.std;
  
  
Generic serializer for Object arrays (Object[]).
 
 public class ObjectArraySerializer
     extends ArraySerializerBase<Object[]>
     implements ContextualSerializer
 {
    
Whether we are using static typing (using declared types, ignoring runtime type) or not for elements.
 
     protected final boolean _staticTyping;

    
Declared type of element entries
 
     protected final JavaType _elementType;

    
Type serializer to use for values, if any.
 
     protected final TypeSerializer _valueTypeSerializer;
    
    
Value serializer to use, if it can be statically determined.
 
     protected JsonSerializer<Object_elementSerializer;

    
If element type can not be statically determined, mapping from runtime type to serializer is handled using this object
 
 
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
     
     public ObjectArraySerializer(JavaType elemTypeboolean staticTyping,
             TypeSerializer vtsJsonSerializer<ObjectelementSerializer)
     {
         super(Object[].classnull);
          = elemType;
          = staticTyping;
          = vts;
          = PropertySerializerMap.emptyMap();
          = elementSerializer;
     }
 
     {
         super(src);
          = src._elementType;
          = vts;
          = src._staticTyping;
          = src._dynamicSerializers;
          = src._elementSerializer;
     }
     
     @SuppressWarnings("unchecked")
             BeanProperty propertyTypeSerializer vtsJsonSerializer<?> elementSerializer)
     {
         super(src,  property);
          = src._elementType;
          = vts;
          = src._staticTyping;
          = src._dynamicSerializers;
          = (JsonSerializer<Object>) elementSerializer;
     }
     
     @Override
     {
         return new ObjectArraySerializer(vts);
    }
            TypeSerializer vtsJsonSerializer<?> ser) {
        if ( == prop && ser ==  &&  == vts) {
            return this;
        }
        return new ObjectArraySerializer(thispropvtsser);
    }
    /*
    /**********************************************************
    /* Post-processing
    /**********************************************************
     */
    @Override
            BeanProperty property)
        throws JsonMappingException
    {
        TypeSerializer vts = ;
        if (vts != null) {
            vts = vts.forProperty(property);
        }
        /* 29-Sep-2012, tatu: Actually, we need to do much more contextual
         *    checking here since we finally know for sure the property,
         *    and it may have overrides
         */
        JsonSerializer<?> ser = null;
        // First: if we have a property, may have property-annotation overrides
        if (property != null) {
            AnnotatedMember m = property.getMember();
            if (m != null) {
                Object serDef = provider.getAnnotationIntrospector().findContentSerializer(m);
                if (serDef != null) {
                    ser = provider.serializerInstance(mserDef);
                }
            }
        }
        if (ser == null) {
            ser = ;
        }
        // #124: May have a content converter
        ser = findConvertingContentSerializer(providerpropertyser);
        if (ser == null) {
            // 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
            //   we can consider it a static case as well.
            if ( != null) {
                if ( || hasContentTypeAnnotation(providerproperty)) {
                    ser = provider.findValueSerializer(property);
                }
            }
        } else {
            ser = provider.handleSecondaryContextualization(serproperty);
        }
        return withResolved(propertyvtsser);
    }
    /*
    /**********************************************************
    /* Accessors
    /**********************************************************
     */
        
    @Override
    public JavaType getContentType() {
        return ;
    }
    @Override
    public JsonSerializer<?> getContentSerializer() {
        return ;
    }
    @Override
    public boolean isEmpty(Object[] value) {
        return (value == null) || (value.length == 0);
    }
    @Override
    public boolean hasSingleElement(Object[] value) {
        return (value.length == 1);
    }
    /*
    /**********************************************************
    /* Actual serialization
    /**********************************************************
     */
    
    @Override
    public void serializeContents(Object[] valueJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException
    {
        final int len = value.length;
        if (len == 0) {
            return;
        }
        if ( != null) {
            serializeContentsUsing(valuejgenprovider);
            return;
        }
        if ( != null) {
            serializeTypedContents(valuejgenprovider);
            return;
        }
        int i = 0;
        Object elem = null;
        try {
            PropertySerializerMap serializers = ;
            for (; i < len; ++i) {
                elem = value[i];
                if (elem == null) {
                    provider.defaultSerializeNull(jgen);
                    continue;
                }
                Class<?> cc = elem.getClass();
                JsonSerializer<Objectserializer = serializers.serializerFor(cc);
                if (serializer == null) {
                    // To fix [JACKSON-508]
                    if (.hasGenericTypes()) {
                        serializer = _findAndAddDynamic(serializers,
                                provider.constructSpecializedType(cc), provider);
                    } else {
                        serializer = _findAndAddDynamic(serializersccprovider);
                    }
                }
                serializer.serialize(elemjgenprovider);
            }
        } catch (IOException ioe) {
            throw ioe;
        } catch (Exception e) {
            // [JACKSON-55] Need to add reference information
            /* 05-Mar-2009, tatu: But one nasty edge is when we get
             *   StackOverflow: usually due to infinite loop. But that gets
             *   hidden within an InvocationTargetException...
             */
            Throwable t = e;
            while (t instanceof InvocationTargetException && t.getCause() != null) {
                t = t.getCause();
            }
            if (t instanceof Error) {
                throw (Errort;
            }
            throw JsonMappingException.wrapWithPath(telemi);
        }
    }
    public void serializeContentsUsing(Object[] valueJsonGenerator jgenSerializerProvider provider,
            JsonSerializer<Objectser)
        throws IOExceptionJsonGenerationException
    {
        final int len = value.length;
        final TypeSerializer typeSer = ;
        int i = 0;
        Object elem = null;
        try {
            for (; i < len; ++i) {
                elem = value[i];
                if (elem == null) {
                    provider.defaultSerializeNull(jgen);
                    continue;
                }
                if (typeSer == null) {
                    ser.serialize(elemjgenprovider);
                } else {
                    ser.serializeWithType(elemjgenprovidertypeSer);
                }
            }
        } catch (IOException ioe) {
            throw ioe;
        } catch (Exception e) {
            Throwable t = e;
            while (t instanceof InvocationTargetException && t.getCause() != null) {
                t = t.getCause();
            }
            if (t instanceof Error) {
                throw (Errort;
            }
            throw JsonMappingException.wrapWithPath(telemi);
        }
    }
    public void serializeTypedContents(Object[] valueJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException
    {
        final int len = value.length;
        final TypeSerializer typeSer = ;
        int i = 0;
        Object elem = null;
        try {
            PropertySerializerMap serializers = ;
            for (; i < len; ++i) {
                elem = value[i];
                if (elem == null) {
                    provider.defaultSerializeNull(jgen);
                    continue;
                }
                Class<?> cc = elem.getClass();
                JsonSerializer<Objectserializer = serializers.serializerFor(cc);
                if (serializer == null) {
                    serializer = _findAndAddDynamic(serializersccprovider);
                }
                serializer.serializeWithType(elemjgenprovidertypeSer);
            }
        } catch (IOException ioe) {
            throw ioe;
        } catch (Exception e) {
            Throwable t = e;
            while (t instanceof InvocationTargetException && t.getCause() != null) {
                t = t.getCause();
            }
            if (t instanceof Error) {
                throw (Errort;
            }
            throw JsonMappingException.wrapWithPath(telemi);
        }
    }
    
    @SuppressWarnings("deprecation")
    @Override
    public JsonNode getSchema(SerializerProvider providerType typeHint)
        throws JsonMappingException
    {
        ObjectNode o = createSchemaNode("array"true);
        if (typeHint != null) {
            JavaType javaType = provider.constructType(typeHint);
            if (javaType.isArrayType()) {
                Class<?> componentType = ((ArrayTypejavaType).getContentType().getRawClass();
                // 15-Oct-2010, tatu: We can't serialize plain Object.class; but what should it produce here? Untyped?
                if (componentType == Object.class) {
                    o.put("items", com.fasterxml.jackson.databind.jsonschema.JsonSchema.getDefaultSchemaNode());
                } else {
                    JsonSerializer<Objectser = provider.findValueSerializer(componentType);
                    JsonNode schemaNode = (ser instanceof SchemaAware) ?
                            ((SchemaAwareser).getSchema(providernull) :
                            	com.fasterxml.jackson.databind.jsonschema.JsonSchema.getDefaultSchemaNode();
                    o.put("items"schemaNode);
                }
            }
        }
        return o;
    }
    
    @Override
    public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitorJavaType typeHint)
        throws JsonMappingException
    {
        JsonArrayFormatVisitor arrayVisitor = visitor.expectArrayFormat(typeHint);
        if (arrayVisitor != null) {
            TypeFactory tf = visitor.getProvider().getTypeFactory();
            JavaType contentType = tf.moreSpecificType(typeHint.getContentType());
            if (contentType == null) {
                throw new JsonMappingException("Could not resolve type");
            }
            JsonSerializer<?> valueSer = ;
            if (valueSer == null) {
                valueSer = visitor.getProvider().findValueSerializer(contentType);
            }
            arrayVisitor.itemsFormat(valueSercontentType);
        }
    }
            Class<?> typeSerializerProvider providerthrows JsonMappingException
    {
        // did we get a new map of serializers? If so, start using it
        if (map != result.map) {
             = result.map;
        }
        return result.serializer;
    }
            JavaType typeSerializerProvider providerthrows JsonMappingException
    {
        // did we get a new map of serializers? If so, start using it
        if (map != result.map) {
             = result.map;
        }
        return result.serializer;
    }
New to GrepCode? Check out our FAQ X