Class JsonSerializer<T>

    • Constructor Detail

      • JsonSerializer

        public JsonSerializer()
    • Method Detail

      • unwrappingSerializer

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

        Parameters:
        unwrapper - Name transformation to use to convert between names of unwrapper properties
      • replaceDelegatee

        public JsonSerializer<T> replaceDelegatee​(JsonSerializer<?> delegatee)
        Method that can be called to try to replace serializer this serializer delegates calls to. If not supported (either this serializer does not delegate anything; or it does not want any changes), should either throw UnsupportedOperationException (if operation does not make sense or is not allowed); or return this serializer as is.
        Since:
        2.1
      • withFilterId

        public JsonSerializer<?> withFilterId​(java.lang.Object filterId)
        Mutant factory method that is called if contextual configuration indicates that a specific filter (as specified by filterId) is to be used for serialization.

        Default implementation simply returns this; sub-classes that do support filtering will need to create and return new instance if filter changes.

        Since:
        2.6
      • serialize

        public abstract void serialize​(T value,
                                       JsonGenerator gen,
                                       SerializerProvider serializers)
                                throws java.io.IOException
        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.
        gen - Generator used to output resulting Json content
        serializers - Provider that can be used to get serializers for serializing Objects value contains, if any.
        Throws:
        java.io.IOException
      • serializeWithType

        public void serializeWithType​(T value,
                                      JsonGenerator gen,
                                      SerializerProvider serializers,
                                      TypeSerializer typeSer)
                               throws java.io.IOException
        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.
        Parameters:
        value - Value to serialize; can not be null.
        gen - Generator used to output resulting Json content
        serializers - 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
      • 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.

      • isEmpty

        @Deprecated
        public boolean isEmpty​(T value)
        Deprecated.
        Since 2.5 Use isEmpty(SerializerProvider, Object) instead; will be removed from 3.0
        Method called to check whether given serializable value is considered "empty" value (for purposes of suppressing serialization of empty values).

        Default implementation will consider only null values to be empty.

      • isEmpty

        public boolean isEmpty​(SerializerProvider provider,
                               T value)
        Method called to check whether given serializable value is considered "empty" value (for purposes of suppressing serialization of empty values).

        Default implementation will consider only null values to be empty.

        NOTE: replaces isEmpty(Object), which was deprecated in 2.5

        Since:
        2.5
      • usesObjectId

        public boolean usesObjectId()
        Method that can be called to see whether this serializer instance will use Object Id to handle cyclic references.
      • 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.
      • getDelegatee

        public JsonSerializer<?> getDelegatee()
        Accessor that can be used to determine if this serializer uses another serializer for actual serialization, by delegating calls. If so, will return immediate delegate (which itself may delegate to further serializers); otherwise will return null.
        Returns:
        Serializer this serializer delegates calls to, if null; null otherwise.
        Since:
        2.1
      • properties

        public java.util.Iterator<PropertyWriter> properties()
        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.
        Since:
        2.6