Class BeanSerializerBase

All Implemented Interfaces:
JsonFormatVisitable
Direct Known Subclasses:
BeanAsArraySerializer, BeanSerializer, UnrolledBeanAsArraySerializer, UnrolledBeanSerializer, UnwrappingBeanSerializer

public abstract class BeanSerializerBase extends StdSerializer<Object>
Base class both for the standard bean serializer, and couple of variants that only differ in small details. Can be used for custom bean serializers as well, although that is not the primary design goal.
  • Field Details

    • NAME_FOR_OBJECT_REF

      protected static final PropertyName NAME_FOR_OBJECT_REF
    • NO_PROPS

      protected static final BeanPropertyWriter[] NO_PROPS
    • _beanType

      protected final JavaType _beanType
    • _props

      protected final BeanPropertyWriter[] _props
      Writers used for outputting actual property values
    • _filteredProps

      protected final BeanPropertyWriter[] _filteredProps
      Optional filters used to suppress output of properties that are only to be included in certain views
    • _propertyFilterId

      protected final Object _propertyFilterId
      Id of the bean property filter to use, if any; null if none.
    • _typeId

      protected final AnnotatedMember _typeId
      If using custom type ids (usually via getter, or field), this is the reference to that member.
    • _objectIdWriter

      protected final ObjectIdWriter _objectIdWriter
      If this POJO can be alternatively serialized using just an object id to denote a reference to previously serialized object, this Object will handle details.
    • _serializationShape

      protected final JsonFormat.Shape _serializationShape
      Requested shape from bean class annotations.
  • Constructor Details

  • Method Details

    • withObjectIdWriter

      public abstract BeanSerializerBase withObjectIdWriter(ObjectIdWriter objectIdWriter)
      Mutant factory used for creating a new instance with different ObjectIdWriter.
    • withByNameInclusion

      protected abstract BeanSerializerBase withByNameInclusion(Set<String> toIgnore, Set<String> toInclude)
      Mutant factory used for creating a new instance with additional set of properties to ignore or include (from properties this instance otherwise has)
    • asArraySerializer

      protected abstract BeanSerializerBase asArraySerializer()
      Mutant factory for creating a variant that output POJO as a JSON Array. Implementations may ignore this request if output as array is not possible (either at all, or reliably).
    • withFilterId

      public abstract BeanSerializerBase withFilterId(Object filterId)
      Mutant factory used for creating a new instance with different filter id (used with JsonFilter annotation)
      Overrides:
      withFilterId in class ValueSerializer<Object>
    • withProperties

      protected abstract BeanSerializerBase withProperties(BeanPropertyWriter[] properties, BeanPropertyWriter[] filteredProperties)
      Mutant factory used for creating a new instance with modified set of properties
    • unwrappingSerializer

      public abstract ValueSerializer<Object> unwrappingSerializer(NameTransformer unwrapper)
      Lets force sub-classes to implement this, to avoid accidental missing of handling...
      Overrides:
      unwrappingSerializer in class ValueSerializer<Object>
      Parameters:
      unwrapper - Name transformation to use to convert between names of unwrapper properties
    • resolve

      public void resolve(SerializationContext provider)
      We need to resolve dependant serializers here to be able to properly handle cyclic type references.
      Overrides:
      resolve in class ValueSerializer<Object>
      Parameters:
      provider - Currently active serialization context.
    • findConvertingSerializer

      protected ValueSerializer<Object> findConvertingSerializer(SerializationContext provider, BeanPropertyWriter prop)
      Helper method that can be used to see if specified property is annotated to indicate use of a converter for property value (in case of container types, it is container type itself, not key or content type).
    • createContextual

      public ValueSerializer<?> createContextual(SerializationContext ctxt, BeanProperty property)
      Description copied from class: ValueSerializer
      Method called to see if a different (or differently configured) serializer is needed to serialize values of specified property (or, for root values, in which case `null` is passed). Note that instance that this method is called on is typically shared one and as a result method should NOT modify this instance but rather construct and return a new instance. This instance should only be returned as-is, in case it is already suitable for use.

      Note that method is only called once per POJO property, and for the first usage as root value serializer; it is not called for every serialization, as doing that would have significant performance impact; most serializers cache contextual instances for future use.

      Overrides:
      createContextual in class ValueSerializer<Object>
      Parameters:
      ctxt - Context to use for accessing config, other serializers
      property - Property (defined by one or more accessors - field or method - used for accessing logical property value) for which serializer is used to be used; or, `null` for root value (or in cases where caller does not have this information, which is handled as root value case).
      Returns:
      Serializer to use for serializing values of specified property; may be this instance or a new instance.
    • properties

      public Iterator<PropertyWriter> properties()
      Description copied from class: ValueSerializer
      Accessor for iterating over logical properties that the type handled by this serializer has, from serialization perspective. Actual type of properties, if any, will be BeanPropertyWriter. Of standard Jackson serializers, only BeanSerializer exposes properties.
      Overrides:
      properties in class ValueSerializer<Object>
    • propertyCount

      public int propertyCount()
      Since:
      3.0
    • hasViewProperties

      public boolean hasViewProperties()
      Accessor for checking if view-processing is enabled for this bean, that is, if it has separate set of properties with view-checking added.
      Since:
      3.0
    • getFilterId

      public Object getFilterId()
      Since:
      3.0
    • canCreateArraySerializer

      public boolean canCreateArraySerializer()
      Helper method for sub-classes to check if it should be possible to construct an "as-array" serializer. Returns if all of following hold true:
      • have Object Id (may be allowed in future)
      • have "any getter"
      Since:
      3.0
    • usesObjectId

      public boolean usesObjectId()
      Description copied from class: ValueSerializer
      Method that can be called to see whether this serializer instance will use Object Id to handle cyclic references.
      Overrides:
      usesObjectId in class ValueSerializer<Object>
    • serialize

      public abstract void serialize(Object bean, JsonGenerator gen, SerializationContext provider) throws JacksonException
      Description copied from class: ValueSerializer
      Method that can be called to ask implementation to serialize values of type this serializer handles.
      Specified by:
      serialize in class StdSerializer<Object>
      Parameters:
      bean - Value to serialize; can not be null.
      gen - Generator used to output resulting Json content
      provider - Context that can be used to get serializers for serializing Objects value contains, if any.
      Throws:
      JacksonException
    • serializeWithType

      public void serializeWithType(Object bean, JsonGenerator gen, SerializationContext ctxt, TypeSerializer typeSer) throws JacksonException
      Description copied from class: ValueSerializer
      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 ValueSerializer<Object>
      Parameters:
      bean - Value to serialize; can not be null.
      gen - Generator used to output resulting Json content
      ctxt - Context that can be used to get serializers for serializing Objects value contains, if any.
      typeSer - Type serializer to use for including type information
      Throws:
      JacksonException
    • _serializeWithObjectId

      protected final void _serializeWithObjectId(Object bean, JsonGenerator g, SerializationContext provider, boolean startEndObject) throws JacksonException
      Throws:
      JacksonException
    • _serializeWithObjectId

      protected final void _serializeWithObjectId(Object bean, JsonGenerator g, SerializationContext provider, TypeSerializer typeSer) throws JacksonException
      Throws:
      JacksonException
    • _serializeObjectId

      protected void _serializeObjectId(Object bean, JsonGenerator g, SerializationContext ctxt, TypeSerializer typeSer, WritableObjectId objectId) throws JacksonException
      Throws:
      JacksonException
    • _typeIdDef

      protected final WritableTypeId _typeIdDef(TypeSerializer typeSer, Object bean, JsonToken valueShape)
    • _serializePropertiesNoView

      protected void _serializePropertiesNoView(Object bean, JsonGenerator gen, SerializationContext provider, BeanPropertyWriter[] props) throws JacksonException
      Method called when neither JSON Filter is to be applied, nor view-filtering. This means that all property writers are non null and can be called directly.
      Throws:
      JacksonException
      Since:
      3.0
    • _serializePropertiesMaybeView

      protected void _serializePropertiesMaybeView(Object bean, JsonGenerator g, SerializationContext provider, BeanPropertyWriter[] props) throws JacksonException
      Method called when no JSON Filter is to be applied, but View filtering is in effect and so some of properties may be nulls to check.
      Throws:
      JacksonException
      Since:
      3.0
    • _serializePropertiesFiltered

      protected void _serializePropertiesFiltered(Object bean, JsonGenerator g, SerializationContext provider, Object filterId) throws JacksonException
      Alternative serialization method that gets called when there is a PropertyFilter that needs to be called to determine which properties are to be serialized (and possibly how)
      Throws:
      JacksonException
    • _serializeProperties

      protected void _serializeProperties(Object bean, JsonGenerator g, SerializationContext provider) throws JacksonException
      Throws:
      JacksonException
    • acceptJsonFormatVisitor

      public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
      Description copied from class: StdSerializer
      Default implementation specifies no format. This behavior is usually overriden by custom serializers.
      Specified by:
      acceptJsonFormatVisitor in interface JsonFormatVisitable
      Overrides:
      acceptJsonFormatVisitor in class StdSerializer<Object>
      typeHint - Type of element (entity like property) being visited
    • toString

      public String toString()
      Overrides:
      toString in class Object