Interface Codec<A>
- Type Parameters:
A
- the type to encode and decode
- All Known Subinterfaces:
PrimitiveCodec<A>
- All Known Implementing Classes:
CompoundListCodec
,EitherCodec
,ListCodec
,MapCodec.MapCodecCodec
,PairCodec
,UnboundedMapCodec
Encoder
and Decoder
which can convert objects to and from a serialized form.
A codec is one of the core components of DFU's serialization API. Conceptually, a codec generally represents a series
of two-way transformations going from the serialized form to the object type, going through any number of intermediate
steps.
Codecs are normally built through methods that combine or transform existing codecs. The Codec
class
contains a number of built-in codecs for simple types, such as STRING
; while more complicated codecs
can be created by implementing the interface directly, it is often easier to use the built-in methods to combine or
transform existing codecs. Some common tools for building codecs include:
listOf()
for decoding lists of a typeunboundedMap(Codec, Codec)
for building maps with arbitrary keysunit(Object)
for codecs representing a unit, or singleton, typepair(Codec, Codec)
for codecs which decode and encode both of two types to the same dataeither(Codec, Codec)
for codecs which decode and encode either of two types to the same data
xmap(Function, Function)
when each type can always be converted to the otherflatXmap(Function, Function)
when either conversion could sometimes failflatComapMap(Function, Function)
when the transformation from the target type to this codec's type might failcomapFlatMap(Function, Function)
when the transformation from this codec's type to the target type might fail
MapCodec
and RecordCodecBuilder
.
Codecs are quite versatile, and a variety of other implementations exist.- See Also:
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
An operator on the result a codec, describing how to transform the result on both encoding and decoding.Nested classes/interfaces inherited from interface com.mojang.serialization.Decoder
Decoder.Boxed<A>, Decoder.Simple<A>, Decoder.Terminal<A>
-
Field Summary
Modifier and TypeFieldDescriptionstatic final PrimitiveCodec<Boolean>
APrimitiveCodec
for representing booleans.static final PrimitiveCodec<Byte>
APrimitiveCodec
for representing bytes.static final PrimitiveCodec<ByteBuffer>
APrimitiveCodec
for representing aByteBuffer
.static final PrimitiveCodec<Double>
APrimitiveCodec
for representing doubles.AMapCodec
which always decodesUnit.INSTANCE
and encodes nothing.static final PrimitiveCodec<Float>
APrimitiveCodec
for representing floats.static final PrimitiveCodec<Integer>
APrimitiveCodec
for representing integers.static final PrimitiveCodec<IntStream>
APrimitiveCodec
for representing anIntStream
.static final PrimitiveCodec<Long>
APrimitiveCodec
for representing longs.static final PrimitiveCodec<LongStream>
APrimitiveCodec
for representing aLongStream
.A codec that represents serialized data itself, alongside information about its format, as aDynamic
.static final PrimitiveCodec<Short>
APrimitiveCodec
for representing shorts.static final PrimitiveCodec<String>
APrimitiveCodec
for representing strings. -
Method Summary
Modifier and TypeMethodDescriptiondefault <S> Codec<S>
comapFlatMap
(Function<? super A, ? extends DataResult<? extends S>> to, Function<? super S, ? extends A> from) Similar toxmap(Function, Function)
, except that the forwards (decoding) transformation produces aDataResult
and is allowed to fail.compoundList
(Codec<K> keyCodec, Codec<V> elementCodec) Returns a codec which represents a list of key-value pairs as a map-like structure in serialized form.deprecated
(int since) Returns a version of this codec with a deprecated lifecycle.default <E> Codec<E>
dispatch
(String typeKey, Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) Delegate codecs allow using the value of an initially decoded key to determine how to decode the rest of the input.default <E> Codec<E>
dispatch
(Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) An overload ofdispatch(String, Function, Function)
which uses the string"type"
as name of the key field.default <E> MapCodec<E>
dispatchMap
(String typeKey, Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) Similar todispatch(String, Function, Function)
except thatMapCodec
is constructed instead of aCodec
.default <E> MapCodec<E>
dispatchMap
(Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) default <E> Codec<E>
dispatchStable
(Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) Similar todispatch(Function, Function)
except that the decoding of the key is considered to be in theLifecycle.stable()
lifecycle.doubleRange
(double minInclusive, double maxInclusive) Returns a codec that will encode and decode doubles only within the specified range.Creates a new codec that decodes and encodes either of two different types from the same data.Creates aMapCodec
representing this as single field in a map-like structure when serialized.default <S> Codec<S>
flatComapMap
(Function<? super A, ? extends S> to, Function<? super S, ? extends DataResult<? extends A>> from) Similar toxmap(Function, Function)
, except that the backwards (encoding) transformation produces aDataResult
and is allowed to fail.default <S> Codec<S>
flatXmap
(Function<? super A, ? extends DataResult<? extends S>> to, Function<? super S, ? extends DataResult<? extends A>> from) Similar toxmap(Function, Function)
, except that both the forwards and backwards transformations produce aDataResult
and are allowed to fail.floatRange
(float minInclusive, float maxInclusive) Returns a codec that will encode and decode floats only within the specified range.intRange
(int minInclusive, int maxInclusive) Returns a codec that will encode and decode integers only within the specified range.Returns a new codec that decodes and encodes a list of a single type.listOf()
Returns a new codec that decodes and encodes a list of a single type.Creates a newMapCodec
that decodes and encodes either of two different types from the same data.Creates a newMapCodec
that decodes and encodes two different types from the same data, in sequence.mapResult
(Codec.ResultFunction<A> function) Creates a new codec by transforming the current one with the givenCodec.ResultFunction
.static <A> Codec<A>
Returns a new codec with the behavior of the given encoder and decoder.static <A> Codec<A>
Returns a new codec with the behavior of the given encoder and decoder, with a custom name.static <A> MapCodec<A>
of
(MapEncoder<A> encoder, MapDecoder<A> decoder) static <A> MapCodec<A>
of
(MapEncoder<A> encoder, MapDecoder<A> decoder, Supplier<String> name) optionalField
(String name, Codec<F> elementCodec) Creates aMapCodec
representing a type as single field in a map-like structure when serialized which may be absent.optionalFieldOf
(String name) Similar tofieldOf(String)
, except that the produced field may be absent.optionalFieldOf
(String name, A defaultValue) Similar tofieldOf(String)
, except that the produced field may be absent.optionalFieldOf
(String name, A defaultValue, Lifecycle lifecycleOfDefault) Equivalent tooptionalFieldOf(String, Object)
except that when the field is absent, the produced result will have the givenLifecycle
.optionalFieldOf
(String name, Lifecycle fieldLifecycle, A defaultValue, Lifecycle lifecycleOfDefault) Equivalent tooptionalFieldOf(String, Object, Lifecycle)
except that the lifecycle of non-default results can be specified.Creates a codec which attempts to decode using this codec, and if that fails, returns the given value.Similar toorElse(UnaryOperator, Object)
except that the error message is not transformed, but only fed to the provided consumer.orElse
(UnaryOperator<String> onError, A value) Creates a codec which attempts to decode using this codec, and if that fails, returns the given value and maps the error using the given function while encoding or decoding.Similar toorElse(Consumer, Object)
except that the default value is calculated lazily.Similar toorElse(Object)
except that the default value is calculated lazily.orElseGet
(UnaryOperator<String> onError, Supplier<? extends A> value) Similar toorElse(UnaryOperator, Object)
except that the default value is calculated lazily.Creates a new codec that decodes and encodes two different types from the same data, in sequence.default <E> Codec<E>
partialDispatch
(String typeKey, Function<? super E, ? extends DataResult<? extends A>> type, Function<? super A, ? extends DataResult<? extends Codec<? extends E>>> codec) Creates a dispatch codec where each of the transforming functions produces aDataResult
and is allowed to fail.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.static <K,
V> SimpleMapCodec<K, V> Creates aMapCodec
which represents bounded map - that is, a map with a fixed set of keys - as a map-like structure when serialized.stable()
Returns a version of this codec with the stable lifecycle.static <K,
V> UnboundedMapCodec<K, V> unboundedMap
(Codec<K> keyCodec, Codec<V> elementCodec) Creates a codec which represents an unbounded map - that is, a map with an arbitrary set of keys - as a map-like structure when serialized.static <A> Codec<A>
unit
(A defaultValue) Returns a codec which always decodes the provided value, and encodes nothing.static <A> Codec<A>
Returns a codec which always decodes a value from the provided supplier, and encodes nothing.withLifecycle
(Lifecycle lifecycle) Produces a new codec with the givenLifecycle
.default <S> Codec<S>
Creates a new codec by transforming the output of this codec during decoding, and the input while encoding.Methods inherited from interface com.mojang.serialization.Decoder
boxed, decode, decode, flatMap, map, parse, parse, simple, terminal
Methods inherited from interface com.mojang.serialization.Encoder
comap, encode, encodeStart, flatComap
-
Field Details
-
BOOL
APrimitiveCodec
for representing booleans. -
BYTE
APrimitiveCodec
for representing bytes. -
SHORT
APrimitiveCodec
for representing shorts. -
INT
APrimitiveCodec
for representing integers. -
LONG
APrimitiveCodec
for representing longs. -
FLOAT
APrimitiveCodec
for representing floats. -
DOUBLE
APrimitiveCodec
for representing doubles. -
STRING
APrimitiveCodec
for representing strings. -
BYTE_BUFFER
APrimitiveCodec
for representing aByteBuffer
. -
INT_STREAM
APrimitiveCodec
for representing anIntStream
. -
LONG_STREAM
APrimitiveCodec
for representing aLongStream
. -
PASSTHROUGH
A codec that represents serialized data itself, alongside information about its format, as aDynamic
. -
EMPTY
AMapCodec
which always decodesUnit.INSTANCE
and encodes nothing.
-
-
Method Details
-
withLifecycle
Produces a new codec with the givenLifecycle
.- Specified by:
withLifecycle
in interfaceDecoder<A>
- Specified by:
withLifecycle
in interfaceEncoder<A>
- Parameters:
lifecycle
- the lifecycle to apply to the new codec- Returns:
- a new codec
-
stable
Returns a version of this codec with the stable lifecycle.- Returns:
- a version of this codec with the stable lifecycle
- See Also:
-
deprecated
Returns a version of this codec with a deprecated lifecycle.- Returns:
- a version of this codec with a deprecated lifecycle
- See Also:
-
of
Returns a new codec with the behavior of the given encoder and decoder.- Type Parameters:
A
- the type to encode and decode- Parameters:
encoder
- used to encode objects to a serialized formdecoder
- used to decode objects from a serialized form- Returns:
- a new codec with the behavior of the given encoder and decoder
-
of
Returns a new codec with the behavior of the given encoder and decoder, with a custom name.- Type Parameters:
A
- the type to encode and decode- Parameters:
encoder
- used to encode objects to a serialized formdecoder
- used to decode objects from a serialized formname
- the name of the codec when printed- Returns:
- a new codec with the behavior of the given encoder and decoder, with a custom name
-
of
- Type Parameters:
A
- the type to encode and decode- Parameters:
encoder
- used to encode objects to key-value pairs in a serialized formdecoder
- used to decode objects from a serialized form- Returns:
- a new map
MapCodec
with the behavior of the given encoder and decoder
-
of
- Type Parameters:
A
- the type to encode and decode- Parameters:
encoder
- used to encode objects to key-value pairs in a serialized formdecoder
- used to decode objects from a serialized formname
- the name of the codec when printed- Returns:
- a new map
MapCodec
with the behavior of the given encoder and decoder
-
pair
Creates a new codec that decodes and encodes two different types from the same data, in sequence. Note that while decoding, the second codec only sees data not consumed by the first codec; if the first codec consumes all the provided data, the second codec will be given an empty input. This works particularly well with codecs constructed withMapCodec.codec()
, as different codecs can consume different keys.- Type Parameters:
F
- the type to decode first and encode secondS
- the type to decode second and encode first- Parameters:
first
- the codec used first on encoding and second on decodingsecond
- the codec used second on encoding and first on decoding- Returns:
- a new codec that decodes and encodes two different types from the same data, in sequence
-
either
Creates a new codec that decodes and encodes either of two different types from the same data. If the first codec fails to decode, the second codec will be used instead. If both codecs fail to decode, the error from the second codec will be returned, and the error from the first silently swallowed.- Type Parameters:
F
- the type to attempt to decode firstS
- the type to attempt to decode second- Parameters:
first
- the codec used to attempt decoding first, or to encode the left componentsecond
- the codec used to attempt decoding second, or to encode the right component- Returns:
- a new codec that decodes and encodes either of two different types from the same data
-
mapPair
Creates a newMapCodec
that decodes and encodes two different types from the same data, in sequence. Note that unlike when usingpair(Codec, Codec)
, no data is "consumed" by aMapCodec
, so the secondMapCodec
will see the same input as the first.- Type Parameters:
F
- the type to decode and encode firstS
- the type to decode and encode second- Parameters:
first
- used to encode and decode the first componentsecond
- used to encode and decode the second component- Returns:
- a new
MapCodec
that decodes and encodes two different types from the same data, in sequence
-
mapEither
Creates a newMapCodec
that decodes and encodes either of two different types from the same data. If the firstMapCodec
fails to decode, the secondMapCodec
will be used instead. If both fail to decode, the error from the second codec will be returned, and the error from the first silently swallowed.- Type Parameters:
F
- the type to attempt to decode firstS
- the type to attempt to decode second- Parameters:
first
- used to attempt decoding first, or to encode the left componentsecond
- used to attempt decoding second, or to encode the right component- Returns:
- a new
MapCodec
that decodes and encodes either of two different types from the same data
-
list
Returns a new codec that decodes and encodes a list of a single type.- Type Parameters:
E
- the type to decode and encode- Parameters:
elementCodec
- the codec used to encode and decode the elements of the list- Returns:
- a new codec that decodes and encodes a list of a single type
- See Also:
-
compoundList
Returns a codec which represents a list of key-value pairs as a map-like structure in serialized form.- Type Parameters:
K
- the type of the keysV
- the type of the values- Parameters:
keyCodec
- the codec used to encode and decode the keys of the map. Will likely cause issues if the keys do not eventually serialize to stringselementCodec
- the codec used to encode and decode the values of the map- Returns:
- a codec which represents a list of key-value pairs as a map-like structure in serialized form
-
simpleMap
Creates aMapCodec
which represents bounded map - that is, a map with a fixed set of keys - as a map-like structure when serialized.- Type Parameters:
K
- the type of the keysV
- the type of the values- Parameters:
keyCodec
- used to encode and decode the keys of the map. Will likely cause issues if the keys do not eventually serialize to stringselementCodec
- used to encode and decode the values of the mapkeys
- provides the set of keys that the map can contain- Returns:
- a new
SimpleMapCodec
representing a bounded map
-
unboundedMap
Creates a codec which represents an unbounded map - that is, a map with an arbitrary set of keys - as a map-like structure when serialized.- Type Parameters:
K
- the type of the keysV
- the type of the values- Parameters:
keyCodec
- used to encode and decode the keys of the map. Will likely cause issues if the keys do not eventually serialize to stringselementCodec
- used to encode and decode the values of the map- Returns:
- a new
UnboundedMapCodec
representing an unbounded map
-
optionalField
Creates aMapCodec
representing a type as single field in a map-like structure when serialized which may be absent.- Type Parameters:
F
- the type of the field- Parameters:
name
- the name of the fieldelementCodec
- the codec used to encode and decode the value of the field- Returns:
- a new
MapCodec
-
listOf
Returns a new codec that decodes and encodes a list of a single type.- Returns:
- a new codec that decodes and encodes a list of a single type
- See Also:
-
xmap
Creates a new codec by transforming the output of this codec during decoding, and the input while encoding.- Type Parameters:
S
- the target type of the new codec- Parameters:
to
- applied to the output of this codec during decoding, to produce the output of the new codecfrom
- applied to the input of this codec during encoding- Returns:
- a new codec
- See Also:
-
comapFlatMap
default <S> Codec<S> comapFlatMap(Function<? super A, ? extends DataResult<? extends S>> to, Function<? super S, ? extends A> from) Similar toxmap(Function, Function)
, except that the forwards (decoding) transformation produces aDataResult
and is allowed to fail.- Type Parameters:
S
- the target type of the new codec- Parameters:
to
- applied to the output of this codec during decoding, to produce the output of the new codecfrom
- applied to the input of this codec during encoding- Returns:
- a new codec
- See Also:
-
flatComapMap
default <S> Codec<S> flatComapMap(Function<? super A, ? extends S> to, Function<? super S, ? extends DataResult<? extends A>> from) Similar toxmap(Function, Function)
, except that the backwards (encoding) transformation produces aDataResult
and is allowed to fail.- Type Parameters:
S
- the target type of the new codec- Parameters:
to
- applied to the output of this codec during decoding, to produce the output of the new codecfrom
- applied to the input of this codec during encoding- Returns:
- a new codec
- See Also:
-
flatXmap
default <S> Codec<S> flatXmap(Function<? super A, ? extends DataResult<? extends S>> to, Function<? super S, ? extends DataResult<? extends A>> from) Similar toxmap(Function, Function)
, except that both the forwards and backwards transformations produce aDataResult
and are allowed to fail.- Type Parameters:
S
- the target type of the new codec- Parameters:
to
- applied to the output of this codec during decoding, to produce the output of the new codecfrom
- applied to the input of this codec during encoding- Returns:
- a new codec
- See Also:
-
fieldOf
Creates aMapCodec
representing this as single field in a map-like structure when serialized. -
optionalFieldOf
Similar tofieldOf(String)
, except that the produced field may be absent. Thus, the deserialized type is anOptional
of the codec type- Parameters:
name
- the name of the field- Returns:
- a new
MapCodec
-
optionalFieldOf
Similar tofieldOf(String)
, except that the produced field may be absent. A default value is provided to be used when the field is absent, and if the default value is encoded nothing will be encoded.- Parameters:
name
- the name of the fielddefaultValue
- the default value to use when the field is absent- Returns:
- a new
MapCodec
-
optionalFieldOf
Equivalent tooptionalFieldOf(String, Object)
except that when the field is absent, the produced result will have the givenLifecycle
. If the field is present, the lifecycle of the produced result will beLifecycle.experimental()
.- Parameters:
name
- the name of the fielddefaultValue
- the default value to use when the field is absent, or when the encoded value is the defaultlifecycleOfDefault
- the lifecycle to provide in the produced result when the field is absent- Returns:
- a new
MapCodec
-
optionalFieldOf
default MapCodec<A> optionalFieldOf(String name, Lifecycle fieldLifecycle, A defaultValue, Lifecycle lifecycleOfDefault) Equivalent tooptionalFieldOf(String, Object, Lifecycle)
except that the lifecycle of non-default results can be specified.- Parameters:
name
- the name of the fieldfieldLifecycle
- the lifecycle to provide in the produced result when the field is presentdefaultValue
- the default value to use when the field is absent, or when the encoded value is the defaultlifecycleOfDefault
- the lifecycle to provide in the produced result when the field is absent- Returns:
- a new
MapCodec
-
mapResult
Creates a new codec by transforming the current one with the givenCodec.ResultFunction
.- Parameters:
function
- the function to apply to the result of encoding and decoding- Returns:
- a new codec
-
orElse
Similar toorElse(UnaryOperator, Object)
except that the error message is not transformed, but only fed to the provided consumer.- Parameters:
onError
- is called with the error message if decoding or encoding failsvalue
- the value to return if decoding fails- Returns:
- a new codec
-
orElse
Creates a codec which attempts to decode using this codec, and if that fails, returns the given value and maps the error using the given function while encoding or decoding. The result of the providing function is thrown out on decoding, as the default value is used, but any side effects will still occur.- Parameters:
onError
- a function to map the error message on encoding or decodingvalue
- the value to return if decoding fails- Returns:
- a new codec
-
orElseGet
Similar toorElse(Consumer, Object)
except that the default value is calculated lazily.- Parameters:
onError
- is called with the error message if decoding or encoding failsvalue
- supplies the value to return if decoding fails- Returns:
- a new codec
-
orElseGet
Similar toorElse(UnaryOperator, Object)
except that the default value is calculated lazily.- Parameters:
onError
- a function to map the error message on encoding or decodingvalue
- supplies the value to return if decoding fails- Returns:
- a new codec
-
orElse
Creates a codec which attempts to decode using this codec, and if that fails, returns the given value.- Parameters:
value
- the value to return if decoding fails- Returns:
- a new codec
-
orElseGet
Similar toorElse(Object)
except that the default value is calculated lazily.- Parameters:
value
- supplies the value to return if decoding fails- Returns:
- a new codec
-
promotePartial
Description copied from interface:Decoder
Returns a new decoder that decodes the same object as this decoder, but also runs the given action on an error.- Specified by:
promotePartial
in interfaceDecoder<A>
- 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
-
unit
Returns a codec which always decodes the provided value, and encodes nothing. Useful for representing singleton or unit types.- Type Parameters:
A
- the type to encode and decode- Parameters:
defaultValue
- the value to decode to- Returns:
- a codec which always decodes the provided value, and encodes nothing
-
unit
Returns a codec which always decodes a value from the provided supplier, and encodes nothing. Useful for representing singleton or unit types where the single instance is supplied lazily.- Type Parameters:
A
- the type to encode and decode- Parameters:
defaultValue
- supplies the value to decode to- Returns:
- a codec which always decodes a value from the provided supplier, and encodes nothing
-
dispatch
default <E> Codec<E> dispatch(Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) An overload ofdispatch(String, Function, Function)
which uses the string"type"
as name of the key field.Note: dispatch codecs have several quirks it is good to be aware of. See
dispatch(String, Function, Function)
andKeyDispatchCodec
.- Type Parameters:
E
- the type to encode and decode- Parameters:
type
- a function which can find a dispatch key from an instance of the target typecodec
- a function which can find a codec from a dispatch key- Returns:
- a new codec
-
dispatch
default <E> Codec<E> dispatch(String typeKey, Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) Delegate codecs allow using the value of an initially decoded key to determine how to decode the rest of the input. On encoding, the behaviour is similar, with the dispatch codec finding a codec from the supplied type, using that to encode the value and using the receiver to encode the key. The reciever for this method is a codec decoding the type of the key. Note that delegated-to codecs constructed from aMapCodec.MapCodecCodec
have special behaviour compared to other codecs; seeKeyDispatchCodec
for more information.Note: the second argument, which provides a codec given the key, is used on both encoding on decoding. As on encoding the
type
function is first used to find a key, and thecodec
function only used after that, there is no guarantee that the capture of? super E
is the same in both cases! 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 codec itself as the key.- Type Parameters:
E
- the type to encode and decode- Parameters:
typeKey
- the name of the field which will contain the key in the serialized formtype
- a function which can find a dispatch key from an instance of the target typecodec
- a function which can find a codec from a dispatch key- Returns:
- a new codec
- See Also:
-
dispatchStable
default <E> Codec<E> dispatchStable(Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) Similar todispatch(Function, Function)
except that the decoding of the key is considered to be in theLifecycle.stable()
lifecycle.Note: dispatch codecs have several quirks it is good to be aware of. See
dispatch(String, Function, Function)
andKeyDispatchCodec
.- Type Parameters:
E
- the type to encode and decode- Parameters:
type
- a function which can find a dispatch key from an instance of the target typecodec
- a function which can find a codec from a dispatch key- Returns:
- a new codec
-
partialDispatch
default <E> Codec<E> partialDispatch(String typeKey, Function<? super E, ? extends DataResult<? extends A>> type, Function<? super A, ? extends DataResult<? extends Codec<? extends E>>> codec) Creates a dispatch codec where each of the transforming functions produces aDataResult
and is allowed to fail.Note: dispatch codecs have several quirks it is good to be aware of. See
dispatch(String, Function, Function)
andKeyDispatchCodec
.- Type Parameters:
E
- the type to encode and decode- Parameters:
typeKey
- the name of the field which will contain the key in the serialized formtype
- a function which can find a dispatch key or fail from an instance of the target typecodec
- a function which can find a codec or fail from a dispatch key- Returns:
- a new codec
- See Also:
-
dispatchMap
default <E> MapCodec<E> dispatchMap(Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) Similar todispatch(Function, Function)
except thatMapCodec
is constructed instead of aCodec
.Note: dispatch codecs have several quirks it is good to be aware of. See
dispatch(String, Function, Function)
andKeyDispatchCodec
.- Type Parameters:
E
- the type to encode and decode- Parameters:
type
- a function which can find a dispatch key from an instance of the target typecodec
- a function which can find a codec from a dispatch key- Returns:
- a new
MapCodec
-
dispatchMap
default <E> MapCodec<E> dispatchMap(String typeKey, Function<? super E, ? extends A> type, Function<? super A, ? extends Codec<? extends E>> codec) Similar todispatch(String, Function, Function)
except thatMapCodec
is constructed instead of aCodec
.Note: dispatch codecs have several quirks it is good to be aware of. See
dispatch(String, Function, Function)
andKeyDispatchCodec
.- Type Parameters:
E
- the type to encode and decode- Parameters:
typeKey
- the name of the field which will contain the key in the serialized formtype
- a function which can find a dispatch key from an instance of the target typecodec
- a function which can find a codec from a dispatch key- Returns:
- a new
MapCodec
-
intRange
Returns a codec that will encode and decode integers only within the specified range.- Parameters:
minInclusive
- the minimum value to accept, inclusivemaxInclusive
- the maximum value to accept, inclusive- Returns:
- a codec that will encode and decode integers only within the specified range
-
floatRange
Returns a codec that will encode and decode floats only within the specified range.- Parameters:
minInclusive
- the minimum value to accept, inclusivemaxInclusive
- the maximum value to accept, inclusive- Returns:
- a codec that will encode and decode floats only within the specified range
-
doubleRange
Returns a codec that will encode and decode doubles only within the specified range.- Parameters:
minInclusive
- the minimum value to accept, inclusivemaxInclusive
- the maximum value to accept, inclusive- Returns:
- a codec that will encode and decode doubles only within the specified range
-