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
| Type | Kind | Description |
|---|---|---|
Codec | interface | A native codec with its own cache of what it selected for each binding. |
Functions
| Function | Kind | Description |
|---|---|---|
codec | function | Makes a Codec. |
decode | function | Decodes a frame into a value. |
decodeDocument | function | Decodes a frame as document contents. |
encode | function | Encodes a value as a frame. |
encodeDocument | function | Encodes a document's contents as a frame, keeping every scalar's exact form. |
Values
| Value | Kind | Description |
|---|---|---|
identity | variable | Identifies 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
| Name | Kind | Description |
|---|---|---|
encode | method | Encodes a value. |
decode | method | Decodes a value. |
encodeDocument | method | Encodes a document's contents. |
decodeDocument | method | Decodes document contents. |
clear | method | Forgets every binding's selections. |
cachedBindings | method | Returns how many bindings have selections retained. |
encodemethod#
Encodes a value. See encode.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
binding | core.Binding<T> | |
borrows value | T |
Returns
| Type | Description |
|---|---|
string |
decodemethod#
decode: function<T>(self, binding: core.Binding<T>, bytes: string): TDecodes a value. See decode.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
binding | core.Binding<T> | |
bytes | string |
Returns
| Type | Description |
|---|---|
T |
encodeDocumentmethod#
encodeDocument: function<D>(self, adapter: core.DocumentAdapter<D>, borrows value: D): stringEncodes a document's contents. See encodeDocument.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
adapter | core.DocumentAdapter<D> | |
borrows value | D |
Returns
| Type | Description |
|---|---|
string |
decodeDocumentmethod#
decodeDocument: function<D>(self, adapter: core.DocumentAdapter<D>, bytes: string): DDecodes document contents. See decodeDocument.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
adapter | core.DocumentAdapter<D> | |
bytes | string |
Returns
| Type | Description |
|---|---|
D |
clearmethod#
clear: function(self): nilForgets every binding's selections.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
Returns
| Type | Description |
|---|---|
nil |
cachedBindingsmethod#
cachedBindings: function(self): integerReturns how many bindings have selections retained.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
Returns
| Type | Description |
|---|---|
integer |
Functions#
codecfunction#
function codec(capacity: integer?): CodecMakes a Codec.
Arguments
| Name | Type | Description |
|---|---|---|
capacity | integer? | the most bindings to retain selections for; nil is 256 |
Returns
| Type | Description |
|---|---|
Codec | the codec |
Raises
| Type | Condition |
|---|---|
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): TDecodes a frame into a value.
Type parameters
| Name | Description |
|---|---|
T |
Arguments
| Name | Type | Description |
|---|---|---|
binding | core.Binding<T> | the binding to construct through |
bytes | string | the frame |
Returns
| Type | Description |
|---|---|
T | the value |
Raises
| Type | Condition |
|---|---|
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): DDecodes a frame as document contents.
Type parameters
| Name | Description |
|---|---|
D |
Arguments
| Name | Type | Description |
|---|---|---|
adapter | core.DocumentAdapter<D> | the document adapter, such as |
bytes | string | the frame |
Returns
| Type | Description |
|---|---|
D | the document |
Raises
| Type | Condition |
|---|---|
nupp.serde.Error | when the frame is malformed or of an unknown version |
encodefunction#
function encode<T>(binding: core.Binding<T>, borrows value: T): stringEncodes a value as a frame.
Type parameters
| Name | Description |
|---|---|
T |
Arguments
| Name | Type | Description |
|---|---|---|
binding | core.Binding<T> | the value's binding |
borrows value | T | the value |
Returns
| Type | Description |
|---|---|
string | the frame |
Raises
| Type | Condition |
|---|---|
nupp.serde.Error | when a value cannot be represented, at its path |
encodeDocumentfunction#
function encodeDocument<D>(adapter: core.DocumentAdapter<D>, borrows value: D): stringEncodes 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
| Name | Description |
|---|---|
D |
Arguments
| Name | Type | Description |
|---|---|---|
adapter | core.DocumentAdapter<D> | the document adapter, such as |
borrows value | D | the document |
Returns
| Type | Description |
|---|---|
string | the frame |
Values#
identityvariable#
Identifies this codec in a nupp.serde.Context it attaches.