Class SerializerProvider

    • Field Detail

      • TYPE_OBJECT

        protected static final JavaType TYPE_OBJECT
      • _config

        protected final SerializationConfig _config
        Serialization configuration to use for serialization processing.
      • _serializationView

        protected final java.lang.Class<?> _serializationView
        View used for currently active serialization
    • Method Detail

      • setNullKeySerializer

        public abstract void setNullKeySerializer​(JsonSerializer<java.lang.Object> nks)
        Method that can be used to specify serializer that will be used to write JSON property names matching null keys for Java Maps (which will throw an exception if try write such property name)
        Since:
        1.8
      • setNullValueSerializer

        public abstract void setNullValueSerializer​(JsonSerializer<java.lang.Object> nvs)
        Method that can be used to specify serializer that will be used to write JSON values matching Java null values instead of default one (which simply writes JSON null)
        Since:
        1.8
      • setDefaultKeySerializer

        public abstract void setDefaultKeySerializer​(JsonSerializer<java.lang.Object> ks)
        Method that can be used to specify serializer to use for serializing all non-null JSON property names, unless more specific key serializer is found (i.e. if not custom key serializer has been registered for Java type).

        Note that key serializer registration are different from value serializer registrations.

        Since:
        1.8
      • serializeValue

        public abstract void serializeValue​(SerializationConfig cfg,
                                            JsonGenerator jgen,
                                            java.lang.Object value,
                                            JavaType rootType,
                                            SerializerFactory jsf)
                                     throws java.io.IOException,
                                            JsonGenerationException
        The method to be called by ObjectMapper to execute recursive serialization, using serializers that this provider has access to; and using specified root type for locating first-level serializer.
        Parameters:
        rootType - Type to use for locating serializer to use, instead of actual runtime type. Must be actual type, or one of its super types
        Throws:
        java.io.IOException
        JsonGenerationException
        Since:
        1.5
      • hasSerializerFor

        public abstract boolean hasSerializerFor​(SerializationConfig cfg,
                                                 java.lang.Class<?> cls,
                                                 SerializerFactory jsf)
        Method that can be called to see if this serializer provider can find a serializer for an instance of given class.

        Note that no Exceptions are thrown, including unchecked ones: implementations are to swallow exceptions if necessary.

      • getConfig

        public final SerializationConfig getConfig()
        Method for accessing configuration for the serialization processing.
      • isEnabled

        public final boolean isEnabled​(SerializationConfig.Feature feature)
        Convenience method for checking whether specified serialization feature is enabled or not. Shortcut for:
          getConfig().isEnabled(feature);
        
      • getSerializationView

        public final java.lang.Class<?> getSerializationView()
        Convenience method for accessing serialization view in use (if any); equivalent to:
           getConfig().getSerializationView();
        
        Since:
        1.4
      • getFilterProvider

        public final FilterProvider getFilterProvider()
        Convenience method for accessing provider to find serialization filters used, equivalent to calling:
           getConfig().getFilterProvider();
        
        Since:
        1.4
      • constructType

        public JavaType constructType​(java.lang.reflect.Type type)
        Since:
        1.8
      • constructSpecializedType

        public JavaType constructSpecializedType​(JavaType baseType,
                                                 java.lang.Class<?> subclass)
        Since:
        1.9.1
      • findValueSerializer

        public abstract JsonSerializer<java.lang.Object> findValueSerializer​(java.lang.Class<?> runtimeType,
                                                                             BeanProperty property)
                                                                      throws JsonMappingException
        Method called to get hold of a serializer for a value of given type; or if no such serializer can be found, a default handler (which may do a best-effort generic serialization or just simply throw an exception when invoked).

        Note: this method is only called for non-null values; not for keys or null values. For these, check out other accessor methods.

        Note that starting with version 1.5, serializers should also be type-aware if they handle polymorphic types. That means that it may be necessary to also use a TypeSerializer based on declared (static) type being serializer (whereas actual data may be serialized using dynamic type)

        Throws:
        JsonMappingException - if there are fatal problems with accessing suitable serializer; including that of not finding any serializer
      • findTypedValueSerializer

        public abstract JsonSerializer<java.lang.Object> findTypedValueSerializer​(java.lang.Class<?> valueType,
                                                                                  boolean cache,
                                                                                  BeanProperty property)
                                                                           throws JsonMappingException
        Method called to locate regular serializer, matching type serializer, and if both found, wrap them in a serializer that calls both in correct sequence. This method is currently only used for root-level serializer handling to allow for simpler caching. A call can always be replaced by equivalent calls to access serializer and type serializer separately.
        Parameters:
        valueType - Type for purpose of locating a serializer; usually dynamic runtime type, but can also be static declared type, depending on configuration
        cache - Whether resulting value serializer should be cached or not; this is just a hint
        Throws:
        JsonMappingException
        Since:
        1.5
      • findTypedValueSerializer

        public abstract JsonSerializer<java.lang.Object> findTypedValueSerializer​(JavaType valueType,
                                                                                  boolean cache,
                                                                                  BeanProperty property)
                                                                           throws JsonMappingException
        Method called to locate regular serializer, matching type serializer, and if both found, wrap them in a serializer that calls both in correct sequence. This method is currently only used for root-level serializer handling to allow for simpler caching. A call can always be replaced by equivalent calls to access serializer and type serializer separately.
        Parameters:
        valueType - Declared type of value being serialized (which may not be actual runtime type); used for finding both value serializer and type serializer to use for adding polymorphic type (if any)
        cache - Whether resulting value serializer should be cached or not; this is just a hint
        Throws:
        JsonMappingException
        Since:
        1.5
      • findValueSerializer

        @Deprecated
        public final JsonSerializer<java.lang.Object> findValueSerializer​(java.lang.Class<?> runtimeType)
                                                                   throws JsonMappingException
        Deprecated.
        As of version 1.7, use version that exposes property object instead of just its type (needed for contextual serializers)
        Deprecated version of accessor method that was used before version 1.7. Implemented as final to ensure that existing code does not accidentally try to redefine it (given that it is not called by core mapper code)
        Throws:
        JsonMappingException
      • findValueSerializer

        @Deprecated
        public final JsonSerializer<java.lang.Object> findValueSerializer​(JavaType serializationType)
                                                                   throws JsonMappingException
        Deprecated.
        As of version 1.7, use version that exposes property object instead of just its type (needed for contextual serializers)
        Deprecated version of accessor method that was used before version 1.7. Implemented as final to ensure that existing code does not accidentally try to redefine it (given that it is not called by core mapper code)
        Throws:
        JsonMappingException
      • findTypedValueSerializer

        @Deprecated
        public final JsonSerializer<java.lang.Object> findTypedValueSerializer​(java.lang.Class<?> valueType,
                                                                               boolean cache)
                                                                        throws JsonMappingException
        Deprecated.
        As of version 1.7, use version that exposes property object instead of just its type (needed for contextual serializers)
        Deprecated version of accessor method that was used before version 1.7. Implemented as final to ensure that existing code does not accidentally try to redefine it (given that it is not called by core mapper code)
        Throws:
        JsonMappingException
      • findTypedValueSerializer

        @Deprecated
        public final JsonSerializer<java.lang.Object> findTypedValueSerializer​(JavaType valueType,
                                                                               boolean cache)
                                                                        throws JsonMappingException
        Deprecated.
        As of version 1.7, use version that exposes property object instead of just its type (needed for contextual serializers)
        Deprecated version of accessor method that was used before version 1.7. Implemented as final to ensure that existing code does not accidentally try to redefine it (given that it is not called by core mapper code)
        Throws:
        JsonMappingException
      • getKeySerializer

        @Deprecated
        public final JsonSerializer<java.lang.Object> getKeySerializer()
                                                                throws JsonMappingException
        Deprecated.
        As of version 1.7, use version that exposes property object instead of just its type (needed for contextual serializers)
        Deprecated version of accessor method that was used before version 1.7. Implemented as final to ensure that existing code does not accidentally try to redefine it (given that it is not called by core mapper code)
        Throws:
        JsonMappingException
      • getNullKeySerializer

        public abstract JsonSerializer<java.lang.Object> getNullKeySerializer()
        Method called to get the serializer to use for serializing Map keys that are nulls: this is needed since JSON does not allow any non-String value as key, including null.

        Typically, returned serializer will either throw an exception, or use an empty String; but other behaviors are possible.

      • getNullValueSerializer

        public abstract JsonSerializer<java.lang.Object> getNullValueSerializer()
        Method called to get the serializer to use for serializing values (root level, Array members or List field values) that are nulls. Specific accessor is needed because nulls in Java do not contain type information.

        Typically returned serializer just writes out Json literal null value.

      • getUnknownTypeSerializer

        public abstract JsonSerializer<java.lang.Object> getUnknownTypeSerializer​(java.lang.Class<?> unknownType)
        Method called to get the serializer to use if provider can not determine an actual type-specific serializer to use; typically when none of SerializerFactory instances are able to construct a serializer.

        Typically, returned serializer will throw an exception, although alternatively ToStringSerializer could be returned as well.

        Parameters:
        unknownType - Type for which no serializer is found
      • defaultSerializeField

        public final void defaultSerializeField​(java.lang.String fieldName,
                                                java.lang.Object value,
                                                JsonGenerator jgen)
                                         throws java.io.IOException,
                                                JsonProcessingException
        Convenience method that will serialize given field with specified value. Value may be null. Serializer is done using the usual null) using standard serializer locating functionality.
        Throws:
        java.io.IOException
        JsonProcessingException
      • defaultSerializeDateValue

        public abstract void defaultSerializeDateValue​(long timestamp,
                                                       JsonGenerator jgen)
                                                throws java.io.IOException,
                                                       JsonProcessingException
        Method that will handle serialization of Date(-like) values, using SerializationConfig settings to determine expected serialization behavior. Note: date here means "full" date, that is, date AND time, as per Java convention (and not date-only values like in SQL)
        Throws:
        java.io.IOException
        JsonProcessingException
      • defaultSerializeDateValue

        public abstract void defaultSerializeDateValue​(java.util.Date date,
                                                       JsonGenerator jgen)
                                                throws java.io.IOException,
                                                       JsonProcessingException
        Method that will handle serialization of Date(-like) values, using SerializationConfig settings to determine expected serialization behavior. Note: date here means "full" date, that is, date AND time, as per Java convention (and not date-only values like in SQL)
        Throws:
        java.io.IOException
        JsonProcessingException
      • cachedSerializersCount

        public abstract int cachedSerializersCount()
        Method that can be used to determine how many serializers this provider is caching currently (if it does caching: default implementation does) Exact count depends on what kind of serializers get cached; default implementation caches all serializers, including ones that are eagerly constructed (for optimal access speed)

        The main use case for this method is to allow conditional flushing of serializer cache, if certain number of entries is reached.

        Since:
        1.4
      • flushCachedSerializers

        public abstract void flushCachedSerializers()
        Method that will drop all serializers currently cached by this provider. This can be used to remove memory usage (in case some serializers are only used once or so), or to force re-construction of serializers after configuration changes for mapper than owns the provider.
        Since:
        1.4