Constructs a union codec that can serialize and deserialize values tagged with a type identifier.
In accordance with the Molecule specification, union is always a dynamic-size type, regardless of whether its variants have identical fixed lengths.
union
If you need a fixed-size union type compatible with fixed containers like struct, array, or vector (as FixVec), use fixedUnion instead.
struct
array
vector
Serialization format:
A record mapping variant names to codecs.
An object whose keys are variant names and values are codecs for each variant.
Optional
Optional mapping from variant names to custom numeric IDs. If omitted, the index of each variant in codecLayout is used as its ID.
codecLayout
// Union without custom numeric IDsunion({ cafe: Uint8, bee: Uint16 });// Union with custom numeric IDsunion({ cafe: Uint8, bee: Uint16 }, { cafe: 0xcafe, bee: 0xbee });// Fixed-size union (explicit extension):fixedUnion({ cafe: Uint16, bee: Uint16 }); Copy
// Union without custom numeric IDsunion({ cafe: Uint8, bee: Uint16 });// Union with custom numeric IDsunion({ cafe: Uint8, bee: Uint16 }, { cafe: 0xcafe, bee: 0xbee });// Fixed-size union (explicit extension):fixedUnion({ cafe: Uint16, bee: Uint16 });
Constructs a union codec that can serialize and deserialize values tagged with a type identifier.
In accordance with the Molecule specification,
unionis always a dynamic-size type, regardless of whether its variants have identical fixed lengths.If you need a fixed-size union type compatible with fixed containers like
struct,array, orvector(as FixVec), use fixedUnion instead.Serialization format: