Class CompactSerializationService

java.lang.Object
com.loomcache.common.serialization.compact.CompactSerializationService

public final class CompactSerializationService extends Object
Registry and entry point for Compact serialization: registers CompactTypeSerializers and encodes/decodes Compact payloads.

Applications register a serializer per class (rejecting conflicting duplicate registrations), then call serialize/deserialize to convert instances to and from schema-bound payloads. serializeRecord(GenericRecord) and deserializeRecord(byte[]) handle GenericRecords for class-free access, and schemaOf(byte[]) extracts a payload's CompactSchema. The registry is thread-safe; the same serializers must be installed consistently across all nodes that read or write a given type.

The public compatibility contract is the schema contract: type name, ordered field names, field kinds, nullability, and the derived fingerprint. Compact bytes are an opaque LoomCache payload; use this service rather than parsing or constructing the private binary layout directly. Changing a type name or field kind is incompatible. Adding fields is compatible only when older readers provide defaults for missing fields; removing fields is compatible only when newer readers no longer require them.

  • Constructor Details

    • CompactSerializationService

      public CompactSerializationService()
  • Method Details

    • register

      public <T> void register(CompactTypeSerializer<T> serializer)
      Registers the serializer for its declared Java type.

      All clients and members that exchange the type must register equivalent serializers with the same Compact type name and field contract before writing values that depend on them.

      Type Parameters:
      T - application type handled by the serializer
      Parameters:
      serializer - serializer to register
      Throws:
      NullPointerException - if the serializer, type, or type name is null
      IllegalArgumentException - if the type name is blank or a different serializer is already registered for the same Java class
    • serialize

      public <T> byte[] serialize(T value)
      Serializes value using the serializer registered for its runtime class.
      Type Parameters:
      T - runtime type of the value
      Parameters:
      value - non-null value to serialize
      Returns:
      opaque Compact payload bytes
      Throws:
      SerializationException - if no serializer is registered or the payload is invalid
    • serialize

      public <T> byte[] serialize(T value, Class<T> sourceType)
      Serializes value using the serializer registered for sourceType.
      Type Parameters:
      T - source type
      Parameters:
      value - non-null value to serialize
      sourceType - registered source type
      Returns:
      opaque Compact payload bytes
      Throws:
      IllegalArgumentException - if value is not an instance of sourceType
      SerializationException - if no serializer is registered or the payload is invalid
    • deserialize

      public <T> T deserialize(byte[] payload, Class<T> targetType)
      Deserializes a Compact payload with the serializer registered for targetType.
      Type Parameters:
      T - target type
      Parameters:
      payload - opaque Compact payload bytes
      targetType - target Java type
      Returns:
      decoded value
      Throws:
      SerializationException - if the payload is malformed, no serializer is registered, or the payload type name does not match the target serializer
    • serializeRecord

      public byte[] serializeRecord(GenericRecord record)
      Serializes a class-free GenericRecord as an opaque Compact payload.
      Parameters:
      record - record to serialize
      Returns:
      opaque Compact payload bytes
    • deserializeRecord

      public GenericRecord deserializeRecord(byte[] payload)
      Decodes a Compact payload as a class-free GenericRecord.
      Parameters:
      payload - opaque Compact payload bytes
      Returns:
      decoded generic record
      Throws:
      SerializationException - if the payload is malformed
    • schemaOf

      public CompactSchema schemaOf(byte[] payload)
      Extracts the schema carried by a Compact payload.
      Parameters:
      payload - opaque Compact payload bytes
      Returns:
      decoded schema
      Throws:
      SerializationException - if the payload is malformed