Bufferize
Bufferize losslessly encodes and decodes Roblox data types into
buffer
objects.
The module exposes a default encoder via the top-level encode / decode
functions, along with helpers for serializing instances and deduplicating
repeated values inside a table. For per-type encoding overrides, create a
dedicated Encoder with Bufferize.custom.
local b = Bufferize.encode("Hello world!", 123, true, { foo = "bar" })
print(Bufferize.decode(b)) -- "Hello world!", 123, true, { foo = "bar" }
Properties
VERSION
VersioningBufferize.VERSION: string
The semantic version string of the running Bufferize module. This is the
same value that gets written into the header of every buffer produced by
encode. See the versioning guide in the readme for compatibility
rules.
read
RawBufferize.read: Readers
A table of raw reader functions keyed by data type name (e.g.
Bufferize.read.CFrame). Each reader has the signature
(b: buffer, offset: number) -> (value, nextOffset) and reads a single
value written by the matching Bufferize.write function.
Unlike decode, raw readers do not expect a version header or a type id —
the caller is responsible for knowing which type is stored at each offset.
local b, offset = Bufferize.write.CFrame(buffer.create(0), 0, cf)
b, offset = Bufferize.write.Vector3(b, offset, v)
local cf2, nextOffset = Bufferize.read.CFrame(b, 0)
local v2 = Bufferize.read.Vector3(b, nextOffset)
Raw readers always use the default encoding and ignore any overrides registered on an Encoder.
write
RawBufferize.write: Writers
A table of raw writer functions keyed by data type name (e.g.
Bufferize.write.CFrame). Each writer has the signature
(b: buffer, offset: number, value) -> (buffer, nextOffset) and writes a
single value without a version header or type id.
If the value fits inside b it is written in place and b is returned.
Otherwise a larger copy of b is created and returned instead, so always
use the returned buffer for subsequent writes.
Raw writers always use the default encoding and ignore any overrides registered on an Encoder.
Functions
readVersion
VersioningBufferize.readVersion(b: buffer--
A buffer previously produced by Bufferize.encode.
) → string?--
The semantic version string written into the header, or nil if it could not be parsed.
Inspects the version that was written into a buffer's header without attempting to decode the body. Useful for migration code that needs to branch on the producing version before decoding.
isCompatible
VersioningBufferize.isCompatible(b: buffer--
A buffer previously produced by Bufferize.encode.
) → boolean--
true if the buffer's header version can be decoded by the running version of Bufferize.
Checks whether the running version of Bufferize is able to decode the given
buffer. This is the same version check decode performs, so a false result
means decode would raise an error rather than return a value.
if Bufferize.isCompatible(b) then
local value = Bufferize.decode(b)
else
-- migrate the data with an older version, or discard it
end
Only the version header is inspected — the body is not decoded, so a true
result does not guarantee the rest of the buffer is well-formed. Buffers that
were not produced by encode at all return false.
custom
Creates a new Encoder that you can configure with custom read/write
functions for individual data types via Encoder:override. The returned
encoder is independent of the module-level encode / decode functions —
you must use the same encoder for both halves of a round-trip.
See custom encoding in the readme for examples.
deduplicateTypeof
DeduplicationBufferize.deduplicateTypeof(typeOfsSet: {[string]: boolean},--
Set of type names (e.g. "string", "CFrame") to consider for deduplication.
rootTbl: {[any]: any}--
The table to deduplicate.
) → {{[any]: any}}--
A { deduplicated, root } pair to pass to encode.
Scans rootTbl for keys and values whose typeof is in typeOfsSet and
that appear more than once, and replaces each duplicate with a pointer to a
single shared entry. Use this when you don't know up front which specific
values are duplicated.
The returned table can be passed directly to Bufferize.encode. After
decoding, use Bufferize.reduplicate to expand the pointers back into the
original values.
See the deduplication guide for usage examples.
deduplicate
DeduplicationBufferize.deduplicate(supportedSet: {[any]: boolean},--
Set of exact values that should be deduplicated.
rootTbl: {[any]: any}--
The table to deduplicate.
) → {{[any]: any}}--
A { deduplicated, root } pair to pass to encode.
Replaces every occurrence of each value in supportedSet (as a key or as a
value) with a pointer into a shared array of deduplicated values. Use this
when you know exactly which values you want to collapse.
The returned table can be passed directly to Bufferize.encode. After
decoding, use Bufferize.reduplicate to expand the pointers back into the
original values.
reduplicate
DeduplicationBufferize.reduplicate(compressed: {{[any]: any}}--
A table previously produced by deduplicate / deduplicateTypeof, after a round-trip through encode / decode.
) → {[any]: any}--
The expanded table with deduplication pointers replaced by their original values.
Reverses Bufferize.deduplicate / Bufferize.deduplicateTypeof, producing a plain table you can use exactly as you would have used the input to the deduplication call.
serializeInstance
InstancesBufferize.serializeInstance() → {any}--
A plain table representation that can be passed to encode.
Walks rootInstance and all of its descendants and produces a table that
captures each instance's ClassName, non-default writable properties,
attributes, and tags. References between instances within the tree are
preserved; references to instances outside the tree are dropped.
See the instances guide for the full set of rules and examples.
deserializeInstance
InstancesBufferize.deserializeInstance(serializedArr: {any}--
A table previously produced by serializeInstance, after a round-trip through encode / decode.
) → Instance--
The reconstructed root instance, with all descendants parented underneath.
Recreates the instance tree captured by Bufferize.serializeInstance. Properties, attributes, tags, and intra-tree references are restored on the new instances.
encode
Bufferize.encode(...: Supported--
One or more values of supported data types.
) → buffer--
A buffer that round-trips through Bufferize.decode to the same values.
Encodes the given values into a buffer using the module-level default
encoder. The buffer is prefixed with the running Bufferize version so that
decode can refuse to read data produced by an incompatible version.
Tables are walked recursively; shared sub-tables are preserved as shared references in the decoded output.
decode
Bufferize.decode(b: buffer--
A buffer previously produced by Bufferize.encode.
) → ...Supported--
The original values, in the same order they were passed to encode.
Decodes a buffer produced by Bufferize.encode. Raises an error if the
buffer was encoded with an incompatible major version, or with a newer
version of Bufferize than the one currently loaded.
encodeHeaderless
VersioningBufferize.encodeHeaderless(...: Supported--
One or more values of supported data types.
) → buffer--
A buffer that round-trips through Bufferize.decodeHeaderless to the same values.
Same as Bufferize.encode, but the version header is omitted. This saves a few bytes per buffer at the cost of the version compatibility checks, so it's best suited to short-lived data (e.g. networking) where both ends are guaranteed to be running the same version of Bufferize.
decodeHeaderless
VersioningBufferize.decodeHeaderless(b: buffer--
A buffer previously produced by Bufferize.encodeHeaderless.
) → ...Supported--
The original values, in the same order they were passed to encodeHeaderless.
Decodes a buffer produced by Bufferize.encodeHeaderless. No version checks are performed, so decoding a buffer produced by an incompatible version of Bufferize may error or return incorrect values.