Class DefaultObjectMapper

java.lang.Object
eu.cloudnetservice.driver.network.rpc.defaults.object.DefaultObjectMapper
All Implemented Interfaces:
ObjectMapper

@Singleton @Provides(ObjectMapper.class) public class DefaultObjectMapper extends Object implements ObjectMapper
The default object mapper implementation.
Since:
4.0
  • Field Details

    • DEFAULT_MAPPER

      public static final ObjectMapper DEFAULT_MAPPER
    • DEFAULT_SERIALIZERS

      private static final Map<Type,ObjectSerializer<?>> DEFAULT_SERIALIZERS
    • registeredSerializers

      private final Map<Type,ObjectSerializer<?>> registeredSerializers
    • typeCache

      private final com.github.benmanes.caffeine.cache.LoadingCache<Type,Collection<eu.cloudnetservice.common.tuple.Tuple2<Type,Type>>> typeCache
  • Constructor Details

    • DefaultObjectMapper

      public DefaultObjectMapper()
      Constructs a new default object mapper instance with all default object serializers already registered. This call is equivalent to new DefaultObjectMapper(true).
    • DefaultObjectMapper

      public DefaultObjectMapper(boolean registerDefaultSerializers)
      Constructs a new default object mapper instance and optionally registers the default serializers to it.
      Parameters:
      registerDefaultSerializers - true if the default serializers should get registered, false otherwise.
  • Method Details

    • unregisterBinding

      @NonNull public @NonNull ObjectMapper unregisterBinding(@NonNull @NonNull Type type, boolean superTypes)
      Unregisters all object serializers which were registered for the given type, include all super types of the given type if requested.
      Specified by:
      unregisterBinding in interface ObjectMapper
      Parameters:
      type - the base type of the mappers to unregister.
      superTypes - if all super types of the base type should get included when unregistering.
      Returns:
      the same instance as used to call the method, for chaining.
    • unregisterBindings

      @NonNull public @NonNull ObjectMapper unregisterBindings(@NonNull @NonNull ClassLoader classLoader)
      Unregisters all object serializers whose classes were loaded by the given class loader.
      Specified by:
      unregisterBindings in interface ObjectMapper
      Parameters:
      classLoader - the class loader of the object serializers to unregister.
      Returns:
      the same instance as used to call the method, for chaining.
    • registerBinding

      @NonNull public <T> @NonNull ObjectMapper registerBinding(@NonNull @NonNull Type type, @NonNull @NonNull ObjectSerializer<T> serializer, boolean superTypes)
      Registers the given object serializer to this mapper, including all super types of the given type if requested. This call will silently be ignored if previously a mapper was registered for one of the requested types.
      Specified by:
      registerBinding in interface ObjectMapper
      Type Parameters:
      T - the generic type of the object serializer to register.
      Parameters:
      type - the type which can be (de-) serialized using the given serializer.
      serializer - the serializer to use for the type.
      superTypes - if all super types of the given type should get associated with the given serializer as well.
      Returns:
      the same instance as used to call the method, for chaining.
    • writeObject

      @NonNull public <T> DataBuf.Mutable writeObject(@NonNull DataBuf.Mutable dataBuf, @Nullable T object)
      Writes the given object into the given data buffer using the previously registered object serializers.
      Specified by:
      writeObject in interface ObjectMapper
      Type Parameters:
      T - the generic type of the object to serialize.
      Parameters:
      dataBuf - the buffer to serialize the data to.
      object - the object to serialize into the buffer, can be null.
      Returns:
      the same buffer used to call the method, for further writing.
    • readObject

      @Nullable public <T> T readObject(@NonNull @NonNull DataBuf dataBuf, @NonNull @NonNull Type type)
      Reads an object from the given buffer by the given type using the previously registered serializers. The returned object might be null if the object written to the buffer was null as well.
      Specified by:
      readObject in interface ObjectMapper
      Type Parameters:
      T - the generic type of the object.
      Parameters:
      dataBuf - the buffer from which the object should get deserialized.
      type - the type of the object to deserialize.
      Returns:
      the deserialized object from the buffer, can be null.
    • serializerForType

      @Nullable protected <T> @Nullable ObjectSerializer<T> serializerForType(@NonNull @NonNull eu.cloudnetservice.common.tuple.Tuple2<Type,Type> typePair)
      Finds the best matching serializer for the given type. The method first tries to get the serializer by the exact type of the supplied type token, then by the raw type.
      Type Parameters:
      T - the generic type of the object serializer to get.
      Parameters:
      typePair - the mapping pair of the actual type (first) and the raw type (second) of the serializer to get.
      Returns:
      the best matching object serializer for the given type.
      Throws:
      NullPointerException - if the given type token is null.