Interface Encoder<A>

Type Parameters:
A - the type of object to encode
All Known Subinterfaces:
Codec<A>, PrimitiveCodec<A>
All Known Implementing Classes:
CompoundListCodec, EitherCodec, ListCodec, MapCodec.MapCodecCodec, PairCodec, UnboundedMapCodec

public interface Encoder<A>
Encodes objects to a serialized form.
  • Method Details

    • encode

      <T> DataResult<T> encode(A input, DynamicOps<T> ops, T prefix)
      Encodes an object to a serialized form, adding to an existing prefix.
      Type Parameters:
      T - the type to serialize to
      Parameters:
      input - the object to encode
      ops - a DynamicOps for the target serialized format
      prefix - existing data that the encoded object should be added to
      Returns:
      a DataResult containing the serialized form of the object, or an error if the object could not be serialized
    • encodeStart

      default <T> DataResult<T> encodeStart(DynamicOps<T> ops, A input)
      Encode an object to a serialized form.
      Type Parameters:
      T - the type to serialize to
      Parameters:
      ops - a DynamicOps for the target serialized format
      input - the object to encode
      Returns:
      a DataResult containing the serialized form of the object, or an error if the object could not be serialized
    • fieldOf

      default MapEncoder<A> fieldOf(String name)
      Returns a MapEncoder that encodes a single field with the given name using this encoder.
      Returns:
      a MapEncoder that encodes a single field with the given name using this encoder
    • comap

      default <B> Encoder<B> comap(Function<? super B,? extends A> function)
      Transforms the type of this encoder using the given function.
      Type Parameters:
      B - the type of object for the new encoder to encode
      Parameters:
      function - a function to apply to the input before encoding
      Returns:
      a new encoder
    • flatComap

      default <B> Encoder<B> flatComap(Function<? super B,? extends DataResult<? extends A>> function)
      Useful when the type to encode cannot always be converted to the type expected by this encoder.
      Type Parameters:
      B - the type of object for the new encoder to encode
      Parameters:
      function - a function to apply to the input before encoding
      Returns:
      a new encoder
    • withLifecycle

      default Encoder<A> withLifecycle(Lifecycle lifecycle)
      Produces a new encoder with the given Lifecycle.
      Parameters:
      lifecycle - the lifecycle to apply to the new encoder
      Returns:
      a new encoder
    • empty

      static <A> MapEncoder<A> empty()
      Produces a MapEncoder which encodes no fields, parameterized by a type.
      Type Parameters:
      A - the type of object for the new encoder to encode
      Returns:
      a new MapEncoder
      See Also:
    • error

      static <A> Encoder<A> error(String error)
      Produces an encoder which always errors with the given message.
      Type Parameters:
      A - the type of object for the new encoder to encode
      Parameters:
      error - the error message
      Returns:
      a new encoder
      See Also: