Class DataClassSerializer

java.lang.Object
eu.cloudnetservice.driver.network.rpc.defaults.object.data.DataClassSerializer
All Implemented Interfaces:
ObjectSerializer<Object>

public final class DataClassSerializer extends Object implements ObjectSerializer<Object>
A serializer for all data classes and arrays which have no special serializer available.
Since:
4.0
  • Field Details

  • Constructor Details

    • DataClassSerializer

      public DataClassSerializer()
  • Method Details

    • ensureClassIsInstantiable

      private static void ensureClassIsInstantiable(@NonNull @NonNull Class<?> clazz)
      Validates that an instance of the given class can be constructed in runtime. This means that the class is not abstract, an interface and does not represent a primitive type.
      Parameters:
      clazz - the class to check.
      Throws:
      IllegalArgumentException - if the given class is not instantiable.
      NullPointerException - if the given class is null.
    • read

      Reads the object from the buffer. This method should only read the content from the buffer which was written to it previously, never more as the given buffer is not a slice of the buffer representing the object being read. Although this method returns an object, the returned object must always match the given type during object construction. The object return type is just a helper, but the return value will be cast to the given type resulting in errors if provided with the wrong type of object.
      Specified by:
      read in interface ObjectSerializer<Object>
      Parameters:
      source - the buffer to read the object from.
      type - the type of the object to read.
      caller - the mapper to which this serializer is registered and from which the deserialize request call came.
      Returns:
      the deserialized object of the serializer type T, or null if not readable.
    • write

      public void write(@NonNull DataBuf.Mutable dataBuf, @NonNull @NonNull Object object, @NonNull @NonNull Type type, @NonNull @NonNull ObjectMapper caller)
      Writes the given object into the given buffer for network transfer and later deserialization.
      Specified by:
      write in interface ObjectSerializer<Object>
      Parameters:
      dataBuf - the buffer to write the data to.
      object - the object to serialize.
      type - the raw type of the object.
      caller - the object mapper to which this serializer belongs and which requested the serialisation.
    • createDataClassCodec

      @NonNull private @NonNull DataClassCodec createDataClassCodec(@NonNull @NonNull Class<?> targetType)
      Generates the data class codec for the given target type. The result of the method is cached, and should not be called directly due to some heavy work that is required to construct the data class codec.
      Parameters:
      targetType - the target type to construct the data class codec for.
      Returns:
      the generated data class codec for the given target type.
      Throws:
      IllegalStateException - if the given target type is missing an all-args constructor.
      NullPointerException - if the given target type is null.