A binding selects how a value is read and constructed, separately from the codec that chooses its wire representation. See Serialization bindings for the guide to bindings, codecs, and documents.
Types
Accessrecord
A declaration's fields and constructor, as an external model sees them.
access returns one for a model library that maps a Nupp record onto its own schema, so it reads fields and constructs values exactly as binding does without depending on that binding.
Type parameters
Members
| Name | Kind | Description |
|---|
info | field | The reflection graph the declaration was read from. |
typeIndex | field | The position of the declaration in info.types. |
fields | field | The declaration's stored, readable fields, in declaration order. |
parameters | field | The constructor's parameters, in call order. |
owner | field | |
target | field | |
defaults | field | |
constructor | constructor | |
constructionProblem | field | |
accessible | field | |
nestedCache | field | |
get | method | Reads a field of a value, borrowed from it. |
begin | method | Starts constructing a value. |
constructionError | method | Returns why the declaration cannot be constructed through this access. |
nested | method | Returns the access for a record or struct the declaration's storage reaches. |
finish | method | Constructs the value from a builder and drops the builder. |
infofield
@readonly
The reflection graph the declaration was read from.
typeIndexfield
@readonly
The position of the declaration in info.types.
fieldsfield
@readonly
The declaration's stored, readable fields, in declaration order.
parametersfield
@readonly
The constructor's parameters, in call order.
The list is empty when the declaration owns resources, is generic, or needs a factory; constructionError then says why.
ownerfield
@private@readonly
targetfield
@private@readonly
defaultsfield
@private@readonly
constructorconstructor
@private@readonly
constructionProblemfield
constructionProblem: string?
@private@readonly
accessiblefield
accessible: {[integer]: boolean}
@private@readonly
nestedCachefield
@private@readonly
getmethod
get: function get(self, borrows value: T, field: const Field): unknown
Reads a field of a value, borrowed from it.
Arguments
| Name | Type | Description |
|---|
self | any | |
borrows value | T | the value to read |
field | const Field | one of this access's fields |
Returns
| Type | Description |
|---|
unknown | the field's value |
Raises
| Type | Condition |
|---|
string | when the field belongs to another access |
beginmethod
begin: function begin(self): Builder<T>
Starts constructing a value.
The builder is an owner: pass it to finish, or drop it to discard the arguments.
Arguments
| Name | Type | Description |
|---|
self | any | |
Returns
| Type | Description |
|---|
Builder<T> | the builder |
Raises
| Type | Condition |
|---|
string | when the declaration cannot be constructed; the message is the one constructionError returns |
constructionErrormethod
constructionError: function constructionError(self): string?
Returns why the declaration cannot be constructed through this access.
The reported problems are an owning or generic declaration, a constructor that needs a factory, a parameter that is not plain or whose type cannot be checked at runtime, and metadata built by a compiler that predates ownership attestation. Each needs a construction adapter of its own, except the last, which needs a rebuild.
Arguments
| Name | Type | Description |
|---|
self | any | |
Returns
| Type | Description |
|---|
string? | the problem, or nil when begin succeeds |
nestedmethod
Returns the access for a record or struct the declaration's storage reaches.
The access is cached, so asking twice for an index returns the same one.
Arguments
| Name | Type | Description |
|---|
self | any | |
index | integer | the nested declaration's position in info.types |
Returns
Raises
| Type | Condition |
|---|
string | when the type is not reachable through readable storage, is not a record or struct, or has no runtime declaration |
finishmethod
Constructs the value from a builder and drops the builder.
An unset optional parameter takes a copy of the field's default, if any, when the declaration has no constructor of its own, and nil when it does. The constructed value is checked against the declaration before it is returned.
Arguments
| Name | Type | Description |
|---|
self | any | |
takes builder | Builder<T> | the builder from begin, transferred |
Returns
| Type | Description |
|---|
T | the value |
Raises
| Type | Condition |
|---|
string | when the builder is closed or belongs to another access, a required parameter is unset, or an argument or the result does not fit its declared type |
Adapterinterface
Reads and writes one type of value against a model's members.
describe returns the shape a codec plans against, write emits a value through a Writer, and read constructs one from a Reader. The builders in nupp.serde.adapters cover scalars, structures, and lists.
Type parameters
Members
| Name | Kind | Description |
|---|
describe | method | Describes the value's shape for a direction. |
write | method | Writes value to writer as exactly one value. |
read | method | Constructs a value by consuming exactly one value from reader. |
describemethod
Describes the value's shape for a direction.
Arguments
Returns
writemethod
write: function(self, borrows value: T, exclusive writer: Writer<M>): nil
Writes value to writer as exactly one value.
Arguments
| Name | Type | Description |
|---|
? | self | |
borrows value | T | |
exclusive writer | Writer<M> | |
Returns
readmethod
read: function(self, exclusive reader: Reader<M>): T
Constructs a value by consuming exactly one value from reader.
Arguments
| Name | Type | Description |
|---|
? | self | |
exclusive reader | Reader<M> | |
Returns
Atominterface
A model-defined scalar a codec passes through without interpreting, such as an enum member or an XML qualified name.
Members
| Name | Kind | Description |
|---|
family | field | A table identifying the model that defined the atom, which a codec compares to recognize atoms it can render. |
snapshot | method | Copies the atom into a value a document can keep after the original is gone. |
familyfield
@readonly
A table identifying the model that defined the atom, which a codec compares to recognize atoms it can render.
snapshotmethod
Copies the atom into a value a document can keep after the original is gone.
Arguments
| Name | Type | Description |
|---|
borrows self | Atom | |
Returns
Bindinginterface
Selects how a value of type T is written and constructed.
A binding is opaque: it hides the model's Member type and is invariant in T, so a binding for one record cannot be passed where another is expected. Make one with binding, or with Bound or OwnedBound over an Adapter.
Type parameters
Members
| Name | Kind | Description |
|---|
accept | method | Passes the erased adapter and state to consumer, recovering the member type. |
acceptInto | method | Passes the erased adapter, state, and output to consumer, recovering the member type. |
acceptmethod
accept: function<S, R>(self, consumer: Consumer<T, S, R>, borrows state: S): R
Passes the erased adapter and state to consumer, recovering the member type.
Arguments
| Name | Type | Description |
|---|
? | self | |
consumer | Consumer<T, S, R> | |
borrows state | S | |
Returns
acceptIntomethod
acceptInto: function<S, O, R>(self, consumer: IntoConsumer<T, S, O, R>, borrows state: S, exclusive output: O): R
Passes the erased adapter, state, and output to consumer, recovering the member type.
Arguments
| Name | Type | Description |
|---|
? | self | |
consumer | IntoConsumer<T, S, O, R> | |
borrows state | S | |
exclusive output | O | |
Returns
Boundrecord
Erases an Adapter's member type into a Binding for a Copyable value.
A codec still has to be told how the model's members are named: see nupp.serde.json.memberNamePolicy.
local serde = require("nupp.serde")
local adapters = require("nupp.serde.adapters")
local json = require("nupp.serde.json")
local record Member is serde.Member
@readonly name: string
@readonly index: integer?
end
local title: serde.Binding<string> = new serde.Bound<string, Member>(
adapter = adapters.text(new Member(name = "title"))
)
assert(json.codec(json.memberNamePolicy):encode(title, "Dune") == '"Dune"')
Type parameters
Members
| Name | Kind | Description |
|---|
adapter | field | The adapter the binding erases. |
discard | method | Discards a value. |
accept | method | Passes adapter and state to consumer. |
acceptInto | method | Passes adapter, state, and output to consumer. |
adapterfield
@readonly
The adapter the binding erases.
discardmethod
discard: function discard(self, takes value: T): nil
Discards a value. A Copyable value carries no cleanup obligation, so only its transfer ends here.
Arguments
| Name | Type | Description |
|---|
self | any | |
takes value | T | |
Returns
acceptmethod
accept: function accept<S, R>(self, consumer: Consumer<T, S, R>, borrows state: S): R
Passes adapter and state to consumer.
Arguments
| Name | Type | Description |
|---|
self | any | |
consumer | Consumer<T, S, R> | |
borrows state | S | |
Returns
acceptIntomethod
acceptInto: function acceptInto<S, O, R>(self, consumer: IntoConsumer<T, S, O, R>, borrows state: S, exclusive output: O): R
Passes adapter, state, and output to consumer.
Arguments
| Name | Type | Description |
|---|
self | any | |
consumer | IntoConsumer<T, S, O, R> | |
borrows state | S | |
exclusive output | O | |
Returns
Childinterface
One member of a described structure, union, list, or map.
Type parameters
Members
| Name | Kind | Description |
|---|
member | field | The member the child's value sits at. |
required | field | Whether reading needs the member present. |
hasDefault | field | Whether construction supplies a value when the member is absent. |
describe | method | Describes the child's value for a direction. |
memberfield
@readonly
The member the child's value sits at.
requiredfield
@readonly
Whether reading needs the member present.
hasDefaultfield
@readonly
Whether construction supplies a value when the member is absent.
describemethod
Describes the child's value for a direction.
Arguments
Returns
Consumerinterface
interface Consumer<T, S, R> ...
Applies an adapter to state, recovering the member type a Binding erased. A codec passes one to Binding:accept.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Applies the adapter a binding erased to borrowed state. |
applymethod
Applies the adapter a binding erased to borrowed state.
Arguments
| Name | Type | Description |
|---|
? | self | |
adapter | Adapter<T, M> | |
borrows state | S | |
Returns
Contextinterface
Typed values a codec attaches to what it reads, such as the schema a document came from. Keys are nupp.util.Keys; a context returns its own value for a key, then its parent's.
Members
| Name | Kind | Description |
|---|
codec | field | The identity of the codec that read the value. |
profile | field | The codec configuration the value was read under. |
parent | field | The context this one extends, or nil. |
get | method | Returns the value held under a key, falling back to the parent's. |
codecfield
@readonly
The identity of the codec that read the value.
profilefield
@readonly
The codec configuration the value was read under. A JSON writer replays a captured value only under the same profile.
parentfield
@readonly
The context this one extends, or nil.
getmethod
Returns the value held under a key, falling back to the parent's.
Arguments
| Name | Type | Description |
|---|
? | self | |
key | store.Key<V> | |
Returns
Decimalrecord
An exact decimal: coefficient times ten to the exponent.
Members
| Name | Kind | Description |
|---|
coefficient | field | The digits as decimal integer text, with a leading - when negative. |
exponent | field | The power of ten the coefficient is scaled by. |
coefficientfield
@readonly
The digits as decimal integer text, with a leading - when negative.
exponentfield
@readonly
The power of ten the coefficient is scaled by.
Descriptioninterface
The shape of an adapter's value for one direction, which a codec plans against before it reads or writes anything.
Type parameters
Members
| Name | Kind | Description |
|---|
kind | field | The value's broad shape. |
member | field | The member the value sits at. |
nullable | field | Whether the value may be an explicit null. |
width | field | The width in bits of an integer or float, or nil for other kinds. |
signed | field | Whether an integer is signed, or nil for other kinds. |
canRead | field | Whether the adapter can construct a value when reading. |
children | field | The members of a structure or union, a list's one element, a tuple's slots in order, or a map's key and then its value. |
kindfield
@readonly
The value's broad shape.
memberfield
@readonly
The member the value sits at.
nullablefield
@readonly
Whether the value may be an explicit null.
widthfield
@readonly
The width in bits of an integer or float, or nil for other kinds.
signedfield
@readonly
Whether an integer is signed, or nil for other kinds.
canReadfield
@readonly
Whether the adapter can construct a value when reading. A codec refuses to plan a read of a value that cannot be constructed.
childrenfield
children: const{Child<M>}
@readonly
The members of a structure or union, a list's one element, a tuple's slots in order, or a map's key and then its value.
Directiontype
type Direction = "write" | "read"
Whether a description is for writing or for reading.
Discardinterface
Disposes a value a codec has to give up.
Type parameters
Members
| Name | Kind | Description |
|---|
discard | method | Drops a value the codec cannot hand back, such as one an adapter built before a later check on the input failed. |
discardmethod
discard: function(self, takes value: T): nil
Drops a value the codec cannot hand back, such as one an adapter built before a later check on the input failed.
Arguments
| Name | Type | Description |
|---|
? | self | |
takes value | T | |
Returns
DocumentAdapterinterface
interface DocumentAdapter<D> is Discard<D> ...
Reads and writes a document type as its logical contents. See nupp.serde.document.
Type parameters
Members
| Name | Kind | Description |
|---|
context | method | Returns the context a document carries, or nil when it has none. |
contents | method | Returns the binding that writes a document's contents as an ordinary value. |
readContents | method | Constructs a document by reading its contents from reader. |
contextmethod
Returns the context a document carries, or nil when it has none.
Arguments
| Name | Type | Description |
|---|
? | self | |
borrows document | D | |
Returns
contentsmethod
contents: function(self): Binding<D>
Returns the binding that writes a document's contents as an ordinary value.
Arguments
Returns
readContentsmethod
Constructs a document by reading its contents from reader.
Arguments
Returns
DocumentElementinterface
interface DocumentElement<S> ...
Receives one document list element.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Reads the element at a 1-based index into state, consuming value. |
applymethod
Reads the element at a 1-based index into state, consuming value.
Arguments
| Name | Type | Description |
|---|
? | self | |
exclusive state | S | |
index | integer | |
exclusive value | DocumentReader | |
Returns
DocumentEntryinterface
interface DocumentEntry<S> ...
Receives one document map entry.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Reads one entry into state, consuming both key and value. |
applymethod
Reads one entry into state, consuming both key and value.
Arguments
Returns
DocumentMemberinterface
interface DocumentMember<S> ...
Receives one document structure member.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Reads the member called name into state, consuming value. |
applymethod
Reads the member called name into state, consuming value.
Arguments
| Name | Type | Description |
|---|
? | self | |
exclusive state | S | |
name | string | |
exclusive value | DocumentReader | |
Returns
DocumentReaderinterface
interface DocumentReader ...
Reads one document value without a schema. See nupp.serde.document.
Members
| Name | Kind | Description |
|---|
context | method | Returns the context the value came with, or nil. |
kind | method | Returns the kind of the value without consuming it. |
readNull | method | Reads an explicit null. |
readBoolean | method | Reads a boolean. |
readNumberToken | method | Reads a number as its unparsed text. |
readString | method | Reads a string. |
readBytes | method | Reads a byte string. |
readTimestamp | method | Reads a timestamp. |
readInteger | method | Reads an integer with the width it was written at. |
readDecimal | method | Reads an exact decimal. |
readFloat | method | Reads a floating-point value with the width it was written at. |
readBigInteger | method | Reads an integer of any size as decimal text. |
readAtom | method | Reads a model-defined atom. |
readDocument | method | Reads a nested document through adapter. |
readList | method | Reads a list, passing each element to consume in order. |
readTuple | method | Reads a tuple, passing each slot to consume in order. |
readStruct | method | Reads a structure, passing each member to consume with its name. |
readUnion | method | Reads a union, passing its one alternative to consume with its name. |
readMap | method | Reads a map, passing each entry's key and value to consume. |
contextmethod
Returns the context the value came with, or nil.
Arguments
Returns
kindmethod
kind: function(self): Kind
Returns the kind of the value without consuming it.
Arguments
Returns
readNullmethod
Reads an explicit null.
Arguments
Returns
readBooleanmethod
Reads a boolean.
Arguments
Returns
readNumberTokenmethod
Reads a number as its unparsed text.
Arguments
Returns
readStringmethod
Reads a string.
Arguments
Returns
readBytesmethod
Reads a byte string.
Arguments
Returns
readTimestampmethod
Reads a timestamp.
Arguments
Returns
readIntegermethod
Reads an integer with the width it was written at.
Arguments
Returns
readDecimalmethod
Reads an exact decimal.
Arguments
Returns
readFloatmethod
Reads a floating-point value with the width it was written at.
Arguments
Returns
readBigIntegermethod
Reads an integer of any size as decimal text.
Arguments
Returns
readAtommethod
Reads a model-defined atom.
Arguments
Returns
readDocumentmethod
Reads a nested document through adapter.
Arguments
Returns
readListmethod
Reads a list, passing each element to consume in order.
Arguments
Returns
readTuplemethod
Reads a tuple, passing each slot to consume in order.
Arguments
Returns
readStructmethod
Reads a structure, passing each member to consume with its name.
Arguments
Returns
readUnionmethod
Reads a union, passing its one alternative to consume with its name.
Arguments
Returns
readMapmethod
Reads a map, passing each entry's key and value to consume.
Arguments
Returns
ElementConsumerinterface
interface ElementConsumer<M is Member, S> ...
Receives one list or tuple element while reading.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Reads the element at a 1-based index into state, consuming exactly one value from reader. |
applymethod
apply: function(self, exclusive state: S, index: integer, exclusive reader: Reader<M>): nil
Reads the element at a 1-based index into state, consuming exactly one value from reader.
Arguments
| Name | Type | Description |
|---|
? | self | |
exclusive state | S | |
index | integer | |
exclusive reader | Reader<M> | |
Returns
EntryConsumerinterface
interface EntryConsumer<M is Member, S> ...
Receives one map entry while reading.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Reads one entry into state, consuming one value from each of key and value. |
applymethod
Reads one entry into state, consuming one value from each of key and value.
Arguments
| Name | Type | Description |
|---|
? | self | |
exclusive state | S | |
exclusive key | Reader<M> | |
exclusive value | Reader<M> | |
Returns
Errorrecord
A serialization failure.
path is the logical path to the value, such as $["users"][2]["name"]; byte is the input offset when a decoder knows it. cause is the original problem, and suppressed collects failures raised while cleaning up after it.
local serde = require("nupp.serde")
local json = require("nupp.serde.json")
local record User
id: integer
end
local ok, problem = pcall(json.decode, serde.binding(User), '{"id":"seven"}')
assert(not ok)
local failure = problem as serde.Error
assert(failure.path == '$["id"]')
Members
| Name | Kind | Description |
|---|
code | field | The kind of failure. |
message | field | A description of the failure. |
path | field | The logical path to the value, such as $["users"][2]["name"]. |
byte | field | The input byte offset, when a decoder knows it. |
cause | field | The original problem, such as the value an adapter raised. |
suppressed | field | Failures raised while handling this one, such as a discard that failed after it. |
__tostring | metamethod | Renders the code, the path, the byte offset when known, and the message, as in model $["id"] at byte 7: expected an... |
codefield
@readonly
The kind of failure.
messagefield
@readonly
A description of the failure.
pathfield
@readonly
The logical path to the value, such as $["users"][2]["name"].
bytefield
@readonly
The input byte offset, when a decoder knows it.
causefield
@readonly
The original problem, such as the value an adapter raised.
suppressedfield
@readonly
Failures raised while handling this one, such as a discard that failed after it.
__tostringmetamethod
__tostring: function(self): string
Renders the code, the path, the byte offset when known, and the message, as in model $["id"] at byte 7: expected an integer.
Arguments
Returns
ErrorCodetype
type ErrorCode = "syntax" | "limit" | "unsupported" | "model" | "contract"
What kind of failure an Error is: malformed input, an exceeded limit, a value the codec cannot represent, a model's own failure, or a broken adapter contract.
FieldOptionsrecord
Mapping for one field of a declaration binding.
name replaces the wire name, overriding Options's naming. omitEmpty leaves an empty field out instead of writing it: nil, false, "", or an empty table. representation reinterprets the field's carrier; iso8601Seconds is the one supplied.
Members
| Name | Kind | Description |
|---|
omitEmpty | field | Whether encoding leaves the field out when it is empty. |
representation | field | Reinterprets the field's carrier, or nil to keep it. |
name | field | The field's wire name, overriding the options' naming, or nil to derive it. |
omitEmptyfield
@readonly
Whether encoding leaves the field out when it is empty.
Empty is nil, false, the empty string, or a table with no entries. Decoding is unchanged.
representationfield
representation: "iso8601Seconds"?
@readonly
Reinterprets the field's carrier, or nil to keep it.
"iso8601Seconds" writes epoch seconds as an RFC 3339 string and reads one back. The binding raises when the field's carrier is not integer or a sized integer narrower than 64 bits.
namefield
@readonly
The field's wire name, overriding the options' naming, or nil to derive it.
FieldViewinterface
Chooses an adapter in place of the one a declared type implies.
Set as Options's view. resolve is asked once for every type at every member the binding reaches: the root, each field, list and tuple elements, map keys and values, and both an optional type and the type inside it. It returns nil to keep the default, and an adapter it returns takes precedence over a field's representation.
Members
| Name | Kind | Description |
|---|
resolve | method | Returns the adapter for a type where it sits, or nil to keep the default. |
resolvemethod
Returns the adapter for a type where it sits, or nil to keep the default.
The binding asks once for each type at each member it reaches, from the root declaration down through fields, list and tuple elements, and map keys and values. An optional or const type is asked about before the type it wraps. A returned adapter replaces the default adapter outright, a field's representation included.
Arguments
| Name | Type | Description |
|---|
? | self | |
info | nupp.reflect.Info | the reflection graph of the declaration being bound |
index | integer | the type's position in info.types |
member | M | the member the type sits at |
Returns
| Type | Description |
|---|
core.Adapter<unknown, M>? | the adapter, or nil for the default |
Floatrecord
A floating-point value with the width it was written at.
Members
| Name | Kind | Description |
|---|
value | field | The value as a Lua number. |
width | field | The width in bits the value was written at. |
valuefield
@readonly
The value as a Lua number.
widthfield
@readonly
The width in bits the value was written at.
Integerrecord
An integer of a declared width, carried as its decimal text so 64-bit values survive targets without an exact carrier.
Members
| Name | Kind | Description |
|---|
decimal | field | The value as decimal text, with a leading - when negative. |
width | field | The width in bits the value must fit. |
signed | field | Whether the value is signed; an unsigned value is never negative. |
decimalfield
@readonly
The value as decimal text, with a leading - when negative.
widthfield
@readonly
The width in bits the value must fit.
signedfield
@readonly
Whether the value is signed; an unsigned value is never negative.
IntoConsumerinterface
interface IntoConsumer<T, S, O, R> ...
A Consumer that also writes into an exclusive output.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Applies the adapter a binding erased to borrowed state, writing into output. |
applymethod
Applies the adapter a binding erased to borrowed state, writing into output.
Arguments
| Name | Type | Description |
|---|
? | self | |
adapter | Adapter<T, M> | |
borrows state | S | |
exclusive output | O | |
Returns
Kindtype
type Kind = ScalarKind | "dynamic" | "opaque" | "structure" | "union" | "list" | "tuple" | "map"
The broad shape of a value: a scalar kind, or a structure, union, list, tuple, map, or opaque value.
Memberinterface
A field, list element, or other position in a model's schema.
A model library implements this with whatever its schema needs, such as a shape identifier or numbered binary field, and its codec policy interprets it. Codecs see only name and, for positional formats, index.
Members
| Name | Kind | Description |
|---|
name | field | The member's logical name, which a codec's policy maps to a wire name. |
index | field | The member's position, for a format that numbers its fields, or nil. |
namefield
@readonly
The member's logical name, which a codec's policy maps to a wire name.
indexfield
@readonly
The member's position, for a format that numbers its fields, or nil.
MemberConsumerinterface
interface MemberConsumer<M is Member, S> ...
Receives one structure or union member while reading.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Reads a declared member's value into state, consuming exactly one value from reader. |
applymethod
apply: function(self, exclusive state: S, member: M, exclusive reader: Reader<M>): nil
Reads a declared member's value into state, consuming exactly one value from reader.
Arguments
| Name | Type | Description |
|---|
? | self | |
exclusive state | S | |
member | M | |
exclusive reader | Reader<M> | |
Returns
Membersinterface
Emits a structure's members, a list's elements, or a map's entries.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Writes the members, elements, or entries of source to writer. |
applymethod
apply: function(self, borrows source: S, exclusive writer: Writer<M>): nil
Writes the members, elements, or entries of source to writer.
Arguments
| Name | Type | Description |
|---|
? | self | |
borrows source | S | |
exclusive writer | Writer<M> | |
Returns
Optionsrecord
Field mapping for binding.
naming and unknownMembers apply to every record the binding reaches. fields maps the root declaration's fields one by one, and when it is present it is a whitelist, so a field it does not mention is neither written nor read.
local serde = require("nupp.serde")
local json = require("nupp.serde.json")
local record Account
displayName: string
passwordHash: string?
end
local public = serde.binding(Account, new serde.Options(
naming = "snakeCase",
fields = {displayName = new serde.FieldOptions()},
unknownMembers = "ignore"
))
assert(json.encode(public, new Account(displayName = "Ada", passwordHash = "x")) == '{"display_name":"Ada"}')
assert(json.decode(public, '{"display_name":"Ada","admin":true}').passwordHash == nil)
A field left out of the whitelist must have a default for decoding to be selected; an encode-only binding can leave out required fields.
Members
| Name | Kind | Description |
|---|
fields | field | Options for the root declaration's fields, keyed by declared name. |
naming | field | How declared field names become wire names, for every record and shape the binding reaches. |
unknownMembers | field | What decoding does with a wire member no field declares, for every record and shape the binding reaches. |
view | field | Chooses adapters in place of the defaults, or nil to keep every default. |
fieldsfield
@readonly
Options for the root declaration's fields, keyed by declared name.
When present it is a whitelist: a field it does not name is neither written nor read. It reaches the root declaration only, so a nested record keeps every field. The binding copies it, and raises when it names a field the declaration does not have.
namingfield
naming: "identity" | "snakeCase" | "camelCase"
@readonly
How declared field names become wire names, for every record and shape the binding reaches.
"identity" keeps the name, "snakeCase" turns displayName into display_name, and "camelCase" turns display_name into displayName.
unknownMembersfield
unknownMembers: "reject" | "ignore"
@readonly
What decoding does with a wire member no field declares, for every record and shape the binding reaches.
"reject" raises at the member and "ignore" skips its value.
viewfield
@readonly
Chooses adapters in place of the defaults, or nil to keep every default.
OwnedBoundrecord
Erases an Adapter for a value that owns resources.
drop takes a decoded value the codec has to discard, such as one whose adapter returned without consuming its input, and drops it, so a failed decode does not leak what it built.
Type parameters
Members
| Name | Kind | Description |
|---|
adapter | field | The adapter the binding erases. |
drop | method | Drops a value the codec has to discard. |
discard | method | Discards a value by passing it to drop. |
accept | method | Passes adapter and state to consumer. |
acceptInto | method | Passes adapter, state, and output to consumer. |
adapterfield
@readonly
The adapter the binding erases.
dropmethod
drop: function(takes value: T): nil
@readonly
Drops a value the codec has to discard.
Arguments
| Name | Type | Description |
|---|
takes value | T | |
Returns
discardmethod
discard: function discard(self, takes value: T): nil
Discards a value by passing it to drop.
Arguments
| Name | Type | Description |
|---|
self | any | |
takes value | T | |
Returns
acceptmethod
accept: function accept<S, R>(self, consumer: Consumer<T, S, R>, borrows state: S): R
Passes adapter and state to consumer.
Arguments
| Name | Type | Description |
|---|
self | any | |
consumer | Consumer<T, S, R> | |
borrows state | S | |
Returns
acceptIntomethod
acceptInto: function acceptInto<S, O, R>(self, consumer: IntoConsumer<T, S, O, R>, borrows state: S, exclusive output: O): R
Passes adapter, state, and output to consumer.
Arguments
| Name | Type | Description |
|---|
self | any | |
consumer | IntoConsumer<T, S, O, R> | |
borrows state | S | |
exclusive output | O | |
Returns
Presencetype
type Presence = "absent" | "null" | "value"
Whether a member was absent, explicitly null, or present.
Readerinterface
What a codec reads a value from. Adapters call it; codecs implement it.
Type parameters
Members
| Name | Kind | Description |
|---|
context | method | Returns the context the codec attached to this read, or nil. |
isNull | method | Returns whether the next value is an explicit null, without consuming it. |
readNull | method | Reads an explicit null at a member, raising when the member's description does not admit null. |
readBoolean | method | Reads a boolean at a member. |
readInteger | method | Reads an integer at a member, raising when it does not fit width and signed. |
readFloat | method | Reads a floating-point value at a member, narrowed to width bits. |
readBigInteger | method | Reads an integer of any size at a member, as decimal text. |
readDecimal | method | Reads an exact decimal at a member. |
readNumberToken | method | Reads a number at a member as its unparsed text. |
readString | method | Reads a string at a member. |
readBytes | method | Reads a byte string at a member. |
readTimestamp | method | Reads a timestamp at a member. |
readAtom | method | Reads a model-defined atom at a member. |
readDocument | method | Reads a document at a member, through adapter. |
readStruct | method | Reads a structure at a member. |
readUnion | method | Reads a union at a member, raising unless it holds exactly one alternative. |
readList | method | Reads a list at a member, passing each element to consume in order. |
readTuple | method | Reads a tuple of size slots at a member, passing each to consume in order. |
readMap | method | Reads a map at a member, passing each entry's key and value to consume. |
containerSize | method | Returns the member, element, or entry count of the next value when the format knows it before reading, or nil. |
contextmethod
Returns the context the codec attached to this read, or nil.
Arguments
Returns
isNullmethod
isNull: function(self): boolean
Returns whether the next value is an explicit null, without consuming it.
Arguments
Returns
readNullmethod
readNull: function(exclusive self: Reader<M>, member: M): nil
Reads an explicit null at a member, raising when the member's description does not admit null.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
Returns
readBooleanmethod
readBoolean: function(exclusive self: Reader<M>, member: M): boolean
Reads a boolean at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
Returns
readIntegermethod
readInteger: function(exclusive self: Reader<M>, member: M, width: 8 | 16 | 32 | 64, signed: boolean): Integer
Reads an integer at a member, raising when it does not fit width and signed.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
width | 8 | 16 | 32 | 64 | |
signed | boolean | |
Returns
readFloatmethod
readFloat: function(exclusive self: Reader<M>, member: M, width: 32 | 64): number
Reads a floating-point value at a member, narrowed to width bits.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
width | 32 | 64 | |
Returns
readBigIntegermethod
readBigInteger: function(exclusive self: Reader<M>, member: M): string
Reads an integer of any size at a member, as decimal text.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
Returns
readDecimalmethod
Reads an exact decimal at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
Returns
readNumberTokenmethod
readNumberToken: function(exclusive self: Reader<M>, member: M): string
Reads a number at a member as its unparsed text.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
Returns
readStringmethod
readString: function(exclusive self: Reader<M>, member: M): string
Reads a string at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
Returns
readBytesmethod
readBytes: function(exclusive self: Reader<M>, member: M): string
Reads a byte string at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
Returns
readTimestampmethod
Reads a timestamp at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
Returns
readAtommethod
Reads a model-defined atom at a member. A codec that cannot represent atoms raises.
Arguments
| Name | Type | Description |
|---|
exclusive self | Reader<M> | |
member | M | |
Returns
readDocumentmethod
Reads a document at a member, through adapter.
Arguments
Returns
readStructmethod
Reads a structure at a member.
Each declared member the policy reads is passed to consume, and each wire field the model does not declare to unknown.
Arguments
Returns
readUnionmethod
Reads a union at a member, raising unless it holds exactly one alternative.
A declared alternative is passed to consume, and an undeclared one to unknown.
Arguments
Returns
readListmethod
Reads a list at a member, passing each element to consume in order.
Arguments
Returns
readTuplemethod
Reads a tuple of size slots at a member, passing each to consume in order. The codec raises when the input holds a different count.
Arguments
Returns
readMapmethod
Reads a map at a member, passing each entry's key and value to consume.
Arguments
Returns
containerSizemethod
containerSize: function(self): integer?
Returns the member, element, or entry count of the next value when the format knows it before reading, or nil.
Arguments
Returns
ScalarKindtype
type ScalarKind = "null"
| "boolean"
| "integer"
| "float"
| "bigInteger"
| "decimal"
| "numberToken"
| "string"
| "bytes"
| "timestamp"
| "atom"
| "document"
The scalar kinds a Writer and Reader carry.
Timestamprecord
An instant as whole seconds since the Unix epoch, in decimal text, and nanoseconds within that second.
Members
| Name | Kind | Description |
|---|
seconds | field | Whole seconds since the Unix epoch, as decimal integer text. |
nanos | field | Nanoseconds within the second, from 0 through 999999999. |
secondsfield
@readonly
Whole seconds since the Unix epoch, as decimal integer text.
nanosfield
@readonly
Nanoseconds within the second, from 0 through 999999999.
UnknownConsumerinterface
interface UnknownConsumer<S> ...
Receives a wire field the model does not declare.
Type parameters
Members
| Name | Kind | Description |
|---|
apply | method | Receives an undeclared field, whose value it must skip or capture through value. |
applymethod
Receives an undeclared field, whose value it must skip or capture through value.
Arguments
| Name | Type | Description |
|---|
? | self | |
exclusive state | S | |
field | F | |
exclusive value | UnknownReader | |
Returns
UnknownReaderinterface
interface UnknownReader ...
Skips or captures an undeclared wire field's value.
Members
| Name | Kind | Description |
|---|
skipValue | method | Skips the value. |
captureValue | method | Reads the value as a document through adapter. |
skipValuemethod
Skips the value.
Arguments
Returns
captureValuemethod
Reads the value as a document through adapter.
Arguments
Returns
WireFieldinterface
A wire field a decoder met that the model does not declare.
Members
| Name | Kind | Description |
|---|
diagnosticLabel | method | Returns the field's name as a diagnostic shows it. |
diagnosticLabelmethod
diagnosticLabel: function(self): string
Returns the field's name as a diagnostic shows it.
Arguments
Returns
Writerinterface
What a codec emits a value into. Adapters call it; codecs implement it.
Type parameters
Members
| Name | Kind | Description |
|---|
include | method | Returns whether the codec's policy writes a member at all. |
writeNull | method | Writes an explicit null at a member. |
writeBoolean | method | Writes a boolean at a member. |
writeInteger | method | Writes an integer at a member, raising when it does not fit its width. |
writeFloat | method | Writes a floating-point value at a member, narrowed to width bits. |
writeBigInteger | method | Writes an integer of any size, given as decimal text, at a member. |
writeDecimal | method | Writes an exact decimal at a member. |
writeNumberToken | method | Writes a number given as unparsed number text at a member. |
writeString | method | Writes a string at a member. |
writeBytes | method | Writes a byte string at a member. |
writeTimestamp | method | Writes a timestamp at a member. |
writeAtom | method | Writes a model-defined atom at a member. |
writeDocument | method | Writes a document at a member, through adapter. |
writeStruct | method | Writes a structure at a member, whose members emit writes from source. |
writeUnion | method | Writes a union at a member. |
writeList | method | Writes a list at a member, whose elements emit writes from source. |
writeTuple | method | Writes a tuple of size slots at a member, which emit writes from source. |
writeMap | method | Writes a map at a member, whose entries emit writes from source through writeEntry. |
writeEntry | method | Writes one entry of the map being written. |
writeUnknown | method | Writes an undeclared wire field, such as one captured on an earlier read, into the structure being written. |
includemethod
include: function(exclusive self: Writer<M>, member: M): boolean
Returns whether the codec's policy writes a member at all.
An adapter skips the member on false. The codec may already have written a substitute in its place, such as a redaction.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
Returns
writeNullmethod
writeNull: function(exclusive self: Writer<M>, member: M): nil
Writes an explicit null at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
Returns
writeBooleanmethod
Writes a boolean at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
value | boolean | |
Returns
writeIntegermethod
Writes an integer at a member, raising when it does not fit its width.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
borrows value | Integer | |
Returns
writeFloatmethod
writeFloat: function(exclusive self: Writer<M>, member: M, value: number, width: 32 | 64): nil
Writes a floating-point value at a member, narrowed to width bits.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
value | number | |
width | 32 | 64 | |
Returns
writeBigIntegermethod
Writes an integer of any size, given as decimal text, at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
value | string | |
Returns
writeDecimalmethod
Writes an exact decimal at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
borrows value | Decimal | |
Returns
writeNumberTokenmethod
Writes a number given as unparsed number text at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
value | string | |
Returns
writeStringmethod
Writes a string at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
value | string | |
Returns
writeBytesmethod
Writes a byte string at a member.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
value | string | |
Returns
writeTimestampmethod
Writes a timestamp at a member.
Arguments
Returns
writeAtommethod
Writes a model-defined atom at a member. A codec that cannot represent the atom raises.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
borrows value | A | |
Returns
writeDocumentmethod
Writes a document at a member, through adapter.
Arguments
Returns
writeStructmethod
Writes a structure at a member, whose members emit writes from source.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
borrows source | S | |
emit | Members<M, S> | |
Returns
writeUnionmethod
Writes a union at a member. emit writes exactly one alternative from source.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
borrows source | S | |
emit | Members<M, S> | |
Returns
writeListmethod
writeList: function<S>(
exclusive self: Writer<M>,
member: M,
size: integer?,
borrows source: S,
emit: Members<M, S>
): nil
Writes a list at a member, whose elements emit writes from source.
size is the element count, or nil when it is not known in advance. The codec raises when emit writes a different count.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
size | integer? | |
borrows source | S | |
emit | Members<M, S> | |
Returns
writeTuplemethod
writeTuple: function<S>(
exclusive self: Writer<M>,
member: M,
size: integer,
borrows source: S,
emit: Members<M, S>
): nil
Writes a tuple of size slots at a member, which emit writes from source. The codec raises when emit writes a different count.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
size | integer | |
borrows source | S | |
emit | Members<M, S> | |
Returns
writeMapmethod
writeMap: function<S>(
exclusive self: Writer<M>,
member: M,
size: integer?,
borrows source: S,
emit: Members<M, S>
): nil
Writes a map at a member, whose entries emit writes from source through writeEntry.
size is the entry count, or nil when it is not known in advance. The codec raises when emit writes a different count.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
member | M | |
size | integer? | |
borrows source | S | |
emit | Members<M, S> | |
Returns
writeEntrymethod
writeEntry: function<K, V>(
exclusive self: Writer<M>,
borrows key: K,
keyEmit: Members<M, K>,
borrows value: V,
valueEmit: Members<M, V>
): nil
Writes one entry of the map being written.
keyEmit writes exactly one value from key, and valueEmit exactly one from value. The codec raises outside a writeMap.
Arguments
| Name | Type | Description |
|---|
exclusive self | Writer<M> | |
borrows key | K | |
keyEmit | Members<M, K> | |
borrows value | V | |
valueEmit | Members<M, V> | |
Returns
writeUnknownmethod
Writes an undeclared wire field, such as one captured on an earlier read, into the structure being written.
adapter writes the field's document value. The codec raises outside a structure or union, or when the field collides with a declared member.
Arguments
Returns