Kit
Functions

getMapCodec

Call Signature

getMapCodec<TFromKey, TFromValue, TToKey, TToValue>(key, value, config): FixedSizeCodec<Map<TFromKey, TFromValue>, Map<TToKey, TToValue>, 0>

Returns a codec for encoding and decoding maps.

This codec serializes maps where the key/value pairs are encoded and decoded one after another using the provided key and value codecs. The number of entries is determined by the size configuration and defaults to a u32 size prefix.

Type Parameters

Type ParameterDefault typeDescription
TFromKey-The type of the keys before encoding.
TFromValue-The type of the values before encoding.
TToKeyTFromKeyThe type of the keys after decoding.
TToValueTFromValueThe type of the values after decoding.

Parameters

ParameterTypeDescription
keyCodec<TFromKey, TToKey>The codec for the map's keys.
valueCodec<TFromValue, TToValue>The codec for the map's values.
configMapCodecConfig<NumberCodec> & objectConfiguration options for encoding and decoding the map.

Returns

FixedSizeCodec<Map<TFromKey, TFromValue>, Map<TToKey, TToValue>, 0>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding maps.

Examples

Encoding and decoding a map with a u32 size prefix (default).

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec());
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x02000000616c6963652a626f62000005
//   |       |         | |         └── Value (5)
//   |       |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |       |         └── Value (42)
//   |       └── Key ("alice", 5 bytes fixed)
//   └── 4-byte prefix (2 entries)
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Encoding and decoding a map with a u16 size prefix.

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec(), { size: getU16Codec() });
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x0200616c6963652a626f62000005
//   |   |         | |         └── Value (5)
//   |   |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |   |         └── Value (42)
//   |   └── Key ("alice", 5 bytes fixed)
//   └── 2-byte prefix (2 entries)
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Encoding and decoding a fixed-size map.

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec(), { size: 2 });
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x616c6963652a626f62000005
//   |         | |         └── Value (5)
//   |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |         └── Value (42)
//   └── Key ("alice", 5 bytes fixed)
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Encoding and decoding a map with remainder size.

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec(), { size: 'remainder' });
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x616c6963652a626f62000005
//   |         | |         └── Value (5)
//   |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |         └── Value (42)
//   └── Key ("alice", 5 bytes fixed)
// No size prefix, the size is inferred from the remaining bytes.
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Remarks

Separate getMapEncoder and getMapDecoder functions are available.

const bytes = getMapEncoder(fixCodecSize(getUtf8Encoder(), 5), getU8Encoder()).encode(new Map([['alice', 42]]));
const map = getMapDecoder(fixCodecSize(getUtf8Decoder(), 5), getU8Decoder()).decode(bytes);

See

Call Signature

getMapCodec<TFromKey, TFromValue, TToKey, TToValue>(key, value, config): FixedSizeCodec<Map<TFromKey, TFromValue>, Map<TToKey, TToValue>>

Returns a codec for encoding and decoding maps.

This codec serializes maps where the key/value pairs are encoded and decoded one after another using the provided key and value codecs. The number of entries is determined by the size configuration and defaults to a u32 size prefix.

Type Parameters

Type ParameterDefault typeDescription
TFromKey-The type of the keys before encoding.
TFromValue-The type of the values before encoding.
TToKeyTFromKeyThe type of the keys after decoding.
TToValueTFromValueThe type of the values after decoding.

Parameters

ParameterTypeDescription
keyFixedSizeCodec<TFromKey, TToKey>The codec for the map's keys.
valueFixedSizeCodec<TFromValue, TToValue>The codec for the map's values.
configMapCodecConfig<NumberCodec> & objectConfiguration options for encoding and decoding the map.

Returns

FixedSizeCodec<Map<TFromKey, TFromValue>, Map<TToKey, TToValue>>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding maps.

Examples

Encoding and decoding a map with a u32 size prefix (default).

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec());
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x02000000616c6963652a626f62000005
//   |       |         | |         └── Value (5)
//   |       |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |       |         └── Value (42)
//   |       └── Key ("alice", 5 bytes fixed)
//   └── 4-byte prefix (2 entries)
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Encoding and decoding a map with a u16 size prefix.

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec(), { size: getU16Codec() });
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x0200616c6963652a626f62000005
//   |   |         | |         └── Value (5)
//   |   |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |   |         └── Value (42)
//   |   └── Key ("alice", 5 bytes fixed)
//   └── 2-byte prefix (2 entries)
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Encoding and decoding a fixed-size map.

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec(), { size: 2 });
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x616c6963652a626f62000005
//   |         | |         └── Value (5)
//   |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |         └── Value (42)
//   └── Key ("alice", 5 bytes fixed)
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Encoding and decoding a map with remainder size.

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec(), { size: 'remainder' });
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x616c6963652a626f62000005
//   |         | |         └── Value (5)
//   |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |         └── Value (42)
//   └── Key ("alice", 5 bytes fixed)
// No size prefix, the size is inferred from the remaining bytes.
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Remarks

Separate getMapEncoder and getMapDecoder functions are available.

const bytes = getMapEncoder(fixCodecSize(getUtf8Encoder(), 5), getU8Encoder()).encode(new Map([['alice', 42]]));
const map = getMapDecoder(fixCodecSize(getUtf8Decoder(), 5), getU8Decoder()).decode(bytes);

See

Call Signature

getMapCodec<TFromKey, TFromValue, TToKey, TToValue>(key, value, config?): VariableSizeCodec<Map<TFromKey, TFromValue>, Map<TToKey, TToValue>>

Returns a codec for encoding and decoding maps.

This codec serializes maps where the key/value pairs are encoded and decoded one after another using the provided key and value codecs. The number of entries is determined by the size configuration and defaults to a u32 size prefix.

Type Parameters

Type ParameterDefault typeDescription
TFromKey-The type of the keys before encoding.
TFromValue-The type of the values before encoding.
TToKeyTFromKeyThe type of the keys after decoding.
TToValueTFromValueThe type of the values after decoding.

Parameters

ParameterTypeDescription
keyCodec<TFromKey, TToKey>The codec for the map's keys.
valueCodec<TFromValue, TToValue>The codec for the map's values.
config?MapCodecConfig<NumberCodec>Configuration options for encoding and decoding the map.

Returns

VariableSizeCodec<Map<TFromKey, TFromValue>, Map<TToKey, TToValue>>

A FixedSizeCodec or VariableSizeCodec for encoding and decoding maps.

Examples

Encoding and decoding a map with a u32 size prefix (default).

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec());
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x02000000616c6963652a626f62000005
//   |       |         | |         └── Value (5)
//   |       |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |       |         └── Value (42)
//   |       └── Key ("alice", 5 bytes fixed)
//   └── 4-byte prefix (2 entries)
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Encoding and decoding a map with a u16 size prefix.

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec(), { size: getU16Codec() });
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x0200616c6963652a626f62000005
//   |   |         | |         └── Value (5)
//   |   |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |   |         └── Value (42)
//   |   └── Key ("alice", 5 bytes fixed)
//   └── 2-byte prefix (2 entries)
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Encoding and decoding a fixed-size map.

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec(), { size: 2 });
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x616c6963652a626f62000005
//   |         | |         └── Value (5)
//   |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |         └── Value (42)
//   └── Key ("alice", 5 bytes fixed)
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Encoding and decoding a map with remainder size.

const codec = getMapCodec(fixCodecSize(getUtf8Codec(), 5), getU8Codec(), { size: 'remainder' });
const bytes = codec.encode(new Map([['alice', 42], ['bob', 5]]));
// 0x616c6963652a626f62000005
//   |         | |         └── Value (5)
//   |         | └── Key ("bob", 5 bytes fixed, null-padded)
//   |         └── Value (42)
//   └── Key ("alice", 5 bytes fixed)
// No size prefix, the size is inferred from the remaining bytes.
 
const map = codec.decode(bytes);
// new Map([['alice', 42], ['bob', 5]])

Remarks

Separate getMapEncoder and getMapDecoder functions are available.

const bytes = getMapEncoder(fixCodecSize(getUtf8Encoder(), 5), getU8Encoder()).encode(new Map([['alice', 42]]));
const map = getMapDecoder(fixCodecSize(getUtf8Decoder(), 5), getU8Decoder()).decode(bytes);

See

On this page