Interface Decoder<A>

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

public interface Decoder<A>
Decodes objects from a serialized form.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Like a Decoder, but deserializes from a Dynamic, which combines the serialized form and the DynamicOps for that form.
    static interface 
    Like a Decoder.Terminal, but deserializes from a Dynamic, which combines the serialized form and the DynamicOps for that form.
    static interface 
    Can decode an object from a serialized form, but does not return any remaining serialized data.
  • Method Summary

    Modifier and Type
    Method
    Description
    default Decoder.Boxed<A>
    Returns a boxed decoder that has the same behaviour as this decoder.
    default <T> DataResult<Pair<A,T>>
    decode(Dynamic<T> input)
    Decode an object from a serialized form, returning any remaining serialized data.
    <T> DataResult<Pair<A,T>>
    decode(DynamicOps<T> ops, T input)
    Decodes an object from a serialized form, returning any remaining serialized data.
    static <A> Decoder<A>
    error(String error)
    Returns a decoder which always errors.
    default MapDecoder<A>
    Returns a map decoder that decodes a single field with the given name using this decoder.
    default <B> Decoder<B>
    flatMap(Function<? super A,? extends DataResult<? extends B>> function)
    Transforms the type of this decoder using the given function, where the transformation may fail.
    default <B> Decoder<B>
    map(Function<? super A,? extends B> function)
    Transforms the type of this decoder using the given function.
    static <A> Decoder<A>
    ofBoxed(Decoder.Boxed<? extends A> boxed)
    Returns a decoder which decodes from a combined serialized form and format.
    static <A> Decoder<A>
    ofSimple(Decoder.Simple<? extends A> simple)
    Returns a decoder which decodes from a combined serialized form and format,.
    static <A> Decoder<A>
    ofTerminal(Decoder.Terminal<? extends A> terminal)
    Returns a decoder which throws out any remaining serialized data.
    default <T> DataResult<A>
    parse(Dynamic<T> input)
    Parse a serialized form as an instance of this decoder's type.
    default <T> DataResult<A>
    parse(DynamicOps<T> ops, T input)
     
    default Decoder<A>
    Returns a new decoder that decodes the same object as this decoder, but also runs the given action on an error.
    default Decoder.Simple<A>
    Returns a simple decoder that has the same behaviour as this decoder, but discards any remaining serialized data.
    Returns a terminal decoder that uses this decoder to decode an object from a serialized form, but does not return any remaining serialized data.
    static <A> MapDecoder<A>
    unit(A instance)
    Returns a decoder which always decodes the given object.
    static <A> MapDecoder<A>
    unit(Supplier<A> instance)
    Returns a decoder which always decodes an object taken from the given supplier.
    default Decoder<A>
    Returns a decoder which decodes the same object as this decoder, but attaches the given lifecycle to the result.
  • Method Details

    • decode

      <T> DataResult<Pair<A,T>> decode(DynamicOps<T> ops, T input)
      Decodes an object from a serialized form, returning any remaining serialized data.
      Type Parameters:
      T - the type of the serialized form
      Parameters:
      ops - a DynamicOps for the serialized form
      input - the serialized form to decode
      Returns:
      a DataResult containing the decoded object and any remaining serialized data, or an error if the object could not be decoded
    • parse

      default <T> DataResult<A> parse(DynamicOps<T> ops, T input)
    • decode

      default <T> DataResult<Pair<A,T>> decode(Dynamic<T> input)
      Decode an object from a serialized form, returning any remaining serialized data.
      Type Parameters:
      T - the type of the serialized form
      Parameters:
      input - the Dynamic, containing serialized data, to decode
      Returns:
      a DataResult containing the decoded object and any remaining serialized data, or an error if the object could not be decoded
    • parse

      default <T> DataResult<A> parse(Dynamic<T> input)
      Parse a serialized form as an instance of this decoder's type.
      Type Parameters:
      T - the type of the serialized form
      Parameters:
      input - the Dynamic, containing serialized data, to parse
      Returns:
      a DataResult containing the parsed object, or an error if the object could not be parsed
    • terminal

      default Decoder.Terminal<A> terminal()
      Returns a terminal decoder that uses this decoder to decode an object from a serialized form, but does not return any remaining serialized data.
      Returns:
      a terminal decoder that uses this decoder to decode an object from a serialized form, but does not return any remaining serialized data
      See Also:
    • boxed

      default Decoder.Boxed<A> boxed()
      Returns a boxed decoder that has the same behaviour as this decoder.
      Returns:
      a boxed decoder that has the same behaviour as this decoder
      See Also:
    • simple

      default Decoder.Simple<A> simple()
      Returns a simple decoder that has the same behaviour as this decoder, but discards any remaining serialized data.
      Returns:
      a simple decoder that has the same behaviour as this decoder, but discards any remaining serialized data
      See Also:
    • fieldOf

      default MapDecoder<A> fieldOf(String name)
      Returns a map decoder that decodes a single field with the given name using this decoder.
      Parameters:
      name - the name of the field to decode
      Returns:
      a map decoder that decodes a single field with the given name using this decoder
      See Also:
    • flatMap

      default <B> Decoder<B> flatMap(Function<? super A,? extends DataResult<? extends B>> function)
      Transforms the type of this decoder using the given function, where the transformation may fail.
      Type Parameters:
      B - the type of object for the new decoder to decode
      Parameters:
      function - a function to apply to the decoded object
      Returns:
      a new decoder
    • map

      default <B> Decoder<B> map(Function<? super A,? extends B> function)
      Transforms the type of this decoder using the given function.
      Type Parameters:
      B - the type of object for the new decoder to decode
      Parameters:
      function - a function to apply to the decoded object
      Returns:
      a new decoder
    • promotePartial

      default Decoder<A> promotePartial(Consumer<String> onError)
      Returns a new decoder that decodes the same object as this decoder, but also runs the given action on an error.
      Parameters:
      onError - an action to run on an error
      Returns:
      a new decoder that decodes the same object as this decoder, but also runs the given action on an error
    • withLifecycle

      default Decoder<A> withLifecycle(Lifecycle lifecycle)
      Returns a decoder which decodes the same object as this decoder, but attaches the given lifecycle to the result.
      Parameters:
      lifecycle - the Lifecycle of the decoded object
      Returns:
      a decoder which decodes the same object as this decoder, but attaches the given lifecycle to the result
    • ofTerminal

      static <A> Decoder<A> ofTerminal(Decoder.Terminal<? extends A> terminal)
      Returns a decoder which throws out any remaining serialized data.
      Type Parameters:
      A - the type of object to decode
      Parameters:
      terminal - a Decoder.Terminal with the desired decoding logic
      Returns:
      a decoder which throws out any remaining serialized data
    • ofBoxed

      static <A> Decoder<A> ofBoxed(Decoder.Boxed<? extends A> boxed)
      Returns a decoder which decodes from a combined serialized form and format.
      Type Parameters:
      A - the type of object to decode
      Parameters:
      boxed - a Decoder.Boxed with the desired decoding logic
      Returns:
      a decoder which decodes from a combined serialized form and format
    • ofSimple

      static <A> Decoder<A> ofSimple(Decoder.Simple<? extends A> simple)
      Returns a decoder which decodes from a combined serialized form and format,.
      Type Parameters:
      A - the type of object to decode
      Parameters:
      simple - a Decoder.Simple with the desired decoding logic
      Returns:
      a decoder which decodes from a combined serialized form and format,
    • unit

      static <A> MapDecoder<A> unit(A instance)
      Returns a decoder which always decodes the given object.
      Type Parameters:
      A - the type of object to decode
      Parameters:
      instance - the object to decode
      Returns:
      a decoder which always decodes the given object
    • unit

      static <A> MapDecoder<A> unit(Supplier<A> instance)
      Returns a decoder which always decodes an object taken from the given supplier.
      Type Parameters:
      A - the type of object to decode
      Parameters:
      instance - provides the object to decode
      Returns:
      a decoder which always decodes an object taken from the given supplier
    • error

      static <A> Decoder<A> error(String error)
      Returns a decoder which always errors.
      Type Parameters:
      A - the type of object to (fail to) decode
      Parameters:
      error - the error message to provide
      Returns:
      a decoder which always errors