A - The type this MapCodec serializes and deserializes.public abstract class MapCodec<A> extends CompressorHolder implements MapDecoder<A>, MapEncoder<A>
MapEncoder and MapDecoder.
A MapCodec is a specialized version of a Codec that serializes and deserializes a fixed set of
record fields. While a MapCodec is not itself a Codec, it may be turned into one via the
codec() method.
Implementations of MapCodec are immutable once created. The methods defined in this interface never
mutate the codec in a way visible to the outside.
MapEncoder.encode(Object, DynamicOps, RecordBuilder)
and MapDecoder.decode(DynamicOps, MapLike) methods.Codec| Modifier and Type | Class and Description |
|---|---|
static class |
MapCodec.MapCodecCodec<A>
|
static interface |
MapCodec.ResultFunction<A>
A unary operator applied on the result obtained from decoding or encoding from a
MapCodec. |
MapDecoder.Implementation<A>MapEncoder.Implementation<A>| Constructor and Description |
|---|
MapCodec() |
| Modifier and Type | Method and Description |
|---|---|
Codec<A> |
codec()
Returns a
Codec that implements the same operations as this map codec. |
<E> MapCodec<A> |
dependent(MapCodec<E> initialInstance,
(A) -> (E,MapCodec<E>) splitter,
(A,E) -> A combiner)
Creates a map codec where the encoding and decoding of the given fields depends on the values of the fields
decoded using this map codec.
|
MapCodec<A> |
deprecated(int since)
Sets this map codec to produce deprecated results.
|
MapCodec<A> |
fieldOf(String name)
Returns a
MapCodec that operates on the same type as this map codec, but that extracts the values from
a record field with the given name. |
<S> MapCodec<S> |
flatXmap((? super A) -> ? extends DataResult<? extends S> to,
(? super S) -> ? extends DataResult<? extends A> from)
Transforms this map codec into another map codec using the given partially invertible partial function.
|
<O> RecordCodecBuilder<O,A> |
forGetter((O) -> A getter)
Returns a
RecordCodecBuilder that encodes and decodes the field defined by this map codec. |
abstract <T> Stream<T> |
keys(DynamicOps<T> ops)
Returns the set of keys this object defines or accepts, serialized to the provided form.
|
MapCodec<A> |
mapResult(MapCodec.ResultFunction<A> function)
Transforms the
DataResult returned from this map codec's MapEncoder.encode(Object, DynamicOps, RecordBuilder)
and MapDecoder.decode(DynamicOps, MapLike) methods. |
static <A> MapCodec<A> |
of(MapEncoder<A> encoder,
MapDecoder<A> decoder)
Creates a
MapCodec given a map encoder and a map decoder. |
static <A> MapCodec<A> |
of(MapEncoder<A> encoder,
MapDecoder<A> decoder,
by-name String name)
Creates a named
MapCodec given a map encoder and a map decoder. |
MapCodec<A> |
orElse(A value)
Provides a default value to return if a decoding or encoding error occurs.
|
MapCodec<A> |
orElse((String) -> void onError,
A value)
Provides a callback to run and a default value to return if a decoding or encoding error occurs.
|
MapCodec<A> |
orElse((String) -> String onError,
A value)
Provides an error transformation and a default value to return if a decoding or encoding error occurs.
|
MapCodec<A> |
orElseGet((String) -> void onError,
by-name ? extends A value)
Provides a callback to run and a default value to supply if a decoding or encoding error occurs.
|
MapCodec<A> |
orElseGet(by-name ? extends A value)
Provides a default value to supply if a decoding or encoding error occurs.
|
MapCodec<A> |
orElseGet((String) -> String onError,
by-name ? extends A value)
Provides an error transformation and a default supply to return if a decoding or encoding error occurs.
|
MapCodec<A> |
setPartial(by-name A value) |
MapCodec<A> |
stable()
Sets this map codec to produce stable results.
|
static <A> MapCodec<A> |
unit(A defaultValue)
Returns a map codec that always provides the same value.
|
static <A> MapCodec<A> |
unit(by-name A defaultValue)
Returns a map codec that always provides the supplied value.
|
MapCodec<A> |
withLifecycle(Lifecycle lifecycle)
Sets the
Lifecycle of the results this map codec returns. |
<S> MapCodec<S> |
xmap((? super A) -> ? extends S to,
(? super S) -> ? extends A from)
Transforms this map codec into another map codec using the given invertible mapping functions.
|
compressorclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitap, compressedDecode, compressor, decode, decoder, flatMap, mapcomap, compressedBuilder, compressor, encode, encoder, flatComap, makeCompressedBuilderforStringspublic final <O> RecordCodecBuilder<O,A> forGetter((O) -> A getter)
RecordCodecBuilder that encodes and decodes the field defined by this map codec.
Multiple RecordCodecBuilder objects, each wrapping one map codec, may be combined in order
to create a codec that can serialize and deserialize whole objects.
O - The object type that holds a value this map codec can operate on.getter - A function that extracts a value to encode from some object type.RecordCodecBuilder that encodes and decodes values extracted from getter.RecordCodecBuilderpublic static <A> MapCodec<A> of(MapEncoder<A> encoder, MapDecoder<A> decoder)
MapCodec given a map encoder and a map decoder.A - The type that the returned MapCodec operates on.encoder - The MapEncoder that the returned map codec uses to encode values.decoder - The MapDecoder that the returned map codec uses to decode values.MapCodec that encodes and decodes values based on the provided encoder and decoder.public static <A> MapCodec<A> of(MapEncoder<A> encoder, MapDecoder<A> decoder, by-name String name)
MapCodec given a map encoder and a map decoder. The returned map codec will use the
given name in its string representation.A - The type that the returned MapCodec operates on.encoder - The MapEncoder that the returned map codec uses to encode values.decoder - The MapDecoder that the returned map codec uses to decode values.name - The name that is displayed in the Object.toString() representation of the returned map codec.MapCodec that encodes and decodes values based on the provided encoder and decoder.public MapCodec<A> fieldOf(String name)
MapCodec that operates on the same type as this map codec, but that extracts the values from
a record field with the given name. This method can be used to nest records within one another.codec().fieldOf(name).name - The name of the field values are extracted from.MapCodec that extracts values to be passed to this map codec from a record field.public MapCodec<A> withLifecycle(Lifecycle lifecycle)
Lifecycle of the results this map codec returns.withLifecycle in interface MapDecoder<A>withLifecycle in interface MapEncoder<A>lifecycle - The lifecycle to use.public Codec<A> codec()
Codec that implements the same operations as this map codec.public MapCodec<A> stable()
withLifecycle(Lifecycle),
Lifecycle.stable()public MapCodec<A> deprecated(int since)
since - The deprecation version.withLifecycle(Lifecycle),
Lifecycle.deprecated(int)public <S> MapCodec<S> xmap((? super A) -> ? extends S to, (? super S) -> ? extends A from)
This method performs a map operation on both MapDecoder and MapEncoder.
MapEncoder.comap(Function) and MapDecoder.map(Function) on this map codec.S - The new type.to - A function from this map codec's type to the new type.from - A function from the new type to this map codec's type.MapDecoder.map(Function),
MapEncoder.comap(Function)public <S> MapCodec<S> flatXmap((? super A) -> ? extends DataResult<? extends S> to, (? super S) -> ? extends DataResult<? extends A> from)
This method performs a flatMap operation on both MapDecoder and MapEncoder.
MapEncoder.flatComap(Function) and MapDecoder.flatMap(Function).S - The new type.to - A partial function form this map 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 map codec's type. The value and any errors are wrapped in
a DataResult.MapDecoder.flatMap(Function),
MapEncoder.flatComap(Function)public <E> MapCodec<A> dependent(MapCodec<E> initialInstance, (A) -> (E,MapCodec<E>) splitter, (A,E) -> A combiner)
This is similar to Codec.dispatch(String, Function, Function), except that the fields that are
extracted are the same fields that are dispatched.
E - The type representing the collection of dependent fields.initialInstance - A codec that specifies the set of dependent fields.splitter - A function that takes an object and extracts both a set of fields and the codecs associated
with those fields, for that particular object.combiner - A function that combines an object and a set of decoded fields.Codec.dispatch(Function, Function),
MapCodec.Dependentpublic abstract <T> Stream<T> keys(DynamicOps<T> ops)
Keyablekeys in interface KeyableT - The type of the serialized form.ops - The DynamicOps instance defining the serialized form.public MapCodec<A> mapResult(MapCodec.ResultFunction<A> function)
DataResult returned from this map codec's MapEncoder.encode(Object, DynamicOps, RecordBuilder)
and MapDecoder.decode(DynamicOps, MapLike) methods.function - The transformation to apply to results.public MapCodec<A> orElse((String) -> void onError, A value)
onError - A callback to run when a decoding or encoding error occurs. The callback receives the error message.value - The default value to use if a decoding error occurs and no partial result is present.public MapCodec<A> orElse((String) -> String onError, A value)
onError - A function that transforms the error message when a decoding or encoding error occurs.value - The default value to use if a decoding error occurs and no partial result is present.public MapCodec<A> orElseGet((String) -> void onError, by-name ? extends A value)
onError - A callback to run when a decoding or encoding error occurs.value - A supplier of the default value to use if a decoding error occurs and no partial result is present.public MapCodec<A> orElseGet((String) -> String onError, by-name ? extends A value)
onError - A function that transforms the error message when a decoding or encoding error occurs.value - A supplier of default value to use if a decoding error occurs and no partial result is present.public MapCodec<A> orElse(A value)
value - The default value to use if a decoding error occurs and no partial result is present.public MapCodec<A> orElseGet(by-name ? extends A value)
value - A supplier of the default value to use if a decoding error occurs and no partial result is present.public static <A> MapCodec<A> unit(A defaultValue)
A - The type of object the returned map codec operates on.defaultValue - The value to provide when decoding.public static <A> MapCodec<A> unit(by-name A defaultValue)
A - The type of object the returned map codec operates on.defaultValue - A supplier of the default value.unit(Object)