nupp.serde.native

The native codec saves values as a compact binary frame on a native LuaJIT host. See Serialization bindings for what a frame preserves.

local serde = require("nupp.serde")
local native = require("nupp.serde.native")
local record Save
    level: integer
    inventory: {string}
end
local saves = serde.binding(Save)
local bytes = native.encode(saves, new Save(level = 12, inventory = {"key"}))
local restored = native.decode(saves, bytes)
assert(restored.level == 12 and restored.inventory[1] == "key")

The frame keeps everything a nupp.serde.Writer can say: exact 64-bit integers, float widths, decimals, bytes, timestamps, and document boundaries. Members are stored by name, so a binding can gain optional fields and still read older frames.

A frame holds a finite tree of values. It does not preserve shared identity or cycles, never runs code from its input, and cannot hold process handles. Its input is bounded at 16 MiB, 128 levels of nesting, and a million values.

The frame is versioned and a reader rejects any version it does not know. It is specific to LuaJIT: this module raises on other hosts before it selects a binding. Use nupp.serde.json for data that leaves the machine.

Module contents

Types

TypeKindDescription
CodecinterfaceA native codec with its own cache of what it selected for each binding.

Functions

FunctionKindDescription
codecfunctionMakes a Codec.
decodefunctionDecodes a frame into a value.
decodeDocumentfunctionDecodes a frame as document contents.
encodefunctionEncodes a value as a frame.
encodeDocumentfunctionEncodes a document's contents as a frame, keeping every scalar's exact form.

Values

ValueKindDescription
identityvariableIdentifies this codec in a nupp.serde.Context it attaches.

Types#

Codecinterface#

interface Codec ...

A native codec with its own cache of what it selected for each binding.

Members

NameKindDescription
encodemethodEncodes a value.
decodemethodDecodes a value.
encodeDocumentmethodEncodes a document's contents.
decodeDocumentmethodDecodes document contents.
clearmethodForgets every binding's selections.
cachedBindingsmethodReturns how many bindings have selections retained.

encodemethod#

encode: function<T>(self, binding: core.Binding<T>, borrows value: T): string

Encodes a value. See encode.

Arguments
NameTypeDescription
?self
bindingcore.Binding<T>
borrows valueT
Returns
TypeDescription
string

decodemethod#

decode: function<T>(self, binding: core.Binding<T>, bytes: string): T

Decodes a value. See decode.

Arguments
NameTypeDescription
?self
bindingcore.Binding<T>
bytesstring
Returns
TypeDescription
T

encodeDocumentmethod#

encodeDocument: function<D>(self, adapter: core.DocumentAdapter<D>, borrows value: D): string

Encodes a document's contents. See encodeDocument.

Arguments
NameTypeDescription
?self
adaptercore.DocumentAdapter<D>
borrows valueD
Returns
TypeDescription
string

decodeDocumentmethod#

decodeDocument: function<D>(self, adapter: core.DocumentAdapter<D>, bytes: string): D

Decodes document contents. See decodeDocument.

Arguments
NameTypeDescription
?self
adaptercore.DocumentAdapter<D>
bytesstring
Returns
TypeDescription
D

clearmethod#

clear: function(self): nil

Forgets every binding's selections.

Arguments
NameTypeDescription
?self
Returns
TypeDescription
nil

cachedBindingsmethod#

cachedBindings: function(self): integer

Returns how many bindings have selections retained.

Arguments
NameTypeDescription
?self
Returns
TypeDescription
integer

Functions#

codecfunction#

function codec(capacity: integer?): Codec

Makes a Codec.

Arguments

NameTypeDescription
capacityinteger?

the most bindings to retain selections for; nil is 256

Returns

TypeDescription
Codec

the codec

Raises

TypeCondition
string

when the host is not native LuaJIT, or capacity is not a positive integer

decodefunction#

function decode<T>(binding: core.Binding<T>, bytes: string): T

Decodes a frame into a value.

Type parameters

NameDescription
T

Arguments

NameTypeDescription
bindingcore.Binding<T>

the binding to construct through

bytesstring

the frame

Returns

TypeDescription
T

the value

Raises

TypeCondition
nupp.serde.Error

when the frame is malformed, of an unknown version, or does not fit the binding

decodeDocumentfunction#

function decodeDocument<D>(adapter: core.DocumentAdapter<D>, bytes: string): D

Decodes a frame as document contents.

Type parameters

NameDescription
D

Arguments

NameTypeDescription
adaptercore.DocumentAdapter<D>

the document adapter, such as nupp.serde.document.adapter

bytesstring

the frame

Returns

TypeDescription
D

the document

Raises

TypeCondition
nupp.serde.Error

when the frame is malformed or of an unknown version

encodefunction#

function encode<T>(binding: core.Binding<T>, borrows value: T): string

Encodes a value as a frame.

Type parameters

NameDescription
T

Arguments

NameTypeDescription
bindingcore.Binding<T>

the value's binding

borrows valueT

the value

Returns

TypeDescription
string

the frame

Raises

TypeCondition
nupp.serde.Error

when a value cannot be represented, at its path

encodeDocumentfunction#

function encodeDocument<D>(adapter: core.DocumentAdapter<D>, borrows value: D): string

Encodes a document's contents as a frame, keeping every scalar's exact form.

local document = require("nupp.serde.document")
local native = require("nupp.serde.native")
local value = document.list({document.numberToken("1e400"), document.bytes("\0\1")})
local restored = native.decodeDocument(document.adapter(), native.encodeDocument(document.adapter(), value))
assert(restored:at(1):numberToken() == "1e400" and restored:at(2):bytes() == "\0\1")

Type parameters

NameDescription
D

Arguments

NameTypeDescription
adaptercore.DocumentAdapter<D>

the document adapter, such as nupp.serde.document.adapter

borrows valueD

the document

Returns

TypeDescription
string

the frame

Values#

identityvariable#

Identifies this codec in a nupp.serde.Context it attaches.