A
- The type this Decoder
deserializes.public interface Decoder<A>
A Decoder
is used to transform objects from some serialized representation to a specific type using
a supplied DynamicOps
. Decoders encapsulate the deserialization routine for a specific type, regardless
of the input serialization form.
Implementations of Decoder
are immutable once created. The methods defined in this interface never
mutate the decoder in a way visible to the outside.
Encoder
,
Codec
Modifier and Type | Interface and Description |
---|---|
static interface |
Decoder.Boxed<A>
A simple decoder interface that decodes an object from a
Dynamic . |
static interface |
Decoder.Simple<A>
A simple decoder interface that completely decodes an object from a
Dynamic , discarding any remaining
serialized data. |
static interface |
Decoder.Terminal<A>
A simple decoder interface that discards any serialized input that was not used to decode the object.
|
Modifier and Type | Method and Description |
---|---|
default Decoder.Boxed<A> |
boxed()
Creates a
Decoder.Boxed from this Decoder . |
default <T> DataResult<(A,T)> |
decode(Dynamic<T> input)
Decodes an object from the specified
Dynamic data. |
<T> DataResult<(A,T)> |
decode(DynamicOps<T> ops,
T input)
Decodes an object from the specified serialized data.
|
static <A> Decoder<A> |
error(String error)
An
Decoder that performs no deserialization. |
default MapDecoder<A> |
fieldOf(String name)
Returns a
MapDecoder that decodes objects in a record under a field with the given name. |
default <B> Decoder<B> |
flatMap((? super A) -> ? extends DataResult<? extends B> function)
Transforms this decoder to operate on a different type using the given
DataResult -producing mapping function. |
default <B> Decoder<B> |
map((? super A) -> ? extends B function)
Transforms this decoder to operate on a different type using the given mapping function.
|
static <A> Decoder<A> |
ofBoxed(Decoder.Boxed<? extends A> boxed)
Creates a
Decoder from the given Decoder.Boxed . |
static <A> Decoder<A> |
ofSimple(Decoder.Simple<? extends A> simple)
Creates a
Decoder from the given Decoder.Simple . |
static <A> Decoder<A> |
ofTerminal(Decoder.Terminal<? extends A> terminal)
Creates a
Decoder from the given Decoder.Terminal . |
default <T> DataResult<A> |
parse(Dynamic<T> input)
Decodes an object from the specified
Dynamic data, discarding any remaining data. |
default <T> DataResult<A> |
parse(DynamicOps<T> ops,
T input)
Decodes an object from the specified serialized data, discarding any remaining data.
|
default Decoder<A> |
promotePartial((String) -> void onError)
Returns a
Decoder that returns the partial decoded result in this decoder returns an error. |
default Decoder.Simple<A> |
simple()
Creates a
Decoder.Simple from this Decoder . |
default Decoder.Terminal<A> |
terminal()
Creates a
Decoder.Terminal from this Decoder . |
static <A> MapDecoder<A> |
unit(A instance)
A
MapDecoder that performs no deserialization and always returns the given value. |
static <A> MapDecoder<A> |
unit(by-name A instance)
A
MapDecoder that performs no deserialization and always returns a supplied value. |
default Decoder<A> |
withLifecycle(Lifecycle lifecycle)
Sets the
Lifecycle for the serialized data this decoder produces. |
<T> DataResult<(A,T)> decode(DynamicOps<T> ops, T input)
DataResult
.T
- The type of the serialized form.ops
- The DynamicOps
instance defining the serialized form.input
- The serialized data.Pair
containing the decoded object and the remaining serialized data, wrapped in a DataResult
.default <T> DataResult<A> parse(DynamicOps<T> ops, T input)
DataResult
.
For preserving remaining serialize data, use decode(DynamicOps, Object)
.
decode(ops, input).map(Pair::getFirst)
.T
- The type of the serialized form.ops
- The DynamicOps
instance defining the serialized form.input
- The serialized data.DataResult
containing the decoded object.default <T> DataResult<(A,T)> decode(Dynamic<T> input)
Dynamic
data. If decoding fails, returns an error DataResult
.decode(DynamicOps, Object)
on the
wrapped ops and value.T
- The type of the serialized form.input
- The serialized data.DataResult
containing a pair or the decoded object and any remaining serialized data.decode(DynamicOps, Object)
default <T> DataResult<A> parse(Dynamic<T> input)
Dynamic
data, discarding any remaining data. If decoding fails,
returns an error DataResult
.decode(input).map(Pair::getFirst)
.T
- The type of the serialized form.input
- The serialized data.DataResult
containing the decoded object.default Decoder.Terminal<A> terminal()
Decoder.Terminal
from this Decoder
.Decoder.Terminal
based on parse(DynamicOps, Object)
.default Decoder.Boxed<A> boxed()
Decoder.Boxed
from this Decoder
.Decoder.Boxed
based on decode(Dynamic)
.default Decoder.Simple<A> simple()
Decoder.Simple
from this Decoder
.Decoder.Simple
based on parse(Dynamic)
.default MapDecoder<A> fieldOf(String name)
MapDecoder
that decodes objects in a record under a field with the given name. The returned
MapDecoder
may be used in conjunction with a partially deserialized MapLike
to deserialize
many different fields, all using different decoders, from a single record.FieldDecoder
wrapping this decoder.name
- The field to decode objects from.MapDecoder
that performs the same decoding as this decoder, but takes the serialized value
from a record under the given field.MapLike
default <B> Decoder<B> flatMap((? super A) -> ? extends DataResult<? extends B> function)
DataResult
-producing mapping function.Decoder
.Decoder
that wraps this decoder.B
- The new type of the decoder.function
- A function transforming the old type to the new type. Errors from this decoder and from the function
are merged.B
.map(Function)
default <B> Decoder<B> map((? super A) -> ? extends B function)
Decoder
.Decoder
that wraps this decoder.B
- The new type of the decoder.function
- A function transforming the old type to the new type.B
.default Decoder<A> promotePartial((String) -> void onError)
Decoder
that returns the partial decoded result in this decoder returns an error.Decoder
that wraps this decoder.onError
- A callback to run if an error occurs when decoding. The function receives the error message
as its argument.Decoder
that promotes any partial result to a successful decoded result.DataResult.promotePartial(Consumer)
default Decoder<A> withLifecycle(Lifecycle lifecycle)
Lifecycle
for the serialized data this decoder produces.static <A> Decoder<A> ofTerminal(Decoder.Terminal<? extends A> terminal)
Decoder
from the given Decoder.Terminal
.A
- The type to decode into.terminal
- The terminal decoder.Decoder
from the given terminal decoder.Decoder.Terminal.decoder()
static <A> Decoder<A> ofBoxed(Decoder.Boxed<? extends A> boxed)
Decoder
from the given Decoder.Boxed
.A
- The type to decode into.boxed
- The boxed decoder.Decoder
from the given boxed decoder.Decoder.Boxed.decoder()
static <A> Decoder<A> ofSimple(Decoder.Simple<? extends A> simple)
Decoder
from the given Decoder.Simple
.A
- The type to decode into.simple
- The simple decoder.Decoder
from the given simple decoder.Decoder.Simple.decoder()
static <A> MapDecoder<A> unit(A instance)
MapDecoder
that performs no deserialization and always returns the given value. Its
MapDecoder.decode(DynamicOps, MapLike) decoding} method always returns the instance and its
key stream is always empty.A
- The type of the instance.instance
- The instance to return from the decoder.MapDecoder
that always returns the given value.unit(Supplier)
static <A> MapDecoder<A> unit(by-name A instance)
MapDecoder
that performs no deserialization and always returns a supplied value. Its
MapDecoder.decode(DynamicOps, MapLike) decoding} method always returns a value generated from
the supplier and its key stream is always empty.A
- The type of values to decode.instance
- A Supplier
that returns instances to return from the decoder.MapDecoder
that always returns supplied values.