Interface Codec<T>
- Type Parameters:
T- The Java type that this codec is responsible for handling.
- All Known Implementing Classes:
Codec.EitherCodec, Codec.OneOfCodec
public interface Codec<T>
A bidirectional serializer and deserializer capable of translating between Java objects
and various serialized formats defined by a
DynamicOps provider.-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classAn unchecked exception utilized for failures occurring during the serialization or deserialization process.static final recordInternal implementation for handling theEithertype, allowing for bifurcated data structures.static final recordRepresents the definition of a single field within a structured object.static final recordInternal implementation that iterates through a list of codecs until one successfully processes the data. -
Method Summary
Modifier and TypeMethodDescriptiondefault <C extends Collection<T>>
Codec<C> collection(Supplier<C> factory) Creates a codec for a specific collection implementation.<D> Tdecode(DynamicOps<D> ops, D input) Decodes a serialized input of a specific data format into a Java object.Creates a codec for anEithercontainer.<D> Dencode(DynamicOps<D> ops, T value) Encodes a Java object into its serialized representation.Creates a codec for an Enum type using its string name.static <T> Codec<T> Combines two codecs such that the secondary is used if the primary fails.default <P> Codec.Field<P, T> Defines this codec as a field within a larger object structure.list()Creates a codec for aListof elements of type T.Creates a codec for a Map structure.nullable()Modifies the codec to treat null Java values as empty serialized states.static <T> Codec<T> Factory method to create a simple codec from mapping functions.static <T> Codec<T> Combines multiple codecs to be tried in sequence.optional()Wraps the current codec to handle JavaOptionalvalues.Wraps this codec with a default value to be used if decoding fails or returns null.default <U> Codec<U> Performs an unsafe cast of this codec to another type.default <R> Codec<R> Transforms this codec to handle a new type R via two-way conversion functions.
-
Method Details
-
decode
Decodes a serialized input of a specific data format into a Java object.- Type Parameters:
D- The type of the serialized data (e.g., JsonElement, NBTCompound, ByteBuf).- Parameters:
ops- The provider defining how to navigate and read data of type D.input- The raw serialized input to be processed.- Returns:
- The resulting Java object of type T.
- Throws:
Codec.CodecException- If the input data is malformed, missing fields, or type-mismatched.
-
encode
Encodes a Java object into its serialized representation.- Type Parameters:
D- The target type of the serialized data.- Parameters:
ops- The provider defining how to construct data of type D.value- The Java object instance to be serialized.- Returns:
- The serialized data representing the object.
- Throws:
Codec.CodecException- If the object contains non-serializable states or illegal values.
-
of
Factory method to create a simple codec from mapping functions.- Type Parameters:
T- The target Java type.- Parameters:
decoder- Function to convert raw objects (from DynamicOps) to type T.encoder- Function to convert type T to raw objects.- Returns:
- A basic Codec implementation.
-
map
-
enumCodec
-
fallback
Combines two codecs such that the secondary is used if the primary fails. Internally implemented as a OneOfCodec wrapper.- Type Parameters:
T- Target type.- Parameters:
left- Primary codec.right- Fallback codec.- Returns:
- A fallback-enabled codec.
-
either
-
oneOf
Combines multiple codecs to be tried in sequence.- Type Parameters:
T- Target type.- Parameters:
codecs- Array of codecs to attempt.- Returns:
- A multi-branch codec.
-
xmap
default <R> Codec<R> xmap(Function<? super T, ? extends R> forward, Function<? super R, ? extends T> backward) Transforms this codec to handle a new type R via two-way conversion functions.- Type Parameters:
R- The new target Java type.- Parameters:
forward- Conversion function from T to R.backward- Conversion function from R to T.- Returns:
- A codec for type R.
-
orElse
-
list
-
unchecked
Performs an unsafe cast of this codec to another type.- Type Parameters:
U- Target type.- Returns:
- The same codec instance cast to Codec of U.
-
collection
Creates a codec for a specific collection implementation. Handles complex logic to propagate list serialization through OneOf codecs.- Type Parameters:
C- The specific collection type (e.g., Set, Queue).- Parameters:
factory- Supplier to create a new collection instance.- Returns:
- A collection-specific codec.
-
optional
-
nullable
-
fieldOf
Defines this codec as a field within a larger object structure.- Type Parameters:
P- The parent object type.- Parameters:
name- The name of the field.getter- The function to get the field value from the parent.- Returns:
- A Field definition object.
-