Class JsonSerializer<T>

  • Direct Known Subclasses:
    JsonSerializer.None, SerializerBase

    public abstract class JsonSerializer<T>
    extends java.lang.Object
    Abstract class that defines API used by ObjectMapper (and other chained JsonSerializers too) to serialize Objects of arbitrary types into JSON, using provided JsonGenerator.

    NOTE: it is recommended that custom serializers extend SerializerBase instead of this class, since it will implement many of optional methods of this class.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  JsonSerializer.None
      This marker class is only to be used with annotations, to indicate that no serializer is configured.
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonSerializer()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Class<T> handledType()
      Method for accessing type of Objects this serializer can handle.
      boolean isUnwrappingSerializer()
      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.
      abstract void serialize​(T value, JsonGenerator jgen, SerializerProvider provider)
      Method that can be called to ask implementation to serialize values of type this serializer handles.
      void serializeWithType​(T value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer)
      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.
      JsonSerializer<T> unwrappingSerializer()
      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).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JsonSerializer

        public JsonSerializer()
    • Method Detail

      • unwrappingSerializer

        public JsonSerializer<T> unwrappingSerializer()
        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

        Since:
        1.9
      • isUnwrappingSerializer

        public boolean isUnwrappingSerializer()
        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.
        Since:
        1.9
      • serialize

        public abstract void serialize​(T value,
                                       JsonGenerator jgen,
                                       SerializerProvider provider)
                                throws java.io.IOException,
                                       JsonProcessingException
        Method that can be called to ask implementation to serialize values of type this serializer handles.
        Parameters:
        value - Value to serialize; can not be null.
        jgen - 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
        JsonProcessingException
      • serializeWithType

        public void serializeWithType​(T value,
                                      JsonGenerator jgen,
                                      SerializerProvider provider,
                                      TypeSerializer typeSer)
                               throws java.io.IOException,
                                      JsonProcessingException
        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 ignore serialization of type information, and just calls serialize(T, org.codehaus.jackson.JsonGenerator, org.codehaus.jackson.map.SerializerProvider): serializers that can embed type information should override this to implement actual handling. Most common such handling is done by something like:

          // note: method to call depends on whether this type is serialized as JSON scalar, object or Array!
          typeSer.writeTypePrefixForScalar(value, jgen);
          serialize(value, jgen, provider);
          typeSer.writeTypeSuffixForScalar(value, jgen);
        
        Parameters:
        value - Value to serialize; can not be null.
        jgen - 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
        JsonProcessingException
        Since:
        1.5
      • handledType

        public java.lang.Class<T> handledType()
        Method for accessing type of Objects this serializer can handle. Note that this information is not guaranteed to be exact -- it may be a more generic (super-type) -- but it should not be incorrect (return a non-related type).

        Default implementation will return null, which essentially means same as returning Object.class would; that is, that nothing is known about handled type.