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.
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.
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.
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.
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.
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.
Separate getArrayEncoder and getArrayDecoder functions are available.