getArrayCodec
Call Signature
Returns a codec for encoding and decoding arrays of values.
This codec serializes arrays by encoding each element using the provided item codec.
By default, a u32 size prefix is included to indicate the number of items in the array.
The size option can be used to modify this behaviour.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TFrom | - | The type of the elements to encode. |
TTo | TFrom | The type of the decoded elements. |
Parameters
| Parameter | Type | Description |
|---|---|---|
item | Codec<TFrom, TTo> | The codec for each item in the array. |
config | ArrayCodecConfig<NumberCodec> & object | Optional configuration for the size encoding/decoding strategy. |
Returns
FixedSizeCodec<TFrom[], TTo[], 0>
A VariableSizeCodec<TFrom[], TTo[]> for encoding and decoding arrays.
Examples
Encoding and decoding an array of u8 numbers.
Using a u16 size prefix instead of u32.
Using a fixed-size array of 3 items.
Using the "remainder" size strategy.
Using a sentinel to mark the end of the array. Note that no valid item may begin with the sentinel's bytes, or decoding would stop early — see ArrayLikeCodecSentinelSize for the full constraints.
Requiring the size prefix to be present when decoding.
Remarks
The size of the array can be controlled using the size option:
- A
Codec<number>(e.g.getU16Codec()) stores a size prefix before the array. - A
numberenforces a fixed number of elements. "remainder"uses all remaining bytes to infer the array length.- A sentinel object (see ArrayLikeCodecSentinelSize) ends the array when the bytes at the next item position match a constant sentinel.
When the size is stored as a prefix, decoding an exhausted byte array yields an empty array,
which allows arrays to be appended to existing data layouts without breaking older data.
Use the requireSizePrefix option to throw instead.
Separate getArrayEncoder and getArrayDecoder functions are available.
See
Call Signature
Returns a codec for encoding and decoding arrays of values.
This codec serializes arrays by encoding each element using the provided item codec.
By default, a u32 size prefix is included to indicate the number of items in the array.
The size option can be used to modify this behaviour.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TFrom | - | The type of the elements to encode. |
TTo | TFrom | The type of the decoded elements. |
Parameters
| Parameter | Type | Description |
|---|---|---|
item | FixedSizeCodec<TFrom, TTo> | The codec for each item in the array. |
config | ArrayCodecConfig<NumberCodec> & object | Optional configuration for the size encoding/decoding strategy. |
Returns
FixedSizeCodec<TFrom[], TTo[]>
A VariableSizeCodec<TFrom[], TTo[]> for encoding and decoding arrays.
Examples
Encoding and decoding an array of u8 numbers.
Using a u16 size prefix instead of u32.
Using a fixed-size array of 3 items.
Using the "remainder" size strategy.
Using a sentinel to mark the end of the array. Note that no valid item may begin with the sentinel's bytes, or decoding would stop early — see ArrayLikeCodecSentinelSize for the full constraints.
Requiring the size prefix to be present when decoding.
Remarks
The size of the array can be controlled using the size option:
- A
Codec<number>(e.g.getU16Codec()) stores a size prefix before the array. - A
numberenforces a fixed number of elements. "remainder"uses all remaining bytes to infer the array length.- A sentinel object (see ArrayLikeCodecSentinelSize) ends the array when the bytes at the next item position match a constant sentinel.
When the size is stored as a prefix, decoding an exhausted byte array yields an empty array,
which allows arrays to be appended to existing data layouts without breaking older data.
Use the requireSizePrefix option to throw instead.
Separate getArrayEncoder and getArrayDecoder functions are available.
See
Call Signature
Returns a codec for encoding and decoding arrays of values.
This codec serializes arrays by encoding each element using the provided item codec.
By default, a u32 size prefix is included to indicate the number of items in the array.
The size option can be used to modify this behaviour.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TFrom | - | The type of the elements to encode. |
TTo | TFrom | The type of the decoded elements. |
Parameters
| Parameter | Type | Description |
|---|---|---|
item | Codec<TFrom, TTo> | The codec for each item in the array. |
config? | ArrayCodecConfig<NumberCodec> | Optional configuration for the size encoding/decoding strategy. |
Returns
VariableSizeCodec<TFrom[], TTo[]>
A VariableSizeCodec<TFrom[], TTo[]> for encoding and decoding arrays.
Examples
Encoding and decoding an array of u8 numbers.
Using a u16 size prefix instead of u32.
Using a fixed-size array of 3 items.
Using the "remainder" size strategy.
Using a sentinel to mark the end of the array. Note that no valid item may begin with the sentinel's bytes, or decoding would stop early — see ArrayLikeCodecSentinelSize for the full constraints.
Requiring the size prefix to be present when decoding.
Remarks
The size of the array can be controlled using the size option:
- A
Codec<number>(e.g.getU16Codec()) stores a size prefix before the array. - A
numberenforces a fixed number of elements. "remainder"uses all remaining bytes to infer the array length.- A sentinel object (see ArrayLikeCodecSentinelSize) ends the array when the bytes at the next item position match a constant sentinel.
When the size is stored as a prefix, decoding an exhausted byte array yields an empty array,
which allows arrays to be appended to existing data layouts without breaking older data.
Use the requireSizePrefix option to throw instead.
Separate getArrayEncoder and getArrayDecoder functions are available.