Skip to main content

Serialiser

API for serialising and deserialising schema-based data structures to and from Luau buffers.

Given a schema definition, the Serialiser API can automatically pack data structures into a compact binary format suitable for network transmission or storage, and unpack them back into usable data structures.

The API will error on any of the functions if the provided schema configuration is invalid, or if the data provided does not conform to the schema.

Functions

Serialiser.writeInPlace

Writes data into a buffer using the provided schema.

To determine the size of the buffer needed, you can use the Serialiser.measure function before calling this one.

Serialiser.writeInPlace(buffer: buffer, schema: Schema, data: any, bitOffset?: number): number

Parameters

NameTypeDefault ValueDescription
bufferbufferRequiredThe buffer to write the data into.
schemaSchemaRequiredThe schema defining the structure of the data.
dataanyRequiredThe data to be serialised according to the schema.
bitOffsetnumber?0The bit offset in the buffer to start writing at.

Returns

NameTypeDescription
newBitOffsetnumberThe new bit offset in the buffer after writing the data.

Serialiser.write

Writes data into a new buffer using the provided schema and returns the buffer.

Serialiser.write(data: any, schema: Schema): buffer

Parameters

NameTypeDescription
dataanyThe data to be serialised according to the schema.
schemaSchemaThe schema defining the structure of the data.

Returns

NameTypeDescription
bufferbufferThe buffer containing the serialised data.

Serialiser.read

Reads data from a buffer according to the given schema.

Serialiser.read(buffer: buffer, schema: Schema, bitOffset?: number): any

Parameters

NameTypeDefault ValueDescription
bufferbufferRequiredThe buffer to read the data from.
schemaSchemaRequiredThe schema defining the structure of the data.
bitOffsetnumber?0The bit offset in the buffer to start reading from.

Returns

NameTypeDescription
dataanyThe data read from the buffer.

Serialiser.measure

Measures the number of bits and bytes required to serialise the data using the provided schema.

Useful for determining the size of the buffer needed before performing the actual serialisation with Serialiser.write.

Changing bitOffset can also affect the measurement, as some schema types add padding to align to the nearest byte.

Serialiser.measure(data: any, schema: Schema, bitOffset?: number): number, number

Parameters

NameTypeDefault ValueDescription
dataanyRequiredThe data to be measured.
schemaSchemaRequiredThe schema defining the structure of the data.
bitOffsetnumber?0The bit offset to start measuring from.

Returns

NameTypeDescription
bitsNeedednumberThe number of bits needed to serialise the data.
bytesNeedednumberThe number of bytes needed to serialise the data.

Serialiser.int

Creates an integer schema with optional min and max values. The range between min and max defines the number of bits needed to serialise the integer.

Serialiser.int(config?: IntSchemaConfig): Schema

Parameters

NameTypeDescription
configIntSchemaConfig?The configuration for the integer schema.

Returns

NameTypeDescription
schemaSchemaThe integer schema.

Serialiser.bool

Creates a boolean schema.

Serialiser.bool(): Schema

Returns

NameTypeDescription
schemaSchemaThe boolean schema.

Serialiser.string

Creates a string schema with an optional maximum length.

Serialiser.string(config?: StringSchemaConfig): Schema

Parameters

NameTypeDescription
configStringSchemaConfig?The configuration for the string schema.

Returns

NameTypeDescription
schemaSchemaThe string schema.

Serialiser.float

Creates a 32-bit floating-point number schema.

Float serialisation uses IEEE 754 format and requires byte-alignment (padding bits are automatically added if necessary).

Serialiser.float(): Schema

Returns

NameTypeDescription
schemaSchemaThe float schema.

Serialiser.double

Creates a 64-bit floating-point number schema.

Double serialisation uses IEEE 754 format and requires byte-alignment (padding bits are automatically added if necessary).

Serialiser.double(): Schema

Returns

NameTypeDescription
schemaSchemaThe double schema.

Serialiser.quantizedFloat

Creates a quantized floating-point number schema.

Quantized floats map a continuous range to discrete integer steps, providing a space-efficient way to represent floating-point values with controlled precision. This is useful for network transmission where exact float precision is not required.

Serialiser.quantizedFloat(config: QuantizedFloatSchemaConfig): Schema

Parameters

NameTypeDescription
configQuantizedFloatSchemaConfigThe configuration for the quantized float schema.

Returns

NameTypeDescription
schemaSchemaThe quantized float schema.

Serialiser.vector3

Creates a Vector3 schema with configurable component serialisation.

Serialiser.vector3(config?: Vector3SchemaConfig): Schema

Parameters

NameTypeDescription
configVector3SchemaConfig?The configuration for the Vector3 schema.

Returns

NameTypeDescription
schemaSchemaThe Vector3 schema.

Serialiser.array

Creates an array schema with either a maximum variable length (given by config.maxLength) or a fixed length (given by config.fixedLength). You must specify either maxLength or fixedLength, but not both.

Variable-length arrays require a few more bits to store the length, but allow for any number of elements up to the max.

Serialiser.array(config: ArraySchemaConfig): Schema

Parameters

NameTypeDescription
configArraySchemaConfigThe configuration for the array schema.

Returns

NameTypeDescription
schemaSchemaThe array schema.

Serialiser.table

Creates an empty table schema.

Use Serialiser.withField to add fields to the table.

Serialiser.table(): Schema

Returns

NameTypeDescription
schemaSchemaThe table schema.

Serialiser.withField

Adds a field to a table schema, mutating it in the process.

Fields are serialised and deserialised in the order they are added.

Serialiser.withField(schema: Schema, fieldName: string, fieldSchema: Schema): Schema

Parameters

NameTypeDescription
schemaSchemaThe table schema to add the field to.
fieldNamestringThe name of the field.
fieldSchemaSchemaThe schema for the field value.

Returns

NameTypeDescription
schemaSchemaThe table schema that was passed in (for method chaining).

Types

IntSchemaConfig

Schema configuration for serialising integers up to 32 bits in size.

Integers are encoded as a range to handle both signed and unsigned integers efficiently. The min and max properties define the inclusive range of values that can be represented.

The default range can represent any 32-bit signed integer.

type IntSchemaConfig = { .min: number?, .max: number? }

Properties

FieldTypeDefault ValueDescription
minnumber?-2147483648The minimum integer value (inclusive) that can be represented.
maxnumber?2147483647The maximum integer value (inclusive) that can be represented.

StringSchemaConfig

Schema configuration for serialising strings.

Strings are encoded as a length prefix followed by individual character bytes (0-255).

type StringSchemaConfig = { .maxLength: number? }

Properties

FieldTypeDefault ValueDescription
maxLengthnumber?65535The maximum length of the string.

QuantizedFloatSchemaConfig

Schema configuration for serialising quantized floating-point numbers.

Quantized floats provide a space-efficient way to represent floating-point values by mapping a continuous range [min, max] to a discrete number of integer steps.

The precision and size is determined by the number of steps: more steps = higher precision but more bits required.

type QuantizedFloatSchemaConfig = { .min: number, .max: number, .steps: number? }

Properties

FieldTypeDefault ValueDescription
minnumberRequiredThe minimum value (inclusive) that can be represented.
maxnumberRequiredThe maximum value (inclusive) that can be represented.
stepsnumber?65535The number of discrete steps to divide the range into.

Vector3SchemaConfig

Schema configuration for serialising Vector3 values.

By default, each component (x, y, z) is serialised as a 32-bit float. You can customize this by providing a different componentSchema (e.g., quantized floats for reduced bandwidth).

type Vector3SchemaConfig = { .componentSchema: Schema? }

Properties

FieldTypeDefault ValueDescription
componentSchemaSchema?Serialiser.float()The schema to use for each component (x, y, z).

ArraySchemaConfig

Schema configuration for serialising arrays.

Arrays can be either variable-length or fixed-length:

  • Variable-length arrays (defined by maxLength) store the actual length in the buffer, allowing arrays of different sizes up to maxLength.
  • Fixed-length arrays (defined by fixedLength) do not store length in the buffer, but require the array to always have fixedLength elements.

You must provide either maxLength or fixedLength, but not both.

type ArraySchemaConfig = { .maxLength: number?, .fixedLength: number?, .elementSchema: Schema }

Properties

FieldTypeDescription
maxLengthnumber?The maximum number of elements in a variable-length array. Cannot be used with fixedLength.
fixedLengthnumber?The exact number of elements in a fixed-length array. Cannot be used with maxLength.
elementSchemaSchemaThe schema for each element in the array.