A
- The type this Codec
serializes and deserializes.public interface Codec<A> extends Encoder<A>, Decoder<A>
Decoder
and Encoder
.
A Codec
handles transformation of a specific type to and from a provided serialized form. Codecs
encapsulate both the behaviors of an encoder and those of a decoder in the same object.
Implementations of Codec
are immutable once created. The methods defined in this interface never
mutate the codec in a way visible to the outside.
Encoder.encode(Object, DynamicOps, Object)
and Decoder.decode(DynamicOps, Object)
methods.MapCodec
Modifier and Type | Interface and Description |
---|---|
static interface |
Codec.ResultFunction<A>
A unary operator applied on the result obtained from decoding or encoding from a
Codec . |
Decoder.Boxed<A>, Decoder.Simple<A>, Decoder.Terminal<A>
Modifier and Type | Field and Description |
---|---|
static PrimitiveCodec<Boolean> |
BOOL
A
PrimitiveCodec for the type boolean . |
static PrimitiveCodec<Byte> |
BYTE
A
PrimitiveCodec for the type byte . |
static PrimitiveCodec<ByteBuffer> |
BYTE_BUFFER
A
PrimitiveCodec for the type ByteBuffer (a byte sequence). |
static PrimitiveCodec<Double> |
DOUBLE
A
PrimitiveCodec for the type double . |
static MapCodec<Unit> |
EMPTY
A
MapCodec that performs no encoding nor decoding. |
static PrimitiveCodec<Float> |
FLOAT
A
PrimitiveCodec for the type float . |
static PrimitiveCodec<Integer> |
INT
A
PrimitiveCodec for the type int . |
static PrimitiveCodec<IntStream> |
INT_STREAM
A
PrimitiveCodec for the type IntStream (an int sequence). |
static PrimitiveCodec<Long> |
LONG
A
PrimitiveCodec for the type long . |
static PrimitiveCodec<LongStream> |
LONG_STREAM
A
PrimitiveCodec for the type LongStream (a long sequence). |
static Codec<Dynamic<?>> |
PASSTHROUGH
A codec that "passes through" serialized forms unchanged.
|
static PrimitiveCodec<Short> |
SHORT
A
PrimitiveCodec for the type short . |
static PrimitiveCodec<String> |
STRING
A
PrimitiveCodec for the type String . |
Modifier and Type | Method and Description |
---|---|
static <N extends Number & Comparable<N>> |
checkRange(N minInclusive,
N maxInclusive) |
default <S> Codec<S> |
comapFlatMap((? super A) -> ? extends DataResult<? extends S> to,
(? super S) -> ? extends A from)
Transforms this codec into another codec using the given invertible partial function.
|
static <K,V> Codec<List<(K,V)>> |
compoundList(Codec<K> keyCodec,
Codec<V> elementCodec)
Creates a codec for a list of key-value pairs from codecs for the element types.
|
default Codec<A> |
deprecated(int since)
Returns an equivalent
Codec with a deprecated lifecycle. |
default <E> Codec<E> |
dispatch((? super E) -> ? extends A type,
(? super A) -> ? extends Codec<? extends E> codec)
Returns a
Codec that uses a default type key extracted using this codec to polymorphically dispatch
against a complete set of supported subtypes. |
default <E> Codec<E> |
dispatch(String typeKey,
(? super E) -> ? extends A type,
(? super A) -> ? extends Codec<? extends E> codec)
Returns a
Codec that uses a type key extracted using this codec to polymorphically dispatch against a
complete set of supported subtypes. |
default <E> MapCodec<E> |
dispatchMap((? super E) -> ? extends A type,
(? super A) -> ? extends Codec<? extends E> codec)
Returns a
MapCodec that uses a default type key extracted using this codec to polymorphically dispatch
against a complete set of supported subtypes. |
default <E> MapCodec<E> |
dispatchMap(String typeKey,
(? super E) -> ? extends A type,
(? super A) -> ? extends Codec<? extends E> codec)
Returns a
MapCodec that uses a type key extracted using this codec to polymorphically dispatch against a
complete set of supported subtypes. |
default <E> Codec<E> |
dispatchStable((? super E) -> ? extends A type,
(? super A) -> ? extends Codec<? extends E> codec)
Returns a
Codec that uses a default type key extracted using this codec to polymorphically dispatch
against a complete set of supported subtypes. |
static Codec<Double> |
doubleRange(double minInclusive,
double maxInclusive) |
static <F,S> Codec<(F|S)> |
either(Codec<F> first,
Codec<S> second)
Creates a codec for an
Either from codecs for its elements. |
default MapCodec<A> |
fieldOf(String name)
Returns a
MapCodec that encodes and decodes objects in a record under a field with the given name. |
default <S> Codec<S> |
flatComapMap((? super A) -> ? extends S to,
(? super S) -> ? extends DataResult<? extends A> from)
Transforms this codec into another codec using the given partially invertible function.
|
default <S> Codec<S> |
flatXmap((? super A) -> ? extends DataResult<? extends S> to,
(? super S) -> ? extends DataResult<? extends A> from)
Transforms this codec into another codec using the given partially invertible partial function.
|
static Codec<Float> |
floatRange(float minInclusive,
float maxInclusive) |
static Codec<Integer> |
intRange(int minInclusive,
int maxInclusive) |
static <E> Codec<List<E>> |
list(Codec<E> elementCodec)
Creates a codec for a
List from a codec for the element type. |
default Codec<List<A>> |
listOf()
Returns a codec for a
List of elements defined by this codec. |
static <F,S> MapCodec<(F|S)> |
mapEither(MapCodec<F> first,
MapCodec<S> second)
Creates a map codec for an
Either from map codecs for its elements. |
static <F,S> MapCodec<(F,S)> |
mapPair(MapCodec<F> first,
MapCodec<S> second)
Creates a map codec for a
Pair from map codecs for its elements. |
default Codec<A> |
mapResult(Codec.ResultFunction<A> function)
Transforms the
DataResult produced by this code's decode and encode methods. |
static <A> Codec<A> |
of(Encoder<A> encoder,
Decoder<A> decoder)
|
static <A> Codec<A> |
of(Encoder<A> encoder,
Decoder<A> decoder,
String name)
|
static <A> MapCodec<A> |
of(MapEncoder<A> encoder,
MapDecoder<A> decoder)
|
static <A> MapCodec<A> |
of(MapEncoder<A> encoder,
MapDecoder<A> decoder,
by-name String name)
|
static <F> MapCodec<Optional<F>> |
optionalField(String name,
Codec<F> elementCodec)
Creates a
MapCodec that encodes and decodes an optional record field. |
default MapCodec<Optional<A>> |
optionalFieldOf(String name)
Returns a
MapCodec that encodes and decodes objects the may optionally appear in a record. |
default MapCodec<A> |
optionalFieldOf(String name,
A defaultValue)
Returns a
MapCodec that encodes and decodes objects that may optionally appear in a record. |
default MapCodec<A> |
optionalFieldOf(String name,
A defaultValue,
Lifecycle lifecycleOfDefault)
Returns a
MapCodec that encodes and decodes objects that may optionally appear in a record, and
additionally allows the caller to provide a lifecycle for the default value. |
default MapCodec<A> |
optionalFieldOf(String name,
Lifecycle fieldLifecycle,
A defaultValue,
Lifecycle lifecycleOfDefault)
Returns a
MapCodec that encodes and decodes objects that may optionally appear in a record, and
additionally allows the caller to provide lifecycles for the present field values and the default value. |
default Codec<A> |
orElse(A value)
Provides a default value for decoding in case of error.
|
default Codec<A> |
orElse((String) -> void onError,
A value)
Provides a default value for decoding in case of error, and runs the given callback on error.
|
default Codec<A> |
orElse((String) -> String onError,
A value)
Provides a default value for decoding in case of error, and transforms the error.
|
default Codec<A> |
orElseGet((String) -> void onError,
by-name ? extends A value)
Provides a default value for decoding in case of error, and runs the given callback on error.
|
default Codec<A> |
orElseGet(by-name ? extends A value)
Provides a default value for decoding in case of error.
|
default Codec<A> |
orElseGet((String) -> String onError,
by-name ? extends A value)
Provides a default value for decoding in case of error, and transforms the error.
|
static <F,S> Codec<(F,S)> |
pair(Codec<F> first,
Codec<S> second)
Creates a codec for a
Pair from codecs for its elements. |
default <E> Codec<E> |
partialDispatch(String typeKey,
(? super E) -> ? extends DataResult<? extends A> type,
(? super A) -> ? extends DataResult<? extends Codec<? extends E>> codec)
Returns a
Codec that uses a type key extracted using this codec to polymorphically dispatch against a
partial set of supported subtypes. |
default Codec<A> |
promotePartial((String) -> void onError)
Returns a
Decoder that returns the partial decoded result in this decoder returns an error. |
static <K,V> SimpleMapCodec<K,V> |
simpleMap(Codec<K> keyCodec,
Codec<V> elementCodec,
Keyable keys)
Creates a map codec for a
Map with a fixed key set. |
default Codec<A> |
stable()
|
static <K,V> UnboundedMapCodec<K,V> |
unboundedMap(Codec<K> keyCodec,
Codec<V> elementCodec)
Creates a map codec for a
Map with arbitrary keys. |
static <A> Codec<A> |
unit(A defaultValue)
Returns a codec that always decodes the given value and always encodes an empty value.
|
static <A> Codec<A> |
unit(by-name A defaultValue)
Returns a codec that always decodes a supplied value and always encodes an empty value.
|
default Codec<A> |
withLifecycle(Lifecycle lifecycle)
Sets the
Lifecycle for any result data this codec produces. |
default <S> Codec<S> |
xmap((? super A) -> ? extends S to,
(? super S) -> ? extends A from)
Transforms this codec into another codec using the given invertible mapping functions.
|
static final PrimitiveCodec<Boolean> BOOL
PrimitiveCodec
for the type boolean
.static final PrimitiveCodec<Byte> BYTE
PrimitiveCodec
for the type byte
.static final PrimitiveCodec<Short> SHORT
PrimitiveCodec
for the type short
.static final PrimitiveCodec<Integer> INT
PrimitiveCodec
for the type int
.static final PrimitiveCodec<Long> LONG
PrimitiveCodec
for the type long
.static final PrimitiveCodec<Float> FLOAT
PrimitiveCodec
for the type float
.static final PrimitiveCodec<Double> DOUBLE
PrimitiveCodec
for the type double
.static final PrimitiveCodec<String> STRING
PrimitiveCodec
for the type String
.static final PrimitiveCodec<ByteBuffer> BYTE_BUFFER
PrimitiveCodec
for the type ByteBuffer
(a byte sequence).static final PrimitiveCodec<IntStream> INT_STREAM
PrimitiveCodec
for the type IntStream
(an int sequence).static final PrimitiveCodec<LongStream> LONG_STREAM
PrimitiveCodec
for the type LongStream
(a long sequence).static final Codec<Dynamic<?>> PASSTHROUGH
default Codec<A> withLifecycle(Lifecycle lifecycle)
Lifecycle
for any result data this codec produces.withLifecycle
in interface Decoder<A>
withLifecycle
in interface Encoder<A>
Codec
that wraps this codec.lifecycle
- the lifecycle to use.Lifecycle
default Codec<A> stable()
Lifecycle.stable()
default Codec<A> deprecated(int since)
Codec
with a deprecated lifecycle.since
- The deprecation version.Lifecycle.deprecated(int)
static <A> Codec<A> of(Encoder<A> encoder, Decoder<A> decoder, String name)
A
- The type the returned codec operates on.encoder
- The Encoder
defining the returned codec.decoder
- The Decoder
defining the returned codec.name
- The name given in the string representation of the returned codec.of(Encoder, Decoder)
static <A> MapCodec<A> of(MapEncoder<A> encoder, MapDecoder<A> decoder)
A
- The type the returned codec operates on.encoder
- The MapEncoder
defining the returned MapCodec
.decoder
- The MapDecoder
defining the returned MapCodec
.MapCodec
combining the given encoder and decoder.static <A> MapCodec<A> of(MapEncoder<A> encoder, MapDecoder<A> decoder, by-name String name)
MapEncoder
and a MapDecoder
into a named MapCodec
. The key stream
of the returned codec is the concatenation of the key stream of the encoder and decoder.A
- The type the returned codec operates on.encoder
- The MapEncoder
defining the returned MapCodec
.decoder
- The MapDecoder
defining the returned MapCodec
.name
- The name given in the string representation of the returned MapCodec
.MapCodec
combining the given encoder and decoder.of(MapEncoder, MapDecoder)
static <F,S> Codec<(F,S)> pair(Codec<F> first, Codec<S> second)
Pair
from codecs for its elements.F
- The first element type.S
- The second element type.first
- A codec for the first element type.second
- A codec for the second element type.Pair
of the element types.static <F,S> Codec<(F|S)> either(Codec<F> first, Codec<S> second)
Either
from codecs for its elements.F
- The first element type.S
- The second element type.first
- A codec for the first element type.second
- A codec for the second element type.Either
of the element types.static <F,S> MapCodec<(F,S)> mapPair(MapCodec<F> first, MapCodec<S> second)
Pair
from map codecs for its elements.F
- The first element type.S
- The second element type.first
- A codec for the first element type.second
- A codec for the second element type.Pair
of the element types.static <F,S> MapCodec<(F|S)> mapEither(MapCodec<F> first, MapCodec<S> second)
Either
from map codecs for its elements.F
- The first element type.S
- The second element type.first
- A codec for the first element type.second
- A codec for the second element type.Either
of the element types.static <E> Codec<List<E>> list(Codec<E> elementCodec)
List
from a codec for the element type.static <K,V> Codec<List<(K,V)>> compoundList(Codec<K> keyCodec, Codec<V> elementCodec)
pair(Codec, Codec)
and
list(Codec)
in sequence.K
- The key type.V
- The element type.keyCodec
- A codec for the key type.elementCodec
- A codec for the element type.pair(Codec, Codec)
,
list(Codec)
static <K,V> SimpleMapCodec<K,V> simpleMap(Codec<K> keyCodec, Codec<V> elementCodec, Keyable keys)
Map
with a fixed key set.
For creating a map codec with an unbounded key set, see unboundedMap(Codec, Codec)
.
static <K,V> UnboundedMapCodec<K,V> unboundedMap(Codec<K> keyCodec, Codec<V> elementCodec)
Map
with arbitrary keys.
For creating a map codec with a fixed key set, see simpleMap(Codec, Codec, Keyable)
.
K
- The key type.V
- The element type.keyCodec
- A codec for the key type.elementCodec
- A codec for the element type.MapCodec
for a map with arbitrary keys.static <F> MapCodec<Optional<F>> optionalField(String name, Codec<F> elementCodec)
MapCodec
that encodes and decodes an optional record field. Absent Optional
values
will not be serialized and visa-versa.F
- The element type.name
- The name of the field.elementCodec
- A codec for the element type.name
.optionalFieldOf(String)
,
fieldOf(String)
default Codec<List<A>> listOf()
List
of elements defined by this codec.list(this)
.default <S> Codec<S> xmap((? super A) -> ? extends S to, (? super S) -> ? extends A from)
This method performs a map
operation on both Decoder
and Encoder
.
Encoder.comap(Function)
and Decoder.map(Function)
on this codec.S
- The new type.to
- A function from this codec's type to the new type.from
- A function from the new type to this codec's type.Decoder.map(Function)
,
Encoder.comap(Function)
default <S> Codec<S> comapFlatMap((? super A) -> ? extends DataResult<? extends S> to, (? super S) -> ? extends A from)
This method performs a flatMap
operation on Decoder
and a map
operation on Encoder
.
Encoder.comap(Function)
and Decoder.flatMap(Function)
.S
- The new type.to
- A partial function form this codec's type to the new type. The value and any errors are wrapped in
a DataResult
.from
- A function from the new type to this codec's type.Decoder.flatMap(Function)
,
Encoder.comap(Function)
default <S> Codec<S> flatComapMap((? super A) -> ? extends S to, (? super S) -> ? extends DataResult<? extends A> from)
This method performs a map
operation on Decoder
and a flatMap
operation on Encoder
.
Encoder.flatComap(Function)
and Decoder.map(Function)
.S
- The new type.to
- A function form this codec's type to the new type.from
- A partial function from the new type to this codec's type. The value and any errors are wrapped in
a DataResult
.Decoder.map(Function)
,
Encoder.flatComap(Function)
default <S> Codec<S> flatXmap((? super A) -> ? extends DataResult<? extends S> to, (? super S) -> ? extends DataResult<? extends A> from)
This method performs a flatMap
operation on both Decoder
and Encoder
.
Encoder.flatComap(Function)
and Decoder.flatMap(Function)
.S
- The new type.to
- A partial function form this codec's type to the new type.The value and any errors are wrapped in
a DataResult
.from
- A partial function from the new type to this codec's type. The value and any errors are wrapped in
a DataResult
.Decoder.flatMap(Function)
,
Encoder.flatComap(Function)
default MapCodec<A> fieldOf(String name)
MapCodec
that encodes and decodes objects in a record under a field with the given name.
The returned MapCodec
may be used to serialize and deserialize many different fields, all using
different codecs, from a single record.fieldOf
in interface Decoder<A>
fieldOf
in interface Encoder<A>
Encoder.fieldOf(String)
and Decoder.fieldOf(String)
and combines them into a MapCodec
.name
- The field to encode and decode objects from.MapCodec
that performs the same operations as this codec, but takes the serialized value
from a record under the given field.RecordCodecBuilder
,
MapLike
,
MapCodec.of(MapEncoder, MapDecoder)
default MapCodec<Optional<A>> optionalFieldOf(String name)
MapCodec
that encodes and decodes objects the may optionally appear in a record. Absent
Optional
values will not be serialized to the record, and visa-versa.optionalField(name, this)
.name
- The field to encode and decode objects from.MapCodec
that performs the same operations as this codec, but taking the optional value
from a record field.optionalField(String, Codec)
,
fieldOf(String)
,
optionalFieldOf(String, Object)
default MapCodec<A> optionalFieldOf(String name, A defaultValue)
MapCodec
that encodes and decodes objects that may optionally appear in a record. Absent
values will be decoded as the default value, and the default value will be encoded as absent.optionalField(String, Codec)
, then maps absent optionals
to the default value.name
- The field to encode and decode objects from.defaultValue
- The default value to use if the value is absent in the serialized form.MapCodec
that performs operations on an optional record field.default MapCodec<A> optionalFieldOf(String name, A defaultValue, Lifecycle lifecycleOfDefault)
MapCodec
that encodes and decodes objects that may optionally appear in a record, and
additionally allows the caller to provide a lifecycle for the default value. Absent values will be decoded
as the default value, and the default value will be encoded as absent.optionalFieldOf(String, Lifecycle, Object, Lifecycle)
with a field lifecycle of Lifecycle.experimental()
.name
- The field to encode and decode objects from.defaultValue
- The default value to use if the value is absent in the serialized form.lifecycleOfDefault
- The lifecycle of the default value.MapCodec
that performs operations on an optional record field.optionalFieldOf(String, Object)
default MapCodec<A> optionalFieldOf(String name, Lifecycle fieldLifecycle, A defaultValue, Lifecycle lifecycleOfDefault)
MapCodec
that encodes and decodes objects that may optionally appear in a record, and
additionally allows the caller to provide lifecycles for the present field values and the default value.
Absent values will be decoded as the default value, and the default value will be encoded as absent.optionalField(String, Codec)
, then maps absent optionals
to the default value.name
- The field to encode and decode objects from.defaultValue
- The default value to use if the value is absent in the serialized form.fieldLifecycle
- The lifecycle of the encoded or decoded fields.lifecycleOfDefault
- The lifecycle of the default value.MapCodec
that performs operations on an optional record field.optionalFieldOf(String, Object)
default Codec<A> mapResult(Codec.ResultFunction<A> function)
DataResult
produced by this code's decode and encode methods.function
- The function to run on the produced DataResult
.Decoder.decode(DynamicOps, Object)
,
Encoder.encode(Object, DynamicOps, Object)
default Codec<A> orElse((String) -> void onError, A value)
orElse(UnaryOperator, Object)
, passing an error
function that returns its input after calling onError
.onError
- The callback to run if decoding or encoding fails.value
- The default value to return if decoding fails.orElse(Object)
default Codec<A> orElse((String) -> String onError, A value)
mapResult(ResultFunction)
with a Codec.ResultFunction
that maps the results and applies the error function.onError
- A function that transforms an error if one is returned.value
- The default value to return if decoding fails.Codec.ResultFunction
default Codec<A> orElseGet((String) -> void onError, by-name ? extends A value)
orElseGet(UnaryOperator, Supplier)
, passing an error
function that returns its input after calling onError
.onError
- The callback to run if decoding or encoding fails.value
- A supplier of default values to return if decoding fails.default Codec<A> orElseGet((String) -> String onError, by-name ? extends A value)
mapResult(ResultFunction)
with a Codec.ResultFunction
that maps the results and applies the error function.onError
- A function that transforms an error if one is returned.value
- A supplier of default values to return if decoding fails.Codec.ResultFunction
default Codec<A> orElse(A value)
mapResult(ResultFunction)
with a Codec.ResultFunction
that supplies the default value if this codec cannot decode.value
- The default value to return if decoding fails.default Codec<A> orElseGet(by-name ? extends A value)
mapResult(ResultFunction)
with a Codec.ResultFunction
that supplies the default value if this codec cannot decode.value
- A supplier of default values to return if decoding fails.default Codec<A> promotePartial((String) -> void onError)
Decoder
Decoder
that returns the partial decoded result in this decoder returns an error.promotePartial
in interface Decoder<A>
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)
static <A> Codec<A> unit(A defaultValue)
unit
in interface Decoder<A>
A
- The type of the value.defaultValue
- The value to decode.Decoder.unit(Supplier)
static <A> Codec<A> unit(by-name A defaultValue)
unit
in interface Decoder<A>
A
- The type of the value.defaultValue
- A supplier of the value to decode.Decoder.unit(Supplier)
default <E> Codec<E> dispatch((? super E) -> ? extends A type, (? super A) -> ? extends Codec<? extends E> codec)
Codec
that uses a default type key extracted using this codec to polymorphically dispatch
against a complete set of supported subtypes. This method can be used to implement encoding and decoding over
algebraic types or sealed class hierarchies which may require different codecs for each subtype.
This method is equivalent to dispatch("type", type, codec)
.
Warning: This method is unsound. Care must be taken that the codecs returned by
codec
actually accept the correct subtype, and not an incidental subtype thereof. Heap pollution
may occur when using this method unless the programmer checks that the correct types are inferred or
specified.
dispatch(String, Function, Function)
with the type key "type"
.E
- The polymorphic object type.type
- A function that extracts the type key from the a polymorphic value.codec
- A function that produces a codec for a subtype corresponding to the provided type key.dispatchStable(Function, Function)
default <E> Codec<E> dispatch(String typeKey, (? super E) -> ? extends A type, (? super A) -> ? extends Codec<? extends E> codec)
Codec
that uses a type key extracted using this codec to polymorphically dispatch against a
complete set of supported subtypes. This method can be used to implement encoding and decoding over
algebraic types or sealed class hierarchies which may require different codecs for each subtype.
Warning: This method is unsound. Care must be taken that the codecs returned by
codec
actually accept the correct subtype, and not an incidental subtype thereof. Heap pollution
may occur when using this method unless the programmer checks that the correct types are inferred or
specified.
partialDispatch(String, Function, Function)
, with
the type and codec functions always returning a success.E
- The polymorphic object type.typeKey
- The field in the record that the serialized type key is stored under.type
- A partial function that extracts the type key from the a polymorphic value.codec
- A function that produces a codec for a subtype corresponding to the provided type key.dispatch(Function, Function)
default <E> Codec<E> dispatchStable((? super E) -> ? extends A type, (? super A) -> ? extends Codec<? extends E> codec)
Codec
that uses a default type key extracted using this codec to polymorphically dispatch
against a complete set of supported subtypes. This method can be used to implement encoding and decoding over
algebraic types or sealed class hierarchies which may require different codecs for each subtype.
This method is equivalent to dispatchStable("type", type, codec)
.
Warning: This method is unsound. Care must be taken that the codecs returned by
codec
actually accept the correct subtype, and not an incidental subtype thereof. Heap pollution
may occur when using this method unless the programmer checks that the correct types are inferred or
specified.
partialDispatch(String, Function, Function)
with the type key "type"
.E
- The polymorphic object type.type
- A function that extracts the type key from the a polymorphic value.codec
- A function that produces a codec for a subtype corresponding to the provided type key.dispatchStable(Function, Function)
default <E> Codec<E> partialDispatch(String typeKey, (? super E) -> ? extends DataResult<? extends A> type, (? super A) -> ? extends DataResult<? extends Codec<? extends E>> codec)
Codec
that uses a type key extracted using this codec to polymorphically dispatch against a
partial set of supported subtypes. This method can be used to implement partial encoding and decoding over
algebraic types or sealed class hierarchies which may require different codecs for each subtype.
Warning: This method is unsound. Care must be taken that the codecs returned by
codec
actually accept the correct subtype, and not an incidental subtype thereof. Heap pollution
may occur when using this method unless the programmer checks that the correct types are inferred or
specified.
KeyDispatchCodec
.E
- The polymorphic object type.typeKey
- The field in the record that the serialized type key is stored under.type
- A partial function that extracts the type key from the a polymorphic value.codec
- A function that produces a codec for a subtype corresponding to the provided type key.dispatch(String, Function, Function)
default <E> MapCodec<E> dispatchMap((? super E) -> ? extends A type, (? super A) -> ? extends Codec<? extends E> codec)
MapCodec
that uses a default type key extracted using this codec to polymorphically dispatch
against a complete set of supported subtypes. This method can be used to implement encoding and decoding over
algebraic types or sealed class hierarchies which may require different codecs for each subtype.
This method is equivalent to dispatchMap("type", type, codec)
.
Warning: This method is unsound. Care must be taken that the codecs returned by
codec
actually accept the correct subtype, and not an incidental subtype thereof. Heap pollution
may occur when using this method unless the programmer checks that the correct types are inferred or
specified.
dispatchMap(String, Function, Function)
with the type key "type"
.E
- The polymorphic object type.type
- A function that extracts the type key from the a polymorphic value.codec
- A function that produces a codec for a subtype corresponding to the provided type key.dispatchStable(Function, Function)
default <E> MapCodec<E> dispatchMap(String typeKey, (? super E) -> ? extends A type, (? super A) -> ? extends Codec<? extends E> codec)
MapCodec
that uses a type key extracted using this codec to polymorphically dispatch against a
complete set of supported subtypes. This method can be used to implement encoding and decoding over
algebraic types or sealed class hierarchies which may require different codecs for each subtype.
Warning: This method is unsound. Care must be taken that the codecs returned by
codec
actually accept the correct subtype, and not an incidental subtype thereof. Heap pollution
may occur when using this method unless the programmer checks that the correct types are inferred or
specified.
KeyDispatchCodec
with the
type and codec functions always returning a success.E
- The polymorphic object type.typeKey
- The field in the record that the serialized type key is stored under.type
- A function that extracts the type key from the a polymorphic value.codec
- A function that produces a codec for a subtype corresponding to the provided type key.KeyDispatchCodec
static <N extends Number & Comparable<N>> (N) -> DataResult<N> checkRange(N minInclusive, N maxInclusive)