Interface Encoder<C>

Type Parameters:
C - The type the encoder is for
All Known Subinterfaces:
Codec<C>, KeyableCodec<C>, KeyableEncoder<C>
All Known Implementing Classes:
EitherCodec, ListCodec, MapCodec, NamedCodec, OptionalCodec, RangeCodec, UnitCodec
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Encoder<C>
Represents an encoder for a specific type.
The implementation encodes a value of the type specified by the type provider
and returns a result containing the encoded value or an error message.
  • Method Summary

    Modifier and Type
    Method
    Description
    default <R> R
    encode(@NotNull TypeProvider<R> provider, C value)
    Encodes the value of the specified type and returns the encoded value directly.
    default <R> R
    encode(@NotNull TypeProvider<R> provider, R current, C value)
    Encodes the value of the specified type and returns the encoded value directly.
    <R> @NotNull Result<R>
    encodeStart(@NotNull TypeProvider<R> provider, R current, C value)
    Encodes the value of the specified type and returns the result.
    The result contains the encoded value or an error message.
    default <O> @NotNull Encoder<O>
    mapEncoder(@NotNull ResultingFunction<O,C> to)
    Maps the type of the encoded value to another type.
    The mapping function is applied to the raw input value of encodeStart(TypeProvider, Object, Object).
    The mapping function returns a result containing the mapped value or an error message if the mapping process fails.
    The mapping function is not allowed to throw any exceptions.
  • Method Details

    • encode

      @NotNull default <R> R encode(@NotNull @NotNull TypeProvider<R> provider, @Nullable C value)
      Encodes the value of the specified type and returns the encoded value directly.
      Type Parameters:
      R - The type to encode to
      Parameters:
      provider - The type provider
      value - The value to encode
      Returns:
      The encoded value
      Throws:
      NullPointerException - If the type provider is null
      EncoderException - If an error occurs during encoding
    • encode

      @NotNull default <R> R encode(@NotNull @NotNull TypeProvider<R> provider, @NotNull R current, @Nullable C value)
      Encodes the value of the specified type and returns the encoded value directly.

      The current value is the value that is currently encoded.
      In the most cases this value should be equal to TypeProvider.empty().
      In the case of encoding a value that is part of a bigger structure, the current value should be the structure.

      Type Parameters:
      R - The type to encode to
      Parameters:
      provider - The type provider
      current - The current value
      value - The value to encode
      Returns:
      The encoded value
      Throws:
      NullPointerException - If the type provider is null
      EncoderException - If an error occurs during encoding
      See Also:
    • encodeStart

      @NotNull <R> @NotNull Result<R> encodeStart(@NotNull @NotNull TypeProvider<R> provider, @NotNull R current, @Nullable C value)
      Encodes the value of the specified type and returns the result.
      The result contains the encoded value or an error message.

      The current value is the value that is currently encoded.
      In the most cases this value should be equal to TypeProvider.empty().
      In the case of encoding a value that is part of a bigger structure, the current value should be the structure.

      Type Parameters:
      R - The type to encode to
      Parameters:
      provider - The type provider
      current - The current value
      value - The value to encode
      Returns:
      The result
      Throws:
      NullPointerException - If the type provider is null
    • mapEncoder

      @NotNull default <O> @NotNull Encoder<O> mapEncoder(@NotNull @NotNull ResultingFunction<O,C> to)
      Maps the type of the encoded value to another type.
      The mapping function is applied to the raw input value of encodeStart(TypeProvider, Object, Object).
      The mapping function returns a result containing the mapped value or an error message if the mapping process fails.
      The mapping function is not allowed to throw any exceptions.
      Type Parameters:
      O - The type to map to
      Parameters:
      to - The mapping function
      Returns:
      The mapped encoder
      Throws:
      NullPointerException - If the mapping function is null