Interface StructCodec<R>

Type Parameters:
R - the return type, never null.
All Superinterfaces:
Codec<R>, Decoder<R>, Encoder<R>

public interface StructCodec<R> extends Codec<R>
A struct codec is a map backed Codec, where the keys are strings. See Codec, Decoder and Encoder
You can also use struct(String, Codec, Function, F1) to create as templating similar to NetworkBufferTemplate


You can use structs to create complex objects


 record MyObject(double coolnessFactor, @Nullable String of) {
     static final StructCodec<MyObject> CODEC = StructCodec.struct(
             "id", Codec.DOUBLE, MyObject::coolnessFactor,
             "name", Codec.STRING.optional(), MyObject::of,
             MyObject::new
     );

     public MyObject {
         coolnessFactor = Math.clamp(coolnessFactor, 0.0, 2.0); // Too powerful
     }
 }

 MyObject value = new MyObject(7.8d, "me"); // Or use a null name for no name.
 // Encoding to JSON
 JsonElement encoded = MyObject.CODEC.encode(Transcoder.JSON, value).orElseThrow();
 // Decoding from JSON
 MyObject decoded = MyObject.CODEC.decode(Transcoder.JSON, encoded).orElseThrow();