nupp.codec.json.spi

Module contents

Types

TypeKindDescription
EncodedStringrecordA JSON string whose immutable encoded bytes have already been produced or validated.
EncodedValuerecordA complete JSON value whose immutable bytes have already been encoded or validated.
JSONEncodableinterfaceValues that can write one complete JSON value through a checked writer.
MarkContainertypeMarks a table while returning the same generic table and ownership.
ProviderinterfaceJSON API 2, including portable encoding and optional compiled serde.
WriterinterfaceA checked incremental JSON writer.

Types#

EncodedStringrecord#

record EncodedString

A JSON string whose immutable encoded bytes have already been produced or validated.

Writer:key accepts this type without validating UTF-8 or escaping it again. Values produced by encodedString and verifiedString are interned.

EncodedValuerecord#

record EncodedValue

A complete JSON value whose immutable bytes have already been encoded or validated.

Values come from encoded or verified; the private token prevents checked callers from manufacturing an unverified fast-path value.

JSONEncodableinterface#

interface JSONEncodable ...

Values that can write one complete JSON value through a checked writer.

nupp.codec.json names it for callers; it is declared here so the modules the facade reads, nupp.serde and nupp.derive among them, can name it without reading the facade. Writer below names the encoder newWriter() returns.

local record Money is nupp.codec.json.JSONEncodable
    cents: integer

    function writeJSON(self, exclusive out: nupp.codec.json.Writer): nil
        out:startObject():key("cents"):write(self.cents):endObject()
    end
end

Members

NameKindDescription
writeJSONmethodWrites this value at the writer's current value position.

writeJSONmethod#

writeJSON: function(self, exclusive out: Writer): nil

Writes this value at the writer's current value position.

Arguments
NameTypeDescription
?self
exclusive outWriter

the checked destination

Returns
TypeDescription
nil

MarkContainertype#

type MarkContainer = function<T is table>(takes value: T): T preserves value

Marks a table while returning the same generic table and ownership.

The takes/preserves relation is intentional: an implementation must not copy the container or erase an affine owner while applying its JSON marker.

Providerinterface#

interface Provider ...

JSON API 2, including portable encoding and optional compiled serde.

Arrays, objects, null sentinels, and verified fragments must agree across every member of this table. Keep marker identities stable for the loaded provider. The caller's nullValue controls null conversion; omission follows the public JSON API's nil behavior. Reject malformed syntax, invalid UTF-8, unsupported container shapes, and invalid raw fragments according to that API.

The three serde hooks are optional accelerators. If supplied together, compiled schemas and both decode entry points must share their representation and unknown-member policy. All buffer operations use the canonical shared Buffer; they must preserve the declared exclusive access and ownership guarantees.

Members

NameKindDescription
priorityfield
arrayOfmethodBuilds an array projection shape with the supplied element shape.
asArrayfieldMarks and returns the same table as an array, preserving its ownership.
asObjectfieldMarks and returns the same table as an object, preserving its ownership.
isArraymethodReports whether a value has JSON array semantics.
decodemethodParses JSON, applying the caller-selected representation of null.
encodemethodSerializes a value with this provider's markers and null convention.
encodedmethodCreates a validated encoded-value fragment for subsequent composition.
encodedStringmethodCreates a fragment containing one escaped JSON string.
pullmethodParses only the value projection described by shape.
verifiedmethodValidates raw JSON and retains a fragment suitable for composition.
verifiedStringmethodValidates a raw encoded JSON string fragment.
newWritermethodCreates a JSON writer over exclusive access to the canonical buffer.
NULLfieldStable explicit null marker owned by this loaded implementation.
EMPTY_ARRAYfieldStable empty-array marker value.
EMPTY_OBJECTfieldStable empty-object marker value.
compileSerdefieldOptionally prepares a schema plan with its unknown-member policy.
decodeSerdefieldOptionally decodes a prepared schema, returning value or diagnostic.
decodeSerdeBufferfieldOptionally decodes from exclusive access to the canonical buffer.

priorityfield#

priority: integer?
@readonly

arrayOfmethod#

arrayOf: function(shape: any?): any
@readonly

Builds an array projection shape with the supplied element shape.

Arguments
NameTypeDescription
shapeany?
Returns
TypeDescription
any

asArrayfield#

@readonly

Marks and returns the same table as an array, preserving its ownership.

asObjectfield#

@readonly

Marks and returns the same table as an object, preserving its ownership.

isArraymethod#

isArray: function(value: any): boolean
@readonly

Reports whether a value has JSON array semantics.

Arguments
NameTypeDescription
valueany
Returns
TypeDescription
boolean

decodemethod#

decode: function(text: string, nullValue: any?): any
@readonly

Parses JSON, applying the caller-selected representation of null.

Arguments
NameTypeDescription
textstring
nullValueany?
Returns
TypeDescription
any

encodemethod#

encode: function(value: any, nullValue: any?): string
@readonly

Serializes a value with this provider's markers and null convention.

Arguments
NameTypeDescription
valueany
nullValueany?
Returns
TypeDescription
string

encodedmethod#

encoded: function(value: any, nullValue: any?): any
@readonly

Creates a validated encoded-value fragment for subsequent composition.

Arguments
NameTypeDescription
valueany
nullValueany?
Returns
TypeDescription
any

encodedStringmethod#

encodedString: function(value: string): any
@readonly

Creates a fragment containing one escaped JSON string.

Arguments
NameTypeDescription
valuestring
Returns
TypeDescription
any

pullmethod#

pull: function(text: string, shape: any, nullValue: any?): any
@readonly

Parses only the value projection described by shape.

Arguments
NameTypeDescription
textstring
shapeany
nullValueany?
Returns
TypeDescription
any

verifiedmethod#

verified: function(text: string): any
@readonly

Validates raw JSON and retains a fragment suitable for composition.

Arguments
NameTypeDescription
textstring
Returns
TypeDescription
any

verifiedStringmethod#

verifiedString: function(text: string): any
@readonly

Validates a raw encoded JSON string fragment.

Arguments
NameTypeDescription
textstring
Returns
TypeDescription
any

newWritermethod#

newWriter: function(exclusive out: SharedBuffer, nullValue: any?): any
@readonly

Creates a JSON writer over exclusive access to the canonical buffer.

Arguments
NameTypeDescription
exclusive outSharedBuffer
nullValueany?
Returns
TypeDescription
any

NULLfield#

NULL: any
@readonly

Stable explicit null marker owned by this loaded implementation.

EMPTY_ARRAYfield#

@readonly

Stable empty-array marker value.

EMPTY_OBJECTfield#

@readonly

Stable empty-object marker value.

compileSerdefield#

compileSerde: (function(plan: any, unknownMembers: string): any)?
@readonly

Optionally prepares a schema plan with its unknown-member policy.

decodeSerdefield#

decodeSerde: (function(schema: any, text: string): (any?, string?))?
@readonly

Optionally decodes a prepared schema, returning value or diagnostic.

decodeSerdeBufferfield#

decodeSerdeBuffer: (function(schema: any, exclusive input: SharedBuffer): (any?, string?))?
@readonly

Optionally decodes from exclusive access to the canonical buffer.

Writerinterface#

interface Writer is nupp.Closeable ...

A checked incremental JSON writer.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:startObject():key("ok"):write(true):endObject()
writer:close()
assert(out:tostring() == [[{"ok":true}]])

Members

NameKindDescription
startArraymethodStarts an array value.
startObjectmethodStarts an object value.
keymethodSelects the next object member.
writemethodAppends one complete Lua value.
nullmethodAppends JSON null.
endArraymethodEnds the current array.
endObjectmethodEnds the current object.
flushmethodPublishes staged bytes without ending the document.
closemethodVerifies and publishes one complete root, then releases backing state.

startArraymethod#

startArray: function(exclusive self: Writer): Writer borrows (self)

Starts an array value.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:startArray():write("first"):endArray()
writer:close()
assert(out:tostring() == "[\"first\"]")
Arguments
NameTypeDescription
exclusive selfWriter

the writer

Returns
TypeDescription
Writer borrows (self)

this writer

startObjectmethod#

startObject: function(exclusive self: Writer): Writer borrows (self)

Starts an object value.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:startObject():endObject()
writer:close()
assert(out:tostring() == "{}")
Arguments
NameTypeDescription
exclusive selfWriter

the writer

Returns
TypeDescription
Writer borrows (self)

this writer

keymethod#

key: function(exclusive self: Writer, name: string): Writer borrows (self)
& function(exclusive self: Writer, name: EncodedString): Writer borrows (self)

Selects the next object member.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:startObject():key("answer"):write(42):endObject()
writer:close()
assert(out:tostring() == [[{"answer":42}]])
Arguments
NameTypeDescription
exclusive selfWriter

the writer

namestring

the next member name

Returns
TypeDescription
Writer borrows (self)

this writer

writemethod#

write: function(
    exclusive self: Writer,
    value: string | number | boolean | {any} | {[string]: any}
): Writer borrows (self)
    & function(
        exclusive self: Writer,
        value: EncodedValue | EncodedString
    ): Writer borrows (self)

Appends one complete Lua value.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:startArray():write({id = 41}):endArray()
writer:close()
assert(out:tostring() == "[{\"id\":41}]")
Arguments
NameTypeDescription
exclusive selfWriter

the writer

valuestring | number | boolean | {any} | {[string]: any}

the next JSON value

Returns
TypeDescription
Writer borrows (self)

this writer

nullmethod#

null: function(exclusive self: Writer): Writer borrows (self)

Appends JSON null.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:startArray():null():endArray()
writer:close()
assert(out:tostring() == "[null]")
Arguments
NameTypeDescription
exclusive selfWriter

the writer

Returns
TypeDescription
Writer borrows (self)

this writer

endArraymethod#

endArray: function(exclusive self: Writer): Writer borrows (self)

Ends the current array.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:startArray():startObject():endObject():endArray()
writer:close()
assert(out:tostring() == "[{}]")
Arguments
NameTypeDescription
exclusive selfWriter

the writer

Returns
TypeDescription
Writer borrows (self)

this writer

endObjectmethod#

endObject: function(exclusive self: Writer): Writer borrows (self)

Ends the current object.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:startObject():key("items"):startArray():endArray():endObject()
writer:close()
assert(out:tostring() == [[{"items":[]}]])
Arguments
NameTypeDescription
exclusive selfWriter

the writer

Returns
TypeDescription
Writer borrows (self)

this writer

flushmethod#

flush: @nosuspend function(exclusive self: Writer): nil

Publishes staged bytes without ending the document.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:startArray():write(1)
writer:flush()
assert(out:tostring() == "[1")
writer:endArray()
writer:close()
assert(out:tostring() == "[1]")
Arguments
NameTypeDescription
exclusive selfWriter

the writer

Returns
TypeDescription
nil

closemethod#

close: @nosuspend function(takes self: Writer): nil

Verifies and publishes one complete root, then releases backing state.

local out = nupp.text.newBuffer()
local writer = nupp.codec.json.newWriter(out)
writer:write({ok = true})
writer:close()
assert(out:tostring() == [[{"ok":true}]])
Arguments
NameTypeDescription
takes selfWriter

the writer

Returns
TypeDescription
nil