Package io.zenoh.ext

Class ZSerializer

  • All Implemented Interfaces:

    
    public abstract class ZSerializer<T extends Object>
    
                        

    Zenoh serializer.

    This class is a utility for serializing elements of type T into ZBytes.

    This class supports the following types:

    For List and Map, the inner types can be a combination of the above types, including themselves.

    Due to Java's type erasure, an actual implementation of this abstract class needs to be created (see the examples below).

    This serialization utility can be used across the Zenoh ecosystem with Zenoh versions based on other supported languages such as Rust, Python, C and C++. This works when the types are equivalent (a Byte corresponds to an i8 in Rust, a Short to an i16, etc).

    Example for a basic type, in this case an integer:

    Integer input = 123456;
    ZSerializer<Integer> serializer = new ZSerializer<>() {};
    ZBytes zbytes = serializer.serialize(input);
    
    ZDeserializer<Integer> deserializer = new ZDeserializer<>() {};
    Integer output = deserializer.deserialize(zbytes);
    assert input.equals(output);

    Examples for parameterized types:

    • List

    List<Integer> input = List.of(1, 2, 3, 4, 5);
    ZSerializer<List<Integer>> serializer = new ZSerializer<>() {};
    ZBytes zbytes = serializer.serialize(input12);
    
    ZDeserializer<List<Integer>> deserializer = new ZDeserializer<>() {};
    List<Integer> output = deserializer.deserialize(zbytes);
    assert input.equals(output);
    • Map

    Map<String, Integer> input = Map.of("one", 1, "two", 2, "three", 3);
    ZSerializer<Map<String, Integer>> serializer = new ZSerializer<>() {};
    ZBytes zbytes = serializer.serialize(input);
    
    ZDeserializer<Map<String, Integer>> deserializer = new ZDeserializer<>() {};
    Map<String, Integer> output = deserializer.deserialize(zbytes);
    assert input.equals(output);

    As mentioned, for List and Map, the inner types can be a combination of the above types, including themselves. Here's an example with a List of Maps:

    List<Map<String, Integer>> input = List.of(Map.of("a", 1, "b", 2));
    ZSerializer<List<Map<String, Integer>>> serializer = new ZSerializer<>() {};
    ZBytes zbytes = serializer.serialize(input);
    
    ZDeserializer<List<Map<String, Integer>>> deserializer = new ZDeserializer<>() {};
    List<Map<String, Integer>> output = deserializer.deserialize(zbytes);
    assert input.equals(output);

    For more examples, see the ZBytesExamples in the examples.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      ZSerializer()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final ZBytes serialize(T t) Serialize t into a ZBytes.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait