Interface JavaObjectSerializer<T>

Type Parameters:
T - The type of object the instance is handling. This type filter the objects during runtime, accepting types of T and subclasses.
All Superinterfaces:
BiConsumer<T,ResponseWriter>, Predicate<Object>, ResponseSerializer<T,ResponseWriter>
All Known Implementing Classes:
ScoredValueSerializer, ZSetCommonUtils.ZOperationResponse
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface JavaObjectSerializer<T> extends ResponseSerializer<T,ResponseWriter>
Base class for serializing custom Java objects into the final RESP3 format.

Extend this class to add support to a Java object type. This mechanism is useful when handling methods that return an object instead of the primitive values. Also, nested data structures with heterogeneous elements must provide an instance of JavaObjectSerializer to correctly the elements.

An example of how to utilize the serializer when handling a heterogeneous map. The map contains string and integer values.
 
 class MyCustomSerializer extends JavaObjectSerializer<Map<String, Object>> {

    @Override
    public void accept(Date date, ByteBufPool alloc) {
       // Write map prefix, then the keys and values.
       // Utilize the base pieces from the Resp3Response class to write the primitive values.
    }

    public static void main(String[] args) {
       ByteBufPool alloc = ...;
       Map<String, Object> map = Map.of("k1", "v1", "k2", 42);
       Resp3Response.write(map, alloc, new MyCustomSerializer());
       Resp3Response.map(map, alloc, new MyCustomSerializer());
    }
 }
 
 

The caller can also provide a lambda that receives the response and the ByteBufPool as arguments for serialization. If a serializer is stateless, it can be a singleton and shared for all invocations.

  • Method Summary

    Modifier and Type
    Method
    Description
    default boolean
    test(Object object)
     

    Methods inherited from interface java.util.function.BiConsumer

    accept, andThen

    Methods inherited from interface java.util.function.Predicate

    and, negate, or
  • Method Details