Class TypeSerializer

  • Direct Known Subclasses:
    TypeSerializerBase

    public abstract class TypeSerializer
    extends java.lang.Object
    Interface for serializing type information regarding instances of specified base type (super class), so that exact subtype can be properly deserialized later on. These instances are to be called by regular JsonSerializers using proper contextual calls, to add type information using mechanism type serializer was configured with.

    NOTE: version 2.9 contains significant attempt at simplifying interface, as well as giving format implementation (via JsonGenerator) more control over actual serialization details. Minor changes are required to change call pattern so that return value of "prefix" write needs to be passed to "suffix" write.

    • Constructor Detail

      • TypeSerializer

        public TypeSerializer()
    • Method Detail

      • forProperty

        public abstract TypeSerializer forProperty​(BeanProperty prop)
        Method called to create contextual version, to be used for values of given property. This may be the type itself (as is the case for bean properties), or values contained (for Collection or Map valued properties).
        Since:
        2.0
      • getTypeInclusion

        public abstract com.fasterxml.jackson.annotation.JsonTypeInfo.As getTypeInclusion()
        Accessor for type information inclusion method that serializer uses; indicates how type information is embedded in resulting JSON.
      • getPropertyName

        public abstract java.lang.String getPropertyName()
        Name of property that contains type information, if property-based inclusion is used.
      • getTypeIdResolver

        public abstract TypeIdResolver getTypeIdResolver()
        Accessor for object that handles conversions between types and matching type ids.
      • typeId

        public com.fasterxml.jackson.core.type.WritableTypeId typeId​(java.lang.Object value,
                                                                     com.fasterxml.jackson.core.JsonToken valueShape,
                                                                     java.lang.Object id)
      • typeId

        public com.fasterxml.jackson.core.type.WritableTypeId typeId​(java.lang.Object value,
                                                                     java.lang.Class<?> typeForId,
                                                                     com.fasterxml.jackson.core.JsonToken valueShape)
      • writeTypePrefix

        public abstract com.fasterxml.jackson.core.type.WritableTypeId writeTypePrefix​(com.fasterxml.jackson.core.JsonGenerator g,
                                                                                       com.fasterxml.jackson.core.type.WritableTypeId typeId)
                                                                                throws java.io.IOException
        Method called to write initial part of type information for given value, along with possible wrapping to use: details are specified by `typeId` argument. Note that for structured types (Object, Array), this call will add necessary start token so it should NOT be explicitly written, unlike with non-type-id value writes.

        See writeTypeSuffix(JsonGenerator, WritableTypeId) for a complete example of typical usage.

        Parameters:
        g - Generator to use for outputting type id and possible wrapping
        typeId - Details of what type id is to be written, how.
        Throws:
        java.io.IOException
        Since:
        2.9
      • writeTypeSuffix

        public abstract com.fasterxml.jackson.core.type.WritableTypeId writeTypeSuffix​(com.fasterxml.jackson.core.JsonGenerator g,
                                                                                       com.fasterxml.jackson.core.type.WritableTypeId typeId)
                                                                                throws java.io.IOException
        Method that should be called after writeTypePrefix(JsonGenerator, WritableTypeId) and matching value write have been called, passing WritableTypeId returned. Usual idiom is:
         // Indicator generator that type identifier may be needed; generator may write
         // one as suggested, modify information, or take some other action 
         // NOTE! For Object/Array types, this will ALSO write start marker!
         WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen,
                  typeSer.typeId(value, JsonToken.START_OBJECT));
        
         // serializing actual value for which TypeId may have been written... like
         // NOTE: do NOT write START_OBJECT before OR END_OBJECT after:
         g.writeStringField("message", "Hello, world!"
        
         // matching type suffix call to let generator chance to add suffix, if any
         // NOTE! For Object/Array types, this will ALSO write end marker!
         typeSer.writeTypeSuffix(gen, typeIdDef);
        
        Throws:
        java.io.IOException
        Since:
        2.9
      • writeCustomTypeSuffixForScalar

        @Deprecated
        public void writeCustomTypeSuffixForScalar​(java.lang.Object value,
                                                   com.fasterxml.jackson.core.JsonGenerator g,
                                                   java.lang.String typeId)
                                            throws java.io.IOException
        Deprecated.
        Throws:
        java.io.IOException
      • writeCustomTypeSuffixForObject

        @Deprecated
        public void writeCustomTypeSuffixForObject​(java.lang.Object value,
                                                   com.fasterxml.jackson.core.JsonGenerator g,
                                                   java.lang.String typeId)
                                            throws java.io.IOException
        Deprecated.
        Throws:
        java.io.IOException
      • writeCustomTypeSuffixForArray

        @Deprecated
        public void writeCustomTypeSuffixForArray​(java.lang.Object value,
                                                  com.fasterxml.jackson.core.JsonGenerator g,
                                                  java.lang.String typeId)
                                           throws java.io.IOException
        Deprecated.
        Throws:
        java.io.IOException
      • _writeLegacySuffix

        protected final void _writeLegacySuffix​(com.fasterxml.jackson.core.JsonGenerator g,
                                                com.fasterxml.jackson.core.type.WritableTypeId typeId)
                                         throws java.io.IOException
        Helper method needed for backwards compatibility: since original type id can not be routed through completely, we have to reverse-engineer likely setting before calling suffix.
        Throws:
        java.io.IOException
        Since:
        2.9