Class DispatchCodec<K,V>

java.lang.Object
com.github.darksoulq.abyssallib.common.serialization.codecs.DispatchCodec<K,V>
Type Parameters:
K - Discriminator key type.
V - Polymorphic value type.
All Implemented Interfaces:
Codec<V>

public class DispatchCodec<K,V> extends Object implements Codec<V>
Codec supporting polymorphic serialization through a discriminator field.
  • Constructor Details

    • DispatchCodec

      public DispatchCodec(String typeKey, Codec<K> keyCodec, Function<? super V, ? extends K> typeGetter, Function<? super K, ? extends Codec<? extends V>> codecGetter)
      Creates a dispatch codec.
      Parameters:
      typeKey - Name of the discriminator field.
      keyCodec - Codec used for discriminator values.
      typeGetter - Extracts the discriminator from a value.
      codecGetter - Resolves a codec from a discriminator value.
  • Method Details

    • decode

      public <D> DataResult<V> decode(DynamicOps<D> ops, D input)
      Description copied from interface: Codec
      Decodes a serialized input of a specific data format into a Java object.
      Specified by:
      decode in interface Codec<K>
      Type Parameters:
      D - The type of the serialized data.
      Parameters:
      ops - The provider defining how to navigate and read data of type D.
      input - The raw serialized input to be processed.
      Returns:
      A DataResult containing the decoded object or an error state.
    • encode

      public <D> DataResult<D> encode(DynamicOps<D> ops, V value)
      Description copied from interface: Codec
      Encodes a Java object into its serialized representation.
      Specified by:
      encode in interface Codec<K>
      Type Parameters:
      D - The target type of the serialized data.
      Parameters:
      ops - The provider defining how to construct data of type D.
      value - The Java object instance to be serialized.
      Returns:
      A DataResult containing the serialized data or an error state.
    • describe

      public String describe()
      Description copied from interface: Codec
      Outputs a structural description of the expected format for this codec. Useful for schema generation and validation debugging.
      Specified by:
      describe in interface Codec<K>
      Returns:
      The schema definition representing the target type.
    • accept

      public <R> R accept(CodecVisitor<R> visitor)
      Description copied from interface: Codec
      Routes this instance into the visitor API to construct contextual schema layouts.
      Specified by:
      accept in interface Codec<K>
      Type Parameters:
      R - The layout target natively returning explicitly mapped variants.
      Parameters:
      visitor - The evaluation logic instance.
      Returns:
      Evaluated variant functionally extracting outputs.