Skip to content

Struct I/O API

Pass a @c.struct() class as the first argument:

FunctionDescription
c.read(ctor, bytes, endian?)Parse bytes into an instance (slice bytes if the layout does not start at 0)
c.write(ctor, instance, endian?)Serialize to a new Uint8Array
c.sizeof(ctor)Total packed size in bytes
c.sizeof("u32")Size of a primitive type in bytes
c.json(ctor, instance)Struct fields as a JSON-friendly object

endian is "little" (default) or "big".

ts
const payload = c.read(MyStruct, file.subarray(headerSize), "little");
const out = c.write(MyStruct, { value: 1 } as MyStruct);

Advanced field types

Built-in helpers (c.Bool, c.String, c.WString, c.Time64, c.bitfield) and custom encodings share the same extension point: subclass c.AdvancedType and pass an instance to @c.field. See Advanced fields.

Types on c

The c namespace also exports layout types (StructLayoutCtor, FieldType, UnionField, …) and helpers (pad, arm, when, enum, advanced field factories).

Errors

Layout and validation failures throw CStructError (also c.CStructError).

c namespace overview

SymbolRole
read, write, sizeof, jsonStruct serialization
struct, fieldDefine layouts
union, unionField, arm, whenDiscriminated unions
enumInteger enum slots
padReserved zero bytes
AdvancedTypeBase class for custom single-slot encodings
Bool, String, WString, Time64, bitfieldAdvanced field factories
CBool, CString, CWString, CTime64, CBitfieldAdvanced field classes