Class Serializer

  • Direct Known Subclasses:
    BinarySerializer, FastBinarySerializer, JsonSerializer, PrettySerializer, UrlEncodedSerializer

    public abstract class Serializer
    extends java.lang.Object
    Thrift serializers are stateless injectable implementation classes that transforms messages to binary stream (serializes), or binary stream to messages (deserializes). Since the serializer is state-less it should also be inherently thread safe (including not needing any synchronized methods.
    • Field Detail

      • DEFAULT_STRICT

        public static final boolean DEFAULT_STRICT
        Default value for the "strict" serializer settings.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Serializer

        public Serializer()
    • Method Detail

      • serialize

        public abstract <Message extends PMessage<Message>> int serialize​(@Nonnull
                                                                          java.io.OutputStream output,
                                                                          @Nonnull
                                                                          PMessageOrBuilder<Message> message)
                                                                   throws java.io.IOException
        Serialize a message.
        Type Parameters:
        Message - The message type.
        Parameters:
        output - The output stream to write to.
        message - The message to write.
        Returns:
        The number of bytes written to the output stream.
        Throws:
        java.io.IOException - If writing or serialization failed.
      • serialize

        public abstract <Message extends PMessage<Message>> int serialize​(@Nonnull
                                                                          java.io.OutputStream output,
                                                                          @Nonnull
                                                                          PServiceCall<Message> call)
                                                                   throws java.io.IOException
        Serialize a service call.
        Type Parameters:
        Message - The message type contained in the service call.
        Parameters:
        output - The output stream to write to.
        call - The service call to write.
        Returns:
        The number of bytes written to the output stream.
        Throws:
        java.io.IOException - If writing or serialization failed.
      • deserialize

        @Nonnull
        public abstract <Message extends PMessage<Message>> Message deserialize​(@Nonnull
                                                                                java.io.InputStream input,
                                                                                @Nonnull
                                                                                PMessageDescriptor<Message> descriptor)
                                                                         throws java.io.IOException
        Deserialize a message.
        Type Parameters:
        Message - The message type.
        Parameters:
        input - The input stream to read from.
        descriptor - The descriptor of the message to read.
        Returns:
        The message read.
        Throws:
        java.io.IOException - If reading or deserialization failed.
      • deserialize

        @Nonnull
        public abstract <Message extends PMessage<Message>> PServiceCall<Message> deserialize​(@Nonnull
                                                                                              java.io.InputStream input,
                                                                                              @Nonnull
                                                                                              PService service)
                                                                                       throws java.io.IOException
        Deserialize a service call.
        Type Parameters:
        Message - The message type contained in the returned service call.
        Parameters:
        input - The input stream to read from.
        service - The service definition to read for.
        Returns:
        The service call read.
        Throws:
        java.io.IOException - If reading or deserialization failed.
      • binaryProtocol

        public abstract boolean binaryProtocol()
        Returns:
        True if the protocol of the serializer is "binary" a.k.a. unreadable to most humans.
      • verifyEndOfContent

        public abstract void verifyEndOfContent​(@Nonnull
                                                java.io.InputStream input)
                                         throws java.io.IOException
        Verify that the input stream does not contain any more readable content and close the stream. Throws exception if unable to verify, or the streams contains any meaningful content.
        Parameters:
        input - The input stream to check.
        Throws:
        java.io.IOException - If unable to read from stream or stream contains meaningful content.
      • mediaType

        @Nonnull
        public abstract java.lang.String mediaType()
        Returns:
        Media type of the content generated by the serializer. Can be used for Content-Type and Accept HTTP headers, MIME type etc.
      • isRequestCallType

        protected boolean isRequestCallType​(PServiceCallType type)
        Utility method to check if a service call is a request type.
        Parameters:
        type - The service call type.
        Returns:
        If the service call is a request, and not a response.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object