Interface Codec<T extends @UnknownNullability Object>
- Type Parameters:
T- The type to be represented by this codec, nullable T will provide nullable results.
- All Known Subinterfaces:
DataComponent<T>, StructCodec<R>
A Codec represents a combined Encoder and Decoder for a value.
Enabling easy encoding and decoding of values to and from a between formats, making serialization simple, reusable and type safe.
Going between formats is handled by Transcoder.
Most of the primitive or commonly used codecs are provided as static fields in this interface.
For example, INT is a codec for integers, and STRING is a codec for strings.
You can even use Enum(Class) for enums, which will convert the enum to a string
representation and back.
Codec<@Nullable String> codec = Codec.STRING.optional()
Codec<Set<@Nullable String>> setCodec = codec.set();
Heavily inspired by Mojang/DataFixerUpper, licensed under the MIT license.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA raw value wrapper for entry is an object combined with its current decoder. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Codec<byte[]> static final Codec<int[]> static final Codec<long[]> static final StructCodec<CompoundBinaryTag> static final Codec<Codec.RawValue> -
Method Summary
Modifier and TypeMethodDescriptionCreates an Either Codec, depending on the value of Either decides which codec to use.static <L,R> StructCodec <Either<L, R>> EitherStruct(StructCodec<L> leftCodec, StructCodec<R> rightCodec) Creates an Either Codec, depending on the value of Either decides which codec to use.Creates an enum codec from a given class
Converts theEnum.name()into lowercase when encoding and uppercase into decoding then passing it toEnum.valueOf(Class, String)static <T> Codec<T> ForwardRef(Supplier<Codec<T>> supplier) Lazily gets the reference of a codec; considered immutably lazy.default Codec<@Unmodifiable List<T>> list()Creates an unmodifiable unbounded list codec.default Codec<@Unmodifiable List<T>> list(int maxSize) Creates an unmodifiable list codec ofTwhere its size is no larger thanmaxSize.default Codec<@Unmodifiable @Nullable List<T>> Returns an unmodifiable unbounded list or the first element or null if no such element exists.default Codec<@Unmodifiable @Nullable List<T>> listOrSingle(int maxSize) Returns an unmodifiable list or the first element or null if no such element exists.default <V> Codec<@Unmodifiable Map<T, V>> default <V> Codec<@Unmodifiable Map<T, V>> optional()Creates an optional codec, where null is encodable intoTranscoder.createNull().default Codec<@UnknownNullability T> Creates an optional codec, where null is encodable and is encoded when value equalsdefaultValueor null throughTranscoder.createNull().Creates a or else codec where it will attempt to use the first codec then use the second one if it fails.static <T> Codec<T> Create a recursive codec from the parent codec
Useful when you want to keep encoding/decoding until there is nothing left.static <T> StructCodec<T> RegistryTaggedUnion(String key, Registries.Selector<StructCodec<? extends T>> registrySelector, Function<T, StructCodec<? extends T>> serializerGetter) Creates aStructCodecto bidirectionally map values ofTto their encoded values
Registry selectors will be used to lookup values of codecs ofT.static <T> StructCodec<T> RegistryTaggedUnion(String key, Registry<StructCodec<? extends T>> registry, Function<T, StructCodec<? extends T>> serializerGetter) Shortcut forRegistryTaggedUnion(String, Registries.Selector, Function)whereselectorwill beregistrystatic <T> StructCodec<T> RegistryTaggedUnion(Registries.Selector<StructCodec<? extends T>> registrySelector, Function<T, StructCodec<? extends T>> serializerGetter) static <T> StructCodec<T> RegistryTaggedUnion(Registries.Selector<StructCodec<? extends T>> registrySelector, Function<T, StructCodec<? extends T>> serializerGetter, String key) Deprecated.static <T> StructCodec<T> RegistryTaggedUnion(Registry<StructCodec<? extends T>> registry, Function<T, StructCodec<? extends T>> serializerGetter) static <T> StructCodec<T> RegistryTaggedUnion(Registry<StructCodec<? extends T>> registry, Function<T, StructCodec<? extends T>> serializerGetter, String key) Deprecated.UseRegistryTaggedUnion(String, Registry, Function)instead.default Codec<@Unmodifiable Set<T>> set()Creates an unmodifiable unbounded set.default Codec<@Unmodifiable Set<T>> set(int maxSize) Creates an unmodifiable set where its max is no larger thanmaxSizedefault <S extends @UnknownNullability Object>
Codec<S> transform(ThrowingFunction<T, S> to, ThrowingFunction<S, T> from) default <R> StructCodec<R> unionType(String keyField, Function<T, StructCodec<? extends R>> serializers, Function<R, ? extends T> keyFunc) default <R> StructCodec<R> Creates a union type of typeR.
-
Field Details
-
RAW_VALUE
-
UNIT
-
BOOLEAN
-
BYTE
-
SHORT
-
INT
-
LONG
-
FLOAT
-
DOUBLE
-
STRING
-
KEY
-
BYTE_ARRAY
-
INT_ARRAY
-
LONG_ARRAY
-
UUID
-
UUID_STRING
-
UUID_COERCED
-
COMPONENT
-
COMPONENT_STYLE
-
BLOCK_POSITION
-
VECTOR3D
-
NBT
-
NBT_COMPOUND
-
-
Method Details
-
Enum
Creates an enum codec from a given class
Converts theEnum.name()into lowercase when encoding and uppercase into decoding then passing it toEnum.valueOf(Class, String)- Type Parameters:
E- Enum type, E must be an enum- Parameters:
enumClass- the enum class- Returns:
- the codec enum
-
Recursive
@Contract(pure=true, value="_ -> new") static <T> Codec<T> Recursive(Function<Codec<T>, Codec<T>> func) Create a recursive codec from the parent codec
Useful when you want to keep encoding/decoding until there is nothing left.- Type Parameters:
T- The codec Type- Parameters:
func- the function to get the codec from.- Returns:
- the recursive codec
-
ForwardRef
Lazily gets the reference of a codec; considered immutably lazy.
Useful for breaking possible cyclic loading of recursive codecs. This may become a stable value in the future; don't rely on supplier getting called multiple times.- Type Parameters:
T- the codec type- Parameters:
supplier- the supplier to load the codec from.- Returns:
- the supplier
-
RegistryTaggedUnion
@Deprecated @Contract(pure=true, value="_, _, _ -> new") static <T> StructCodec<T> RegistryTaggedUnion(Registry<StructCodec<? extends T>> registry, Function<T, StructCodec<? extends T>> serializerGetter, String key) Deprecated.UseRegistryTaggedUnion(String, Registry, Function)instead. Shortcut forRegistryTaggedUnion(Registries.Selector, Function, String)- Type Parameters:
T- the struct codec type.- Parameters:
registry- the codec registryserializerGetter- the codec getterkey- the map key- Returns:
- a
StructCodec
-
RegistryTaggedUnion
@Deprecated @Contract(pure=true, value="_, _, _ -> new") static <T> StructCodec<T> RegistryTaggedUnion(Registries.Selector<StructCodec<? extends T>> registrySelector, Function<T, StructCodec<? extends T>> serializerGetter, String key) Deprecated.UseRegistryTaggedUnion(String, Registries.Selector, Function)instead. Creates aStructCodecto bidirectionally map values ofTto their encoded values
Registry selectors will be used to lookup values of codecs ofT. Then will be used to map to objectTfromkey- Type Parameters:
T- the codec type- Parameters:
registrySelector- the registry selector used during lookup.serializerGetter- the serializer for each value ofTkey- the map key forT- Returns:
- a
StructCodecbidirectionally mapping values ofT
-
RegistryTaggedUnion
@Contract(pure=true, value="_, _ -> new") static <T> StructCodec<T> RegistryTaggedUnion(Registry<StructCodec<? extends T>> registry, Function<T, StructCodec<? extends T>> serializerGetter) - Type Parameters:
T- the struct codec type.- Parameters:
registry- the codec registryserializerGetter- the codec getter- Returns:
- a
StructCodec
-
RegistryTaggedUnion
@Contract(pure=true, value="_, _, _ -> new") static <T> StructCodec<T> RegistryTaggedUnion(String key, Registry<StructCodec<? extends T>> registry, Function<T, StructCodec<? extends T>> serializerGetter) Shortcut forRegistryTaggedUnion(String, Registries.Selector, Function)whereselectorwill beregistry- Type Parameters:
T- the struct codec type.- Parameters:
key- the map keyregistry- the codec registryserializerGetter- the codec getter- Returns:
- a
StructCodec
-
RegistryTaggedUnion
@Contract(pure=true, value="_, _ -> new") static <T> StructCodec<T> RegistryTaggedUnion(Registries.Selector<StructCodec<? extends T>> registrySelector, Function<T, StructCodec<? extends T>> serializerGetter) - Type Parameters:
T- the codec type- Parameters:
registrySelector- the registry selector used during lookup.serializerGetter- the serializer for each value ofT- Returns:
- a
StructCodecbidirectionally mapping values ofT
-
RegistryTaggedUnion
@Contract(pure=true, value="_, _, _ -> new") static <T> StructCodec<T> RegistryTaggedUnion(String key, Registries.Selector<StructCodec<? extends T>> registrySelector, Function<T, StructCodec<? extends T>> serializerGetter) Creates aStructCodecto bidirectionally map values ofTto their encoded values
Registry selectors will be used to lookup values of codecs ofT. Then will be used to map to objectTfromkey- Type Parameters:
T- the codec type- Parameters:
key- the map key forTregistrySelector- the registry selector used during lookup.serializerGetter- the serializer for each value ofT- Returns:
- a
StructCodecbidirectionally mapping values ofT
-
Either
-
EitherStruct
@Contract(pure=true, value="_, _ -> new") static <L,R> StructCodec<Either<L,R>> EitherStruct(StructCodec<L> leftCodec, StructCodec<R> rightCodec) Creates an Either Codec, depending on the value of Either decides which codec to use.- Type Parameters:
L- the left typeR- the right type- Parameters:
leftCodec- the left codecrightCodec- the right codec- Returns:
- a
StructCodecwithEitherofLandR
-
optional
-
optional
@Contract(pure=true, value="_ -> new") default Codec<@UnknownNullability T> optional(T defaultValue) Creates an optional codec, where null is encodable and is encoded when value equalsdefaultValueor null throughTranscoder.createNull().
The default value will be used if the decoding is null or fails to decode.- Parameters:
defaultValue- the default value- Returns:
- the optional codec of type
T - Throws:
NullPointerException- if defaultValue is null, useoptional()instead.
-
transform
@Contract(pure=true, value="_, _ -> new") default <S extends @UnknownNullability Object> Codec<S> transform(ThrowingFunction<T, S> to, ThrowingFunction<S, T> from) -
list
-
list
-
listOrSingle
@Contract(pure=true, value="_ -> new") default Codec<@Unmodifiable @Nullable List<T>> listOrSingle(int maxSize) Returns an unmodifiable list or the first element or null if no such element exists.- Parameters:
maxSize- the max size of the list before returning an error result- Returns:
- the list codec of type
T
-
listOrSingle
Returns an unmodifiable unbounded list or the first element or null if no such element exists. SeelistOrSingle(int)- Returns:
- the list codec of type
T
-
set
-
set
-
mapValue
-
mapValue
-
unionType
@Contract(pure=true, value="_, _ -> new") default <R> StructCodec<R> unionType(Function<T, StructCodec<? extends R>> serializers, Function<R, ? extends T> keyFunc) Creates a union type of typeR. SeeunionType(String, Function, Function)
Useful when you have an interface ofTand want a codec subclasses ofT -
unionType
-
orElse
Creates a or else codec where it will attempt to use the first codec then use the second one if it fails.
If both codecs fail the first error will be returned instead.- Parameters:
other- the other codec- Returns:
- the or else codec of
T
-
RegistryTaggedUnion(String, Registries.Selector, Function)instead.