Package com.mojang.serialization
Interface Encoder<A>
- Type Parameters:
A
- the type of object to encode
- All Known Subinterfaces:
Codec<A>
,PrimitiveCodec<A>
- All Known Implementing Classes:
CompoundListCodec
,EitherCodec
,ListCodec
,MapCodec.MapCodecCodec
,PairCodec
,UnboundedMapCodec
public interface Encoder<A>
Encodes objects to a serialized form.
-
Method Summary
Modifier and TypeMethodDescriptiondefault <B> Encoder<B>
Transforms the type of this encoder using the given function.static <A> MapEncoder<A>
empty()
Produces aMapEncoder
which encodes no fields, parameterized by a type.<T> DataResult<T>
encode
(A input, DynamicOps<T> ops, T prefix) Encodes an object to a serialized form, adding to an existing prefix.default <T> DataResult<T>
encodeStart
(DynamicOps<T> ops, A input) Encode an object to a serialized form.static <A> Encoder<A>
Produces an encoder which always errors with the given message.default MapEncoder<A>
Returns aMapEncoder
that encodes a single field with the given name using this encoder.default <B> Encoder<B>
flatComap
(Function<? super B, ? extends DataResult<? extends A>> function) Useful when the type to encode cannot always be converted to the type expected by this encoder.withLifecycle
(Lifecycle lifecycle) Produces a new encoder with the givenLifecycle
.
-
Method Details
-
encode
Encodes an object to a serialized form, adding to an existing prefix.- Type Parameters:
T
- the type to serialize to- Parameters:
input
- the object to encodeops
- aDynamicOps
for the target serialized formatprefix
- existing data that the encoded object should be added to- Returns:
- a
DataResult
containing the serialized form of the object, or an error if the object could not be serialized
-
encodeStart
Encode an object to a serialized form.- Type Parameters:
T
- the type to serialize to- Parameters:
ops
- aDynamicOps
for the target serialized formatinput
- the object to encode- Returns:
- a
DataResult
containing the serialized form of the object, or an error if the object could not be serialized
-
fieldOf
Returns aMapEncoder
that encodes a single field with the given name using this encoder.- Returns:
- a
MapEncoder
that encodes a single field with the given name using this encoder
-
comap
Transforms the type of this encoder using the given function.- Type Parameters:
B
- the type of object for the new encoder to encode- Parameters:
function
- a function to apply to the input before encoding- Returns:
- a new encoder
-
flatComap
Useful when the type to encode cannot always be converted to the type expected by this encoder.- Type Parameters:
B
- the type of object for the new encoder to encode- Parameters:
function
- a function to apply to the input before encoding- Returns:
- a new encoder
-
withLifecycle
Produces a new encoder with the givenLifecycle
.- Parameters:
lifecycle
- the lifecycle to apply to the new encoder- Returns:
- a new encoder
-
empty
Produces aMapEncoder
which encodes no fields, parameterized by a type.- Type Parameters:
A
- the type of object for the new encoder to encode- Returns:
- a new
MapEncoder
- See Also:
-
error
Produces an encoder which always errors with the given message.- Type Parameters:
A
- the type of object for the new encoder to encode- Parameters:
error
- the error message- Returns:
- a new encoder
- See Also:
-