Class SerializerBuilder

java.lang.Object
io.activej.serializer.SerializerBuilder
All Implemented Interfaces:
WithInitializer<SerializerBuilder>

public final class SerializerBuilder extends Object implements WithInitializer<SerializerBuilder>
Scans fields of classes for serialization.
  • Method Details

    • create

      public static SerializerBuilder create()
      Creates a new instance of SerializerBuilder with newly created DefiningClassLoader
    • create

      public static SerializerBuilder create(DefiningClassLoader definingClassLoader)
      Creates a new instance of SerializerBuilder with external DefiningClassLoader
    • with

      public SerializerBuilder with(io.activej.types.TypeT<?> typeT, io.activej.types.scanner.TypeScannerRegistry.Mapping<SerializerDef> fn)
      Adds a mapping to resolve a SerializerDef for a given TypeT
      Parameters:
      typeT - a type token
      fn - a mapping to resolve a serializer
    • with

      public SerializerBuilder with(Type type, io.activej.types.scanner.TypeScannerRegistry.Mapping<SerializerDef> fn)
      Adds a mapping to resolve a SerializerDef for a given Type
      Parameters:
      type - a type
      fn - a mapping to resolve a serializer
    • withImplementationClass

      public SerializerBuilder withImplementationClass(Class<?> implementationClass)
      Adds an implementation class for the serializer
      Parameters:
      implementationClass - an implementation class
    • withCompatibilityLevel

      public SerializerBuilder withCompatibilityLevel(CompatibilityLevel compatibilityLevel)
      Sets a given CompatibilityLevel for the serializer. This method should be used to ensure backwards compatibility with previous versions of serializers
      Parameters:
      compatibilityLevel - a compatibility level
    • withAnnotationCompatibilityMode

      public SerializerBuilder withAnnotationCompatibilityMode()
      Enables annotation compatibility mode
      See Also:
    • withAnnotationCompatibilityMode

      public SerializerBuilder withAnnotationCompatibilityMode(boolean annotationsCompatibilityMode)
      Enables or disables annotation compatibility mode

      In previous ActiveJ versions serializer annotations had to be placed directly on fields/getters. To specify a concrete annotated type a path attribute was used. Now it is possible to annotate types directly. However, for compatibility with classes annotated using a path attribute or when using older versions of Java that may fail resolving type use annotations, this compatibility mode can be enabled

    • withAnnotationAlias

      public <A extends Annotation, T extends Annotation> SerializerBuilder withAnnotationAlias(Class<A> annotation, Class<T> annotationAlias, Function<T,A> mapping)
      Adds alias annotation for a serializer annotation. Alias annotation acts as if it is a regular serializer annotation
      Type Parameters:
      A - a type of serializer annotation
      T - a type of alias annotation
      Parameters:
      annotation - a serializer annotation
      annotationAlias - an alias annotation
      mapping - a function that transforms an alias annotation into a serializer annotation
    • withEncodeVersion

      public SerializerBuilder withEncodeVersion(int encodeVersionMax)
      Sets maximal encode version

      This method is used to ensure compatibility between different versions of serialized objects

      Parameters:
      encodeVersionMax - a maximal encode version
    • withDecodeVersions

      public SerializerBuilder withDecodeVersions(int decodeVersionMin, int decodeVersionMax)
      Sets both minimal and maximal decode versions

      This method is used to ensure compatibility between different versions of serialized objects

      Parameters:
      decodeVersionMin - a minimal decode version
      decodeVersionMax - a maximal decode version
    • withVersions

      public SerializerBuilder withVersions(int encodeVersionMax, int decodeVersionMin, int decodeVersionMax)
      Sets maximal encode version as well as both minimal and maximal decode versions

      This method is used to ensure compatibility between different versions of serialized objects

      Parameters:
      encodeVersionMax - a maximal encode version
      decodeVersionMin - a minimal decode version
      decodeVersionMax - a maximal decode version
    • withAutoOrdering

      public SerializerBuilder withAutoOrdering(int autoOrderingStart, int autoOrderingStride)
      Sets auto ordering parameters (used when no explicit ordering is set)
      Parameters:
      autoOrderingStart - a value of initial order index
      autoOrderingStride - a step between indices
    • withProfile

      public SerializerBuilder withProfile(String profile)
      Sets a serializer profile
      Parameters:
      profile - a serializer profile
    • withSubclasses

      public <T> SerializerBuilder withSubclasses(String subclassesId, List<Class<? extends T>> subclasses)
      Sets subclasses to be serialized. Uses custom string id to identify subclasses
      Type Parameters:
      T - a parent of subclasses
      Parameters:
      subclassesId - an id of subclasses
      subclasses - actual subclasses classes
    • withSubclasses

      public <T> SerializerBuilder withSubclasses(Class<T> type, List<Class<? extends T>> subclasses)
      Sets subclasses to be serialized. Uses parent class to identify subclasses
      Type Parameters:
      T - a parent type of subclasses
      Parameters:
      type - a parent class of subclasses
      subclasses - actual subclasses classes
    • build

      public <T> BinarySerializer<T> build(Type type)
      Builds a BinarySerializer out of this SerializerBuilder.
      See Also:
    • build

      public <T> BinarySerializer<T> build(Class<T> type)
      Builds a BinarySerializer out of this SerializerBuilder.
      See Also:
    • build

      public <T> BinarySerializer<T> build(io.activej.types.TypeT<T> typeT)
      Builds a BinarySerializer out of this SerializerBuilder.
      See Also:
    • build

      public <T> BinarySerializer<T> build(AnnotatedType type)
      Builds a BinarySerializer out of this SerializerBuilder.

      Parameters:
      type - a type data that would be serialized
      Returns:
      a generated BinarySerializer
    • build

      public <T> BinarySerializer<T> build(String className, Type type)
      Builds a BinarySerializer out of this SerializerBuilder.
      See Also:
    • build

      public <T> BinarySerializer<T> build(String className, Class<T> type)
      Builds a BinarySerializer out of this SerializerBuilder.
      See Also:
    • build

      public <T> BinarySerializer<T> build(String className, io.activej.types.TypeT<T> typeT)
      Builds a BinarySerializer out of this SerializerBuilder.
      See Also:
    • build

      public <T> BinarySerializer<T> build(String className, AnnotatedType type)
      Builds a BinarySerializer out of this SerializerBuilder.

      A built serializer would have a class name equal to the one that passed to this method.

      If classLoader has already defined the serializer class, the class would be taken from the class loader's cache. Moreover, if a DefiningClassLoader has a persistent BytecodeStorage, the serializer class would be taken from that persistent cache.

      Parameters:
      className - a name of the class of a serializer
      type - a type data that would be serialized
      Returns:
      a generated BinarySerializer
    • build

      public <T> BinarySerializer<T> build(SerializerDef serializer)
      Builds a BinarySerializer out of some SerializerDef.
      Parameters:
      serializer - a SerializerDef that would be used to create a BinarySerializer
      Returns:
      a generated BinarySerializer
    • toClassBuilder

      public <T> ClassBuilder<BinarySerializer<T>> toClassBuilder(SerializerDef serializer)
      Type Parameters:
      T - a type of data to be serialized by a BinarySerializer
      Parameters:
      serializer - a serializer definition
      Returns:
      a ClassBuilder of BinarySerializer