getOptionCodec
Call Signature
getOptionCodec<
TFrom
,TTo
,TSize
>(item
,config
):FixedSizeCodec
<OptionOrNullable
<TFrom
>,Option
<TTo
>,TSize
>
Returns a codec for encoding and decoding optional values using the Option type.
This codec serializes and deserializes Option<T>
values using a configurable approach:
- By default, a
u8
prefix is used (0 = None
,1 = Some
). - If
noneValue: 'zeroes'
is set,None
values are encoded/decoded as zeroes. - If
noneValue
is a byte array,None
values are represented by the provided constant. - If
prefix: null
is set, the codec determinesNone
values solely fromnoneValue
or the presence of bytes.
For more details on the configuration options, see OptionCodecConfig.
Note that this behaves similarly to getNullableCodec, except it encodes OptionOrNullable values and decodes Option values.
Type Parameters
Type Parameter | Description |
---|---|
TFrom | The type of the main value being encoded. |
TTo | The type of the main value being decoded. |
TSize extends number | - |
Parameters
Parameter | Type | Description |
---|---|---|
item | FixedSizeCodec <TFrom , TTo , TSize > | The codec for the value that may be present. |
config | OptionCodecConfig <NumberCodec > & object | Configuration options for encoding and decoding option values. |
Returns
FixedSizeCodec
<OptionOrNullable
<TFrom
>, Option
<TTo
>, TSize
>
A FixedSizeCodec
or VariableSizeCodec
for encoding and decoding option values.
Examples
Encoding and decoding an optional string with a size prefix.
Encoding nullable values.
Encoding and decoding an optional number with a fixed size.
Encoding and decoding None values with a custom byte sequence and no prefix.
Identifying None values by the absence of bytes.
Remarks
Separate getOptionEncoder and getOptionDecoder functions are available.
See
Call Signature
getOptionCodec<
TFrom
,TTo
>(item
,config
):FixedSizeCodec
<OptionOrNullable
<TFrom
>,Option
<TTo
>>
Returns a codec for encoding and decoding optional values using the Option type.
This codec serializes and deserializes Option<T>
values using a configurable approach:
- By default, a
u8
prefix is used (0 = None
,1 = Some
). - If
noneValue: 'zeroes'
is set,None
values are encoded/decoded as zeroes. - If
noneValue
is a byte array,None
values are represented by the provided constant. - If
prefix: null
is set, the codec determinesNone
values solely fromnoneValue
or the presence of bytes.
For more details on the configuration options, see OptionCodecConfig.
Note that this behaves similarly to getNullableCodec, except it encodes OptionOrNullable values and decodes Option values.
Type Parameters
Type Parameter | Default type | Description |
---|---|---|
TFrom | - | The type of the main value being encoded. |
TTo | TFrom | The type of the main value being decoded. |
Parameters
Parameter | Type | Description |
---|---|---|
item | FixedSizeCodec <TFrom , TTo > | The codec for the value that may be present. |
config | OptionCodecConfig <FixedSizeNumberCodec > & object | Configuration options for encoding and decoding option values. |
Returns
FixedSizeCodec
<OptionOrNullable
<TFrom
>, Option
<TTo
>>
A FixedSizeCodec
or VariableSizeCodec
for encoding and decoding option values.
Examples
Encoding and decoding an optional string with a size prefix.
Encoding nullable values.
Encoding and decoding an optional number with a fixed size.
Encoding and decoding None values with a custom byte sequence and no prefix.
Identifying None values by the absence of bytes.
Remarks
Separate getOptionEncoder and getOptionDecoder functions are available.
See
Call Signature
getOptionCodec<
TFrom
,TTo
>(item
,config
):VariableSizeCodec
<OptionOrNullable
<TFrom
>,Option
<TTo
>>
Returns a codec for encoding and decoding optional values using the Option type.
This codec serializes and deserializes Option<T>
values using a configurable approach:
- By default, a
u8
prefix is used (0 = None
,1 = Some
). - If
noneValue: 'zeroes'
is set,None
values are encoded/decoded as zeroes. - If
noneValue
is a byte array,None
values are represented by the provided constant. - If
prefix: null
is set, the codec determinesNone
values solely fromnoneValue
or the presence of bytes.
For more details on the configuration options, see OptionCodecConfig.
Note that this behaves similarly to getNullableCodec, except it encodes OptionOrNullable values and decodes Option values.
Type Parameters
Type Parameter | Default type | Description |
---|---|---|
TFrom | - | The type of the main value being encoded. |
TTo | TFrom | The type of the main value being decoded. |
Parameters
Parameter | Type | Description |
---|---|---|
item | FixedSizeCodec <TFrom , TTo > | The codec for the value that may be present. |
config | OptionCodecConfig <NumberCodec > & object | Configuration options for encoding and decoding option values. |
Returns
VariableSizeCodec
<OptionOrNullable
<TFrom
>, Option
<TTo
>>
A FixedSizeCodec
or VariableSizeCodec
for encoding and decoding option values.
Examples
Encoding and decoding an optional string with a size prefix.
Encoding nullable values.
Encoding and decoding an optional number with a fixed size.
Encoding and decoding None values with a custom byte sequence and no prefix.
Identifying None values by the absence of bytes.
Remarks
Separate getOptionEncoder and getOptionDecoder functions are available.
See
Call Signature
getOptionCodec<
TFrom
,TTo
>(item
,config?
):VariableSizeCodec
<OptionOrNullable
<TFrom
>,Option
<TTo
>>
Returns a codec for encoding and decoding optional values using the Option type.
This codec serializes and deserializes Option<T>
values using a configurable approach:
- By default, a
u8
prefix is used (0 = None
,1 = Some
). - If
noneValue: 'zeroes'
is set,None
values are encoded/decoded as zeroes. - If
noneValue
is a byte array,None
values are represented by the provided constant. - If
prefix: null
is set, the codec determinesNone
values solely fromnoneValue
or the presence of bytes.
For more details on the configuration options, see OptionCodecConfig.
Note that this behaves similarly to getNullableCodec, except it encodes OptionOrNullable values and decodes Option values.
Type Parameters
Type Parameter | Default type | Description |
---|---|---|
TFrom | - | The type of the main value being encoded. |
TTo | TFrom | The type of the main value being decoded. |
Parameters
Parameter | Type | Description |
---|---|---|
item | Codec <TFrom , TTo > | The codec for the value that may be present. |
config? | OptionCodecConfig <NumberCodec > & object | Configuration options for encoding and decoding option values. |
Returns
VariableSizeCodec
<OptionOrNullable
<TFrom
>, Option
<TTo
>>
A FixedSizeCodec
or VariableSizeCodec
for encoding and decoding option values.
Examples
Encoding and decoding an optional string with a size prefix.
Encoding nullable values.
Encoding and decoding an optional number with a fixed size.
Encoding and decoding None values with a custom byte sequence and no prefix.
Identifying None values by the absence of bytes.
Remarks
Separate getOptionEncoder and getOptionDecoder functions are available.