O
- The type of the complete object.F
- The type of the field or collection of fields handled by this builder.public final class RecordCodecBuilder<O,F> extends Object
map codecs
into a single codec for some complete
type. This allows for building a single codec out of the codecs for many independent fields.
Typically, RecordCodecBuilder
is used to create a codec for a record like the following.
public class SomeRecord {
private int intValue;
private String stringValue;
private List<String> stringValues;
public SomeRecord(int i, String s, List<String> ls) {
this.intValue = i;
this.stringValue = s;
this.stringValues = ls;
}
// methods elided
}
A codec for this type may be created like so.
public static final Codec<SomeRecord> CODEC = RecordCodecBuilder.create(inst -> inst.group(
Codec.INT.fieldOf("IntValue").forGetter(r -> r.intValue),
Codec.STRING.fieldOf("StringValue").forGetter(r -> r.stringValue),
Codec.STRING.listOf().fieldOf("StringValues").forGetter(r -> r.stringValues)
).apply(inst, SomeRecord::new));
Each independent record field is added to the builder using Codec.fieldOf(String)
(which specifies which
field in the record the codec is for) and MapCodec.forGetter(Function)
(which specifies how to get the field
value out of the record). These record field codecs are then aggregated and a combining function (typically a
constructor or factory method) combines the fields into an instance of the complete record.
Note that the "record" in RecordCodecBuilder
does not refer to the Java concept of a record, but rather
to any object that can be logically serialized as a set of fields.
Modifier and Type | Class and Description |
---|---|
static class |
RecordCodecBuilder.Instance<O>
The
Applicative instance for RecordCodecBuilder . |
static class |
RecordCodecBuilder.Mu<O>
The witness type of a
RecordCodecBuilder . |
Modifier and Type | Method and Description |
---|---|
static <O> MapCodec<O> |
build(RecordCodecBuilder<O,O> builderBox)
Builds a
MapCodec that operates on the fields defined using the given RecordCodecBuilder . |
static <O> Codec<O> |
create((instance RecordCodecBuilder<O,_>) -> ? extends RecordCodecBuilder<O,O> builder)
Creates a
Codec using the fields provided via the given function. |
<E> RecordCodecBuilder<O,E> |
dependent((O) -> E getter,
MapEncoder<E> encoder,
(? super F) -> ? extends MapDecoder<E> decoderGetter) |
static <O,F> RecordCodecBuilder<O,F> |
deprecated(F instance,
int since)
Creates a builder for a deprecated singleton value.
|
static <O> instance RecordCodecBuilder<O,_> |
instance()
Returns the applicative type class instance for
RecordCodecBuilder . |
static <O> MapCodec<O> |
mapCodec((instance RecordCodecBuilder<O,_>) -> ? extends RecordCodecBuilder<O,O> builder)
Creates a
MapCodec using the fields provided via the given function. |
static <O,F> RecordCodecBuilder<O,F> |
of((O) -> F getter,
MapCodec<F> codec)
Creates a builder for a field or collection of fields in some object, given a map codec for the fields.
|
static <O,F> RecordCodecBuilder<O,F> |
of((O) -> F getter,
String name,
Codec<F> fieldCodec)
Creates a builder for a single field in some object, given a field name and a
Codec for that field. |
static <O,F> RecordCodecBuilder<O,F> |
point(F instance)
Creates a builder for an experimental singleton field value.
|
static <O,F> RecordCodecBuilder<O,F> |
point(F instance,
Lifecycle lifecycle)
Creates a builder for a singleton value with the given lifecycle.
|
static <O,F> RecordCodecBuilder<O,F> |
stable(F instance)
Creates a builder for a stable singleton value.
|
static <O,F> RecordCodecBuilder<O,F> |
unbox(RecordCodecBuilder<O,F> box)
Thunk method that casts an applied
RecordCodecBuilder.Mu to a RecordCodecBuilder . |
public static <O,F> RecordCodecBuilder<O,F> unbox(RecordCodecBuilder<O,F> box)
RecordCodecBuilder.Mu
to a RecordCodecBuilder
.O
- The type of the complete object.F
- The type of the field or collection of fields handled by the builder.box
- The boxed builder.public static <O> instance RecordCodecBuilder<O,_> instance()
RecordCodecBuilder
.O
- The type of the complete object.RecordCodecBuilder
.Applicative
public static <O,F> RecordCodecBuilder<O,F> of((O) -> F getter, String name, Codec<F> fieldCodec)
Codec
for that field.O
- The type of the complete object.F
- The type of the field.getter
- A function that extracts the value of the field from a complete object.name
- The name the field is serialized under.fieldCodec
- A Codec
for the field.public static <O,F> RecordCodecBuilder<O,F> of((O) -> F getter, MapCodec<F> codec)
O
- The type of the complete object.F
- The type of the field or collection of fields.getter
- A function that extracts the fields from a complete object.codec
- A MapCodec
for the fields.public static <O,F> RecordCodecBuilder<O,F> point(F instance)
point(Object, Lifecycle)
invoked with Lifecycle.experimental()
.O
- The type of the complete object.F
- The type of the field.instance
- The singleton value.Codec.unit(Object)
public static <O,F> RecordCodecBuilder<O,F> stable(F instance)
point(Object, Lifecycle)
invoked with Lifecycle.stable()
.O
- The type of the complete object.F
- The type of the field.instance
- The singleton value.public static <O,F> RecordCodecBuilder<O,F> deprecated(F instance, int since)
point(Object, Lifecycle)
invoked with Lifecycle.deprecated(int)
.O
- The type of the complete object.F
- The type of the field.instance
- The singleton value.since
- The deprecation version.public static <O,F> RecordCodecBuilder<O,F> point(F instance, Lifecycle lifecycle)
O
- The type of the complete object.F
- The type of the field.instance
- The singleton value.lifecycle
- The lifecycle of the given field value.public static <O> Codec<O> create((instance RecordCodecBuilder<O,_>) -> ? extends RecordCodecBuilder<O,O> builder)
Codec
using the fields provided via the given function. The builder function is expected
to create a RecordCodecBuilder
for a complete object with some set of fields.O
- The type of the complete object.builder
- A function that produces a builder for some set of fields.Codec
for the complete object type.RecordCodecBuilder
public static <O> MapCodec<O> mapCodec((instance RecordCodecBuilder<O,_>) -> ? extends RecordCodecBuilder<O,O> builder)
MapCodec
using the fields provided via the given function. The builder function is expected
to create a RecordCodecBuilder
for a complete object with some set of fields.O
- The type of the complete object.builder
- A function that produces a builder for some set of fields.Codec
for the complete object type.create(Function)
public <E> RecordCodecBuilder<O,E> dependent((O) -> E getter, MapEncoder<E> encoder, (? super F) -> ? extends MapDecoder<E> decoderGetter)
public static <O> MapCodec<O> build(RecordCodecBuilder<O,O> builderBox)
MapCodec
that operates on the fields defined using the given RecordCodecBuilder
.O
- The type of the complete object.builderBox
- The builder.MapCodec
created from the given builder.