Interface ResponseWriter

All Known Implementing Classes:
ByteBufResponseWriter, LuaResponseWriter

public interface ResponseWriter
Entrypoint to serialize a response in RESP format.

The class provides specific methods for the RESP types. In cases where the response object type is unknown or heterogeneous during runtime, there is a generic method.

During serialization, the class introspects the registry for a serializer capable of handling the object before proceeding. Utilizing a specific method reduces the number of candidates to check. The generic method verifies all serializers in the registry and short circuits after the first match. However, this could cause many checks until the correct serializer. Give preference to specific methods.

The method throws an exception when an object does not have a serializer.

Since:
15.0
Author:
José Bolina
  • Field Details Link icon

  • Method Details Link icon

    • isInternal Link icon

      boolean isInternal()
    • version Link icon

      RespVersion version()
    • version Link icon

      void version(RespVersion version)
    • nulls Link icon

      void nulls()
      Writes the null value.
    • ok Link icon

      void ok()
      Serializes a simple string with the "OK" content.
    • queued Link icon

      void queued(Object ignore)
      Serializes a simple string with the "QUEUED" content.
      Parameters:
      ignore - Content to ignore.
    • simpleString Link icon

      void simpleString(CharSequence value)
      Serializes a char sequence in a simple string format.
      Parameters:
      value - The ASCII string to serialize.
    • string Link icon

      void string(CharSequence value)
      Serializes a char sequence in a bulk string format.
      Parameters:
      value - The string to serialize.
    • string Link icon

      void string(byte[] value)
      Serializes the binary blob in a bulk string format.
      Parameters:
      value - The binary blob to serialize.
    • integers Link icon

      void integers(Number value)
      Serializes a 64-bit number in the integer format.
      Parameters:
      value - Number to serialize.
    • doubles Link icon

      void doubles(Number value)
      Serializes a double-precision floating point into the doubles format.
      Parameters:
      value - The floating point to serialize.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • booleans Link icon

      void booleans(boolean value)
      Serializes a boolean value in the RESP3 format.
      Parameters:
      value - Boolean value to serialize.
    • arrayEmpty Link icon

      void arrayEmpty()
      Serializes a collection in the array format.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • array Link icon

      <T> void array(Collection<T> collection, JavaObjectSerializer<T> serializer)
      Serializes the collection by delegating the serialization of the elements to the provided serializer.
      Type Parameters:
      T - The type of the elements in the collection and the serializer handles.
      Parameters:
      collection - Collection of elements to serialize.
      serializer - Serializer for a single element in the collection.
    • array Link icon

      void array(Collection<?> collection, Resp3Type contentType)
      Serializes a collection in the array format with elements of a specified type.
      Parameters:
      collection - Collection of heterogeneous values to serialize.
      contentType - The type of elements contained by the sequence.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • emptySet Link icon

      void emptySet()
      Serializes a set
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • set Link icon

      void set(Set<?> set, Resp3Type contentType)
      Serializes a set with elements of a specified type.
      Parameters:
      set - Set of heterogeneous values to serialize.
      contentType - The type of elements contained by the set.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • set Link icon

      <T> void set(Set<?> set, JavaObjectSerializer<T> serializer)
    • map Link icon

      void map(Map<?,?> value)
      Serializes a map.
      Parameters:
      value - A map with heterogeneous key-value tuples to serialize.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • map Link icon

      void map(Map<?,?> value, Resp3Type contentType)
      Serializes a map with key and values of the same specified type.
      Parameters:
      value - A map with heterogeneous key-value tuples to serialize.
      contentType - The type of key and value elements.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • map Link icon

      void map(Map<?,?> value, Resp3Type keyType, Resp3Type valueType)
      Serializes a map in the RESP3 map format with keys and values of a specified format.
      Parameters:
      value - A map with heterogeneous key-value tuples to serialize.
      keyType - The type of keys in the map.
      valueType - The type of values in the map.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • map Link icon

      void map(Map<?,?> map, SerializationHint.KeyValueHint keyValueHint)
    • error Link icon

      void error(CharSequence value)
      Serializes an error message.

      The first character in the error message must be the '-' symbol.

      Parameters:
      value - An ASCII char sequence with the error message.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • error Link icon

      void error(Throwable t)
      Serializes the exception message in the RESP3 error format.
      Parameters:
      t - The throwable to serialize.
    • write Link icon

      <T> void write(T object, JavaObjectSerializer<T> serializer)
      Writes an object utilizing the specific serializer.

      Implementors do not need to check for nullability in the implementation. Null response values are handled by native RESP3 serializers before passing the serialization ahead.

      Type Parameters:
      T - The type of the object.
      Parameters:
      object - The element to serialize.
      serializer - The serializer to utilize.
    • write Link icon

      <T> void write(JavaObjectSerializer<T> serializer)
      Invokes the callback to write a response with the serializer.
      Parameters:
      serializer - Serializer to write.
    • serialize Link icon

      void serialize(Object object)
      Generic method to serialize an object of unknown type.

      This method searches all serializers available in the registry.

      Parameters:
      object - The object to serialize in RESP3 format.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • writeNumericPrefix Link icon

      void writeNumericPrefix(byte symbol, long number)
    • syntaxError Link icon

      default void syntaxError()
    • unknownCommand Link icon

      default void unknownCommand()
    • unauthorized Link icon

      default void unauthorized()
    • customError Link icon

      default void customError(String error)
    • wrongArgumentCount Link icon

      default void wrongArgumentCount(RespCommand command)
    • nanOrInfinity Link icon

      default void nanOrInfinity()
    • valueNotInteger Link icon

      default void valueNotInteger()
    • valueNotAValidFloat Link icon

      default void valueNotAValidFloat()
    • minOrMaxNotAValidFloat Link icon

      default void minOrMaxNotAValidFloat()
    • minOrMaxNotAValidStringRange Link icon

      default void minOrMaxNotAValidStringRange()
    • transactionAborted Link icon

      default void transactionAborted()
    • mustBePositive Link icon

      default void mustBePositive(String argumentName)
    • mustBePositive Link icon

      default void mustBePositive()
    • wrongArgumentNumber Link icon

      default void wrongArgumentNumber(RespCommand command)
    • indexOutOfRange Link icon

      default void indexOutOfRange()
    • noSuchKey Link icon

      default void noSuchKey()
    • wrongType Link icon

      default void wrongType()
    • handleException Link icon

      static Consumer<ResponseWriter> handleException(Throwable t)
    • arrayStart Link icon

      void arrayStart(int size)
      Start writing an array
      Parameters:
      size - the number of elements
    • arrayEnd Link icon

      void arrayEnd()
      Finish writing an array
    • arrayNext Link icon

      void arrayNext()
      Start writing a new array element