Google FlatBuffers zero-copy binary serialization for PascalAI. Serialize and deserialize structured data with no parsing overhead — read fields directly from the buffer. Ideal for game state, IPC, networking, and mobile where allocation and parsing latency matter.
uses flatbuffers;
var b := NewBuilder(256);
{ Create strings and vectors first (bottom-up order) }var nameOff := CreateString(b, 'Alice');
var tagsOff := CreateStringVector(b, ['admin', 'user']);
var scoresOff := CreateFloatVector(b, [98.5, 87.0, 92.3]);
{ Build the table }StartTable(b, 4);
AddInt32(b, 0, 42); { field 0: id }AddOffset(b, 1, nameOff); { field 1: name }AddBool(b, 2, True); { field 2: active }AddOffset(b, 3, tagsOff); { field 3: tags }var root := EndTable(b);
Finish(b, root);
var buf := GetBuffer(b);
FreeBuilder(b);
Reading without parsing
uses flatbuffers;
{ Verify before reading (recommended) }if notVerifyBuffer(buf) thenraise'Invalid FlatBuffer';
var t := GetRoot(buf);
{ Zero-copy field access — no allocation }var id := ReadInt32(t, 0, 0);
var name := ReadString(t, 1);
var active := ReadBool(t, 2, False);
var tags := ReadVector(t, 3);
for var i := 0toVectorLen(tags) - 1doWriteLn(VectorString(tags, i));
Builder API
Function
Signature
Description
NewBuilder
NewBuilder(initialSize: Integer): TBuilder
Allocate a new FlatBuffers builder with given initial buffer size in bytes.
FreeBuilder
FreeBuilder(b: TBuilder)
Release all memory held by the builder.
ResetBuilder
ResetBuilder(b: TBuilder)
Clear the builder state so it can be reused without a new allocation.
String & Vector Creation
Bottom-up order: strings and vectors must be created before the table that references them. The builder works from the end of the buffer toward the beginning.
Function
Signature
Description
CreateString
CreateString(b: TBuilder; s: string): TOffset
Serialize a UTF-8 string into the buffer. Returns an offset for use with AddOffset.
CreateIntVector
CreateIntVector(b: TBuilder; v: array of Integer): TOffset
Serialize an integer array.
CreateFloatVector
CreateFloatVector(b: TBuilder; v: array of Double): TOffset
Serialize a float/double array.
CreateStringVector
CreateStringVector(b: TBuilder; v: array of string): TOffset
Read a vector field handle for further element access.
Vector Reading
Function
Signature
Description
VectorLen
VectorLen(v: TVector): Integer
Return the number of elements in the vector.
VectorInt32
VectorInt32(v: TVector; index: Integer): Integer
Read a 32-bit integer element at index.
VectorFloat64
VectorFloat64(v: TVector; index: Integer): Double
Read a double element at index.
VectorString
VectorString(v: TVector; index: Integer): string
Read a string element at index.
VectorTable
VectorTable(v: TVector; index: Integer): TTable
Read a nested table element at index.
VectorToIntArray
VectorToIntArray(v: TVector): array of Integer
Convert the entire integer vector to a Pascal array.
VectorToFloatArray
VectorToFloatArray(v: TVector): array of Double
Convert the entire float vector to a Pascal array.
Key Concepts
Zero-copy access
Unlike JSON or Protocol Buffers, FlatBuffers requires no parse step and no allocation when reading. Fields are accessed by computing a byte offset into the original buffer. The data never needs to be copied or transformed — your code reads directly from the wire bytes.
Bottom-up building
The builder constructs the buffer from end to start. Nested objects (strings, vectors, inner tables) must be fully written before the parent table that references them. Call CreateString and CreateFloatVector first, then StartTable / AddOffset / EndTable.
Field indices
Fields are identified by a 0-based integer index, not by name. The index is stable across schema versions — absent fields are represented by a default value, never an error. This allows schema evolution without breaking existing serialized data.