Class ToStringSerializer<T>

  • Type Parameters:
    T - class of the entity, representing messages
    All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, org.apache.kafka.common.serialization.Serializer<T>

    public class ToStringSerializer<T>
    extends java.lang.Object
    implements org.apache.kafka.common.serialization.Serializer<T>
    Generic Serializer that relies on Object.toString() to get serialized representation of the entity.
    Since:
    2.5
    Author:
    Alexei Klenin, Gary Russell
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ADD_TYPE_INFO_HEADERS
      Kafka config property for enabling/disabling adding type headers.
      static java.lang.String KEY_TYPE
      Header for the type of key.
      static java.lang.String VALUE_TYPE
      Header for the type of value.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void configure​(java.util.Map<java.lang.String,​?> configs, boolean isKey)  
      java.nio.charset.Charset getCharset()
      Get the configured charset.
      boolean isAddTypeInfo()
      Get the addTypeInfo property.
      byte[] serialize​(java.lang.String topic, org.apache.kafka.common.header.Headers headers, T data)  
      byte[] serialize​(java.lang.String topic, T data)  
      void setAddTypeInfo​(boolean addTypeInfo)
      Set to false to disable adding type info headers.
      void setCharset​(java.nio.charset.Charset charset)
      Set a charset to use when converting String to byte[].
      • Methods inherited from class java.lang.Object

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

      • ADD_TYPE_INFO_HEADERS

        public static final java.lang.String ADD_TYPE_INFO_HEADERS
        Kafka config property for enabling/disabling adding type headers.
        See Also:
        Constant Field Values
      • KEY_TYPE

        public static final java.lang.String KEY_TYPE
        Header for the type of key.
        See Also:
        Constant Field Values
      • VALUE_TYPE

        public static final java.lang.String VALUE_TYPE
        Header for the type of value.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ToStringSerializer

        public ToStringSerializer()
    • Method Detail

      • configure

        public void configure​(java.util.Map<java.lang.String,​?> configs,
                              boolean isKey)
        Specified by:
        configure in interface org.apache.kafka.common.serialization.Serializer<T>
      • serialize

        public byte[] serialize​(java.lang.String topic,
                                @Nullable
                                T data)
        Specified by:
        serialize in interface org.apache.kafka.common.serialization.Serializer<T>
      • serialize

        @Nullable
        public byte[] serialize​(java.lang.String topic,
                                @Nullable
                                org.apache.kafka.common.header.Headers headers,
                                @Nullable
                                T data)
        Specified by:
        serialize in interface org.apache.kafka.common.serialization.Serializer<T>
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface org.apache.kafka.common.serialization.Serializer<T>
      • isAddTypeInfo

        public boolean isAddTypeInfo()
        Get the addTypeInfo property.
        Returns:
        the addTypeInfo
      • setAddTypeInfo

        public void setAddTypeInfo​(boolean addTypeInfo)
        Set to false to disable adding type info headers.
        Parameters:
        addTypeInfo - true to add headers
      • setCharset

        public void setCharset​(java.nio.charset.Charset charset)
        Set a charset to use when converting String to byte[]. Default UTF-8.
        Parameters:
        charset - the charset.
      • getCharset

        public java.nio.charset.Charset getCharset()
        Get the configured charset.
        Returns:
        the charset.