Class BeanAsArraySerializer

  • All Implemented Interfaces:
    JsonFormatVisitable, SchemaAware, ContextualSerializer, ResolvableSerializer, java.io.Serializable

    public class BeanAsArraySerializer
    extends BeanSerializerBase
    Specialized POJO serializer that differs from 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 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
    See Also:
    Serialized Form
    • Constructor Detail

    • Method Detail

      • unwrappingSerializer

        public JsonSerializer<java.lang.Object> unwrappingSerializer​(NameTransformer transformer)
        Description copied from class: JsonSerializer
        Method that will return serializer instance that produces "unwrapped" serialization, if applicable for type being serialized (which is the case for some serializers that produce JSON Objects as output). If no unwrapped serializer can be constructed, will simply return serializer as-is.

        Default implementation just returns serializer as-is, indicating that no unwrapped variant exists

        Overrides:
        unwrappingSerializer in class JsonSerializer<java.lang.Object>
        Parameters:
        transformer - Name transformation to use to convert between names of unwrapper properties
      • isUnwrappingSerializer

        public boolean isUnwrappingSerializer()
        Description copied from class: JsonSerializer
        Accessor for checking whether this serializer is an "unwrapping" serializer; this is necessary to know since it may also require caller to suppress writing of the leading property name.
        Overrides:
        isUnwrappingSerializer in class JsonSerializer<java.lang.Object>
      • serializeWithType

        public void serializeWithType​(java.lang.Object bean,
                                      JsonGenerator gen,
                                      SerializerProvider provider,
                                      TypeSerializer typeSer)
                               throws java.io.IOException
        Description copied from class: JsonSerializer
        Method that can be called to ask implementation to serialize values of type this serializer handles, using specified type serializer for embedding necessary type information.

        Default implementation will throw UnsupportedOperationException to indicate that proper type handling needs to be implemented.

        For simple datatypes written as a single scalar value (JSON String, Number, Boolean), implementation would look like:

          // note: method to call depends on whether this type is serialized as JSON scalar, object or Array!
          typeSer.writeTypePrefixForScalar(value, gen);
          serialize(value, gen, provider);
          typeSer.writeTypeSuffixForScalar(value, gen);
        
        and implementations for type serialized as JSON Arrays or Objects would differ slightly, as START-ARRAY/END-ARRAY and START-OBJECT/END-OBJECT pairs need to be properly handled with respect to serializing of contents.
        Overrides:
        serializeWithType in class BeanSerializerBase
        Parameters:
        bean - Value to serialize; can not be null.
        gen - Generator used to output resulting Json content
        provider - Provider that can be used to get serializers for serializing Objects value contains, if any.
        typeSer - Type serializer to use for including type information
        Throws:
        java.io.IOException
      • serialize

        public final void serialize​(java.lang.Object bean,
                                    JsonGenerator gen,
                                    SerializerProvider provider)
                             throws java.io.IOException
        Main serialization method that will delegate actual output to configured BeanPropertyWriter instances.
        Specified by:
        serialize in class BeanSerializerBase
        Parameters:
        bean - Value to serialize; can not be null.
        gen - Generator used to output resulting Json content
        provider - Provider that can be used to get serializers for serializing Objects value contains, if any.
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object