Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.std;
  
  
Base class for serializers that will output contents as JSON arrays; typically serializers used for java.util.Collection and array types.
 
 public abstract class AsArraySerializerBase<T>
     extends ContainerSerializer<T>
     implements ContextualSerializer
 {
     protected final boolean _staticTyping;
 
     protected final JavaType _elementType;

    
Type serializer used for values, if any.
 
     protected final TypeSerializer _valueTypeSerializer;

    
Value serializer to use, if it can be statically determined
 
     protected final JsonSerializer<Object_elementSerializer;

    
Collection-valued property being serialized with this instance
 
     protected final BeanProperty _property;

    
If element type can not be statically determined, mapping from runtime type to serializer is handled using this object
 
     
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
     
     protected AsArraySerializerBase(Class<?> clsJavaType etboolean staticTyping,
             TypeSerializer vtsBeanProperty propertyJsonSerializer<ObjectelementSerializer)
     {
         // typing with generics is messy... have to resort to this:
         super(clsfalse);
          = et;
         // static if explicitly requested, or if element type is final
          = staticTyping || (et != null && et.isFinal());
          = vts;
          = property;
          = elementSerializer;
          = PropertySerializerMap.emptyMap();
     }
 
     @SuppressWarnings("unchecked")
     protected AsArraySerializerBase(AsArraySerializerBase<?> src,
             BeanProperty propertyTypeSerializer vtsJsonSerializer<?> elementSerializer)
     {
         super(src);
          = src._elementType;
          = src._staticTyping;
          = vts;
          = property;
          = (JsonSerializer<Object>) elementSerializer;
          = src._dynamicSerializers;
     }
     
     public abstract AsArraySerializerBase<T> withResolved(BeanProperty property,
             TypeSerializer vtsJsonSerializer<?> elementSerializer);
 
     /*
     /**********************************************************
     /* Post-processing
     /**********************************************************
      */
    
    
This method is needed to resolve contextual annotations like per-property overrides, as well as do recursive call to createContextual of content serializer, if known statically.
    @Override
            BeanProperty property)
        throws JsonMappingException
    {
        TypeSerializer typeSer = ;
        if (typeSer != null) {
            typeSer = typeSer.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 = ;
        }
        // 18-Feb-2013, tatu: 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) {
                // 20-Aug-2013, tatu: Need to avoid trying to access serializer for java.lang.Object tho
                if (( && .getRawClass() != Object.class)
                        || hasContentTypeAnnotation(providerproperty)) {
                    ser = provider.findValueSerializer(property);
                }
            }
        } else {
            ser = provider.handleSecondaryContextualization(serproperty);
        }
        if ((ser != ) || (property != ) ||  != typeSer) {
            return withResolved(propertytypeSerser);
        }
        return this;
    }
    
    /*
    /**********************************************************
    /* Accessors
    /**********************************************************
     */
    
    @Override
    public JavaType getContentType() {
        return ;
    }
    @Override
    public JsonSerializer<?> getContentSerializer() {
        return ;
    }
    /*
    /**********************************************************
    /* Serialization
    /**********************************************************
     */
    
    @Override
    public final void serialize(T valueJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException
    {
        // [JACKSON-805]
                && hasSingleElement(value)) {
            serializeContents(valuejgenprovider);
            return;
        }
        jgen.writeStartArray();
        serializeContents(valuejgenprovider);
        jgen.writeEndArray();
    }
    // Note: was 'final' modifier in 2.2 and before; no real need to be, removed
    @Override
    public void serializeWithType(T valueJsonGenerator jgenSerializerProvider provider,
            TypeSerializer typeSer)
        throws IOExceptionJsonGenerationException
    {
        // note: let's NOT consider [JACKSON-805] here; gets too complicated, and probably just won't work
        typeSer.writeTypePrefixForArray(valuejgen);
        serializeContents(valuejgenprovider);
        typeSer.writeTypeSuffixForArray(valuejgen);
    }
    protected abstract void serializeContents(T valueJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException;
    @SuppressWarnings("deprecation")
    @Override
    public JsonNode getSchema(SerializerProvider providerType typeHint)
        throws JsonMappingException
    {
        /* 15-Jan-2010, tatu: This should probably be rewritten, given that
         *    more information about content type is actually being explicitly
         *    passed. So there should be less need to try to re-process that
         *    information.
         */
        ObjectNode o = createSchemaNode("array"true);
        JavaType contentType = null;
        if (typeHint != null) {
            JavaType javaType = provider.constructType(typeHint);
            contentType = javaType.getContentType();
            if (contentType == null) { // could still be parametrized (Iterators)
                if (typeHint instanceof ParameterizedType) {
                    Type[] typeArgs = ((ParameterizedTypetypeHint).getActualTypeArguments();
                    if (typeArgs.length == 1) {
                        contentType = provider.constructType(typeArgs[0]);
                    }
                }
            }
        }
        if (contentType == null &&  != null) {
            contentType = ;
        }
        if (contentType != null) {
            JsonNode schemaNode = null;
            // 15-Oct-2010, tatu: We can't serialize plain Object.class; but what should it produce here? Untyped?
            if (contentType.getRawClass() != Object.class) {
                JsonSerializer<Objectser = provider.findValueSerializer(contentType);
                if (ser instanceof SchemaAware) {
                    schemaNode = ((SchemaAwareser).getSchema(providernull);
                }
            }
            if (schemaNode == null) {
                schemaNode = 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 == null) ? null : 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
    {
        if (map != result.map) {
             = result.map;
        }
        return result.serializer;
    }
New to GrepCode? Check out our FAQ X