Class KeyDispatchCodec<K,V>
- Type Parameters:
K
- the type of the keyV
- the type of the value
- All Implemented Interfaces:
Compressable
,Keyable
,MapDecoder<V>
,MapEncoder<V>
Note: codecs constructed from a MapCodec.MapCodecCodec
have special behaviour compared to
other codecs; while other codecs, act on a "value
field within the structure that the dispatch codec is
decoding, a MapCodec.MapCodecCodec
that has been delegated to (such as the output of MapCodec.codec()
or RecordCodecBuilder.create(Function)
will instead decode and encode to the root level of the data. This
means that if you want this behaviour, you should make sure that your codec is a MapCodec.MapCodecCodec
- which
means using the MapCodec
specific xmap
/flatXmap
methods instead of the ones on Codec
in any transformations you might do! This behaviour can be disabled by setting the assumeMap
argument of the
constructor to true
, in which case every delegated codec will attempt to decode the full input object. Also of
note is that this behaviour is lost when using a DynamicOps
with DynamicOps.compressMaps()
, in which
case the delegated codec is always encoded in a value field.
Note: On encoding the function fed into the type
argument of both constructors is used to find
a key from an object, and that key is then used to find the encoder using the function fed into the constructor;
however, there is no guarantee that the capture of ? super E
fed into the first function is the same capture
that comes out of the second! In other words, this method is not inherently typesafe. Take care that whatever
functions you provide only involve a single capture of that generic for a given key; a common approach is using the
delegated codec itself as the key.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.mojang.serialization.MapCodec
MapCodec.MapCodecCodec<A>, MapCodec.ResultFunction<A>
Nested classes/interfaces inherited from interface com.mojang.serialization.MapDecoder
MapDecoder.Implementation<A>
Nested classes/interfaces inherited from interface com.mojang.serialization.MapEncoder
MapEncoder.Implementation<A>
-
Constructor Summary
ModifierConstructorDescriptionKeyDispatchCodec
(String typeKey, Codec<K> keyCodec, Function<? super V, ? extends DataResult<? extends K>> type, Function<? super K, ? extends DataResult<? extends Codec<? extends V>>> codec) protected
KeyDispatchCodec
(String typeKey, Codec<K> keyCodec, Function<? super V, ? extends DataResult<? extends K>> type, Function<? super K, ? extends DataResult<? extends Decoder<? extends V>>> decoder, Function<? super V, ? extends DataResult<? extends Encoder<V>>> encoder, boolean assumeMap) -
Method Summary
Modifier and TypeMethodDescription<T> DataResult<V>
decode
(DynamicOps<T> ops, MapLike<T> input) <T> RecordBuilder<T>
encode
(V input, DynamicOps<T> ops, RecordBuilder<T> prefix) <T> Stream<T>
keys
(DynamicOps<T> ops) Returns a stream of keys this map codec may write or read.toString()
static <K,
V> KeyDispatchCodec<K, V> unsafe
(String typeKey, Codec<K> keyCodec, Function<? super V, ? extends DataResult<? extends K>> type, Function<? super K, ? extends DataResult<? extends Decoder<? extends V>>> decoder, Function<? super V, ? extends DataResult<? extends Encoder<V>>> encoder) Methods inherited from class com.mojang.serialization.MapCodec
codec, dependent, deprecated, fieldOf, flatXmap, forGetter, mapResult, of, of, orElse, orElse, orElse, orElseGet, orElseGet, orElseGet, setPartial, stable, unit, unit, withLifecycle, xmap
Methods inherited from class com.mojang.serialization.CompressorHolder
compressor
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.mojang.serialization.MapDecoder
ap, compressedDecode, compressor, decoder, flatMap, map
Methods inherited from interface com.mojang.serialization.MapEncoder
comap, compressedBuilder, compressor, encoder, flatComap
-
Constructor Details
-
Method Details
-
unsafe
public static <K,V> KeyDispatchCodec<K,V> unsafe(String typeKey, Codec<K> keyCodec, Function<? super V, ? extends DataResult<? extends K>> type, Function<? super K, ? extends DataResult<? extends Decoder<? extends V>>> decoder, Function<? super V, ? extends DataResult<? extends Encoder<V>>> encoder) - Mojang-Added Docs:
Will assume that the result of all elements is a map
-
decode
-
encode
-
keys
Description copied from class:MapCodec
Returns a stream of keys this map codec may write or read. This stream should contain any key that may be written or read, even ones that are optional. The codec need not read or write all of these keys on any given occasion, but all keys that might be read or written should be included, as this stream is used to form the number-based indexing used withDynamicOps.compressMaps()
.Note: if a map codec writes a key that is not returned by this method, it will not work correctly with a
DynamicOps
that hasDynamicOps.compressMaps()
. -
toString
-