A
- The type this Encoder
serializes.public interface Encoder<A>
An Encoder
is used to transform objects of a specific type to some serialized representation using
a supplied DynamicOps
. Encoders encapsulate the serialization routine for a specific type, regardless
of the desired serialization form.
Implementations of Encoder
are immutable once created. The methods defined in this interface never
mutate the encoder in a way visible to the outside.
Decoder
,
Codec
Modifier and Type | Method and Description |
---|---|
default <B> Encoder<B> |
comap((? super B) -> ? extends A function)
Transforms this encoder to operate on a different type using the given mapping function.
|
static <A> MapEncoder<A> |
empty()
A
MapEncoder that performs no serialization. |
<T> DataResult<T> |
encode(A input,
DynamicOps<T> ops,
T prefix)
Encodes an object into the specified serialized data.
|
default <T> DataResult<T> |
encodeStart(DynamicOps<T> ops,
A input)
Encodes an object.
|
static <A> Encoder<A> |
error(String error)
An
Encoder that performs no serialization. |
default MapEncoder<A> |
fieldOf(String name)
Returns a
MapEncoder that encodes objects in a record under a field with the given name. |
default <B> Encoder<B> |
flatComap((? super B) -> ? extends DataResult<? extends A> function)
Transforms this encoder to operate on a different type using the given
DataResult -producing mapping function. |
default Encoder<A> |
withLifecycle(Lifecycle lifecycle)
Sets the
Lifecycle for the serialized data this encoder produces. |
<T> DataResult<T> encode(A input, DynamicOps<T> ops, T prefix)
DataResult
.T
- The type of the serialized form.input
- The object to serialize.ops
- The DynamicOps
instance defining the serialized form.prefix
- The existing serialized data to append to.DataResult
wrapping the serialized form of input
, appended to prefix
.default <T> DataResult<T> encodeStart(DynamicOps<T> ops, A input)
DataResult
.
For merging the input into existing serialized data, use encode(Object, DynamicOps, Object)
.
encode(input, ops, ops.empty())
.T
- The type of the serialized form.input
- The object to serialize.ops
- The DynamicOps
instance defining the serialized form.DataResult
wrapping the serialized form of input
.encode(Object, DynamicOps, Object)
default MapEncoder<A> fieldOf(String name)
MapEncoder
that encodes objects in a record under a field with the given name. The returned
MapEncoder
may be used in conjunction with a RecordBuilder
to serialize many different fields,
all using different encoders, to a single record.FieldEncoder
wrapping this encoder.name
- The field to encode objects into.MapEncoder
that performs the same encoding as this encoder, but places the serialized value
in a record under the given field.RecordBuilder
default <B> Encoder<B> comap((? super B) -> ? extends A function)
Encoder
.Encoder
that wraps this encoder.B
- The new type of the encoder.function
- A function transforming the new type to the old type.B
.default <B> Encoder<B> flatComap((? super B) -> ? extends DataResult<? extends A> function)
DataResult
-producing mapping function.Encoder
.Encoder
that wraps this encoder.B
- The new type of the encoder.function
- A function transforming the new type to the old type. Errors from this encoder and from the function
are merged.B
.comap(Function)
default Encoder<A> withLifecycle(Lifecycle lifecycle)
Lifecycle
for the serialized data this encoder produces.static <A> MapEncoder<A> empty()
MapEncoder
that performs no serialization. Its encoding
method returns the prefix unchanged and it has an empty key stream.A
- The type the returned MapEncoder
operates on.MapEncoder
that performs no serialization.