Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.impl;
  
  
Specialized POJO serializer that differs from com.fasterxml.jackson.databind.ser.BeanSerializer in that instead of producing a JSON Object it will output a JSON Array, omitting field names, and serializing values in specified serialization order. This behavior is usually triggered by using annotation com.fasterxml.jackson.annotation.JsonFormat or its equivalents.

This serializer can be used for "simple" instances; and will NOT be used if one of following is true:

  • Unwrapping is used (no way to expand out array in JSON Object)
  • Type information ("type id") is to be used: while this could work for some embedding methods, it would likely cause conflicts.
  • Object Identity ("object id") is used: while references would work, the problem is inclusion of id itself.
Note that it is theoretically possible that last 2 issues could be addressed (by reserving room in array, for example); and if so, support improved.

In cases where array-based output is not feasible, this serializer can instead delegate to the original Object-based serializer; this is why a reference is retained to the original serializer.

Since:
2.1
 
 public class BeanAsArraySerializer
     extends BeanSerializerBase
 {
    
Serializer that would produce JSON Object version; used in cases where array output can not be used.
 
     protected final BeanSerializerBase _defaultSerializer;
     
     /*
     /**********************************************************
     /* Life-cycle: constructors
     /**********************************************************
      */
 
 
     public BeanAsArraySerializer(BeanSerializerBase src) {    
         super(src, (ObjectIdWriternull);
          = src;
     }
 
     protected BeanAsArraySerializer(BeanSerializerBase srcString[] toIgnore) {
         super(srctoIgnore);
          = src;
     }
 
     /*
     /**********************************************************
     /* Life-cycle: factory methods, fluent factories
     /**********************************************************
      */
 
     @Override
     public JsonSerializer<ObjectunwrappingSerializer(NameTransformer transformer) {
         /* If this gets called, we will just need delegate to the default
          * serializer, to "undo" as-array serialization
          */
         return .unwrappingSerializer(transformer);
     }
 
     @Override
     public boolean isUnwrappingSerializer() {
         return false;
     }
 
     @Override
     public BeanSerializerBase withObjectIdWriter(ObjectIdWriter objectIdWriter) {
         // can't handle Object Ids, for now, so:
         return .withObjectIdWriter(objectIdWriter);
     }
 
     @Override
     protected BeanAsArraySerializer withIgnorals(String[] toIgnore) {
        return new BeanAsArraySerializer(thistoIgnore);
    }
    @Override
    protected BeanSerializerBase asArraySerializer() {
        // already is one, so:
        return this;
    }
    
    /*
    /**********************************************************
    /* JsonSerializer implementation that differs between impls
    /**********************************************************
     */
    // Re-defined from base class...
    @Override
    public void serializeWithType(Object beanJsonGenerator jgen,
            SerializerProvider providerTypeSerializer typeSer)
        throws IOExceptionJsonGenerationException
    {
        /* Should not even get here; but let's be nice and re-route
         * if need be.
         */
        .serializeWithType(beanjgenprovidertypeSer);
    }
    
    
Main serialization method that will delegate actual output to configured com.fasterxml.jackson.databind.ser.BeanPropertyWriter instances.
    @Override
    public final void serialize(Object beanJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException
    {
        // [JACKSON-805]
                && hasSingleElement(provider)) {
            serializeAsArray(beanjgenprovider);
            return;
        }
        /* note: it is assumed here that limitations (type id, object id,
         * any getter, filtering) have already been checked; so code here
         * is trivial.
         */
        jgen.writeStartArray();
        serializeAsArray(beanjgenprovider);
        jgen.writeEndArray();
    }
    /*
    /**********************************************************
    /* Field serialization methods
    /**********************************************************
     */
    private boolean hasSingleElement(SerializerProvider provider) {
        final BeanPropertyWriter[] props;
        if ( != null && provider.getSerializationView() != null) {
            props = ;
        } else {
            props = ;
        }
        return props.length == 1;
    }
    protected final void serializeAsArray(Object beanJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException
    {
        final BeanPropertyWriter[] props;
        if ( != null && provider.getSerializationView() != null) {
            props = ;
        } else {
            props = ;
        }
        int i = 0;
        try {
            for (final int len = props.lengthi < len; ++i) {
                BeanPropertyWriter prop = props[i];
                if (prop == null) { // can have nulls in filtered list; but if so, MUST write placeholders
                    jgen.writeNull();
                } else {
                    prop.serializeAsColumn(beanjgenprovider);
                }
            }
            // NOTE: any getters can not be supported either
            //if (_anyGetterWriter != null) {
            //    _anyGetterWriter.getAndSerialize(bean, jgen, provider);
            //}
        } catch (Exception e) {
            String name = (i == props.length) ? "[anySetter]" : props[i].getName();
            wrapAndThrow(providerebeanname);
        } catch (StackOverflowError e) {
            JsonMappingException mapE = new JsonMappingException("Infinite recursion (StackOverflowError)"e);
            String name = (i == props.length) ? "[anySetter]" : props[i].getName();
            mapE.prependPath(new JsonMappingException.Reference(beanname));
            throw mapE;
        }
    }
    
    /*
    /**********************************************************
    /* Standard methods
    /**********************************************************
     */
    @Override public String toString() {
        return "BeanAsArraySerializer for "+handledType().getName();
    }
New to GrepCode? Check out our FAQ X