Module: nupp.data

Data and text#

nupp.data groups serialization, Unicode, identifiers and byte digests. Reach it directly from the global nupp namespace; the C and Rust providers remain hidden.

JSON#

encodeJSON and decodeJSON use the mature cjson implementation without exposing its module name:

null, emptyArray, arrayMt, and emptyArrayMt preserve distinctions Lua tables cannot express by themselves. The configuration methods match cjson's established semantics, but live on nupp.data. newJSON() returns an independent nupp.JSON encoder/decoder with its own settings:

JSON selects the cjson native feature. No require("cjson") is needed or recommended.

Member Result Purpose
encodeJSON(value) string Encode one Lua value.
decodeJSON(text) any Decode one JSON document.
newJSON() nupp.JSON Create an independently configured codec.
null, emptyArray sentinel values Represent JSON values that plain Lua tables cannot distinguish.
arrayMt, emptyArrayMt metatables Mark array-shaped tables explicitly.

Both nupp.data and an object returned by newJSON provide the configuration methods encodeEmptyTableAsObject, decodeArrayWithArrayMt, decodeAllowComment, encodeSparseArray, encodeMaxDepth, decodeMaxDepth, encodeNumberPrecision, encodeKeepBuffer, encodeInvalidNumbers, decodeInvalidNumbers, encodeEscapeForwardSlash, encodeSkipUnsupportedValueTypes, and encodeIndent. Omitting a method's setting reads the current value where the provider supports that form; passing a setting changes only that codec.

UTF-8#

nupp.data.utf8 treats strings and nupp.ByteView values as byte sequences. Byte offsets are 1-based here so they compose with Lua string positions. Invalid input decodes as U+FFFD while validation remains explicit.

validPrefixLength(value, maxBytes) returns the largest valid prefix no longer than the byte budget. truncate applies that operation to a string. The underlying lua-utf8 provider loads only when this namespace is selected and reached.

Member Result Position rule
length(value) integer Counts decoded scalar values, replacing malformed bytes individually.
decodeAt(value, byteOffset) integer?, integer Decodes forward from a 1-based byte offset and returns the next offset.
decodeBefore(value, byteOffset) integer?, integer Decodes the scalar ending before a 1-based byte offset.
encode(codepoint) string Encodes one Unicode scalar value.
isValid(value) boolean Validates the complete byte sequence.
validPrefixLength(value, maxBytes) integer Finds a valid prefix within a byte budget.
truncate(text, maxBytes) string Copies that valid prefix.

UUIDs#

uuid4() returns a random RFC 9562 version 4 identifier. uuid7() returns a time-ordered version 7 identifier, useful when identifier order should roughly follow creation order:

Both are lowercase canonical strings. They share one uuid feature and never expose a native UUID object.

Hashes and checksums#

Each function accepts a string or immutable nupp.ByteView.

fnv1a64 is a fast non-cryptographic identity rendered as 16 lowercase hexadecimal digits. sha256 is a cryptographic digest rendered as 64 lowercase hexadecimal digits. adler32 and crc32 return unsigned 32-bit numbers and accept a previous result for incremental checksumming. Checksums detect accidental damage; they do not authenticate data. A supplied previous checksum must itself be an unsigned 32-bit integer.

FNV-1a, Adler-32 and CRC-32 are pure generated Lua. SHA-256 is independently gated in the shared Rust provider. See automatic native selection.

Member Result Native provider
uuid4() canonical UUID string shared provider, uuid feature
uuid7() canonical UUID string shared provider, uuid feature
fnv1a64(value) 16 lowercase hex digits none
sha256(value) 64 lowercase hex digits shared provider, sha256 feature
adler32(value, previous?) unsigned 32-bit number none
crc32(value, previous?) unsigned 32-bit number none

JSON, UTF-8, UUIDs, hashes and checksums.

Submodules

ModuleDescription
nupp.data.utf8UTF-8 codepoint operations over strings and byte views.

Module contents

Constructors

ConstructorDescription
newJSONCreates an independent JSON codec with its own settings.

Functions

FunctionKindDescription
adler32functionComputes or continues an Adler-32 checksum.
crc32functionComputes or continues a CRC-32 checksum.
decodeAllowCommentfunctionSets whether the decoder accepts comments, or reads the current setting when omitted.
decodeArrayWithArrayMtfunctionSets whether decoded arrays carry arrayMt, or reads the current setting when omitted.
decodeInvalidNumbersfunctionSets whether the decoder accepts NaN and infinities, or reads the current setting when omitted.
decodeJSONfunctionDecodes one JSON document into Lua values.
decodeMaxDepthfunctionSets the maximum nested-container depth accepted by the decoder, or reads the current limit when omitted.
encodeEmptyTableAsObjectfunctionSets whether an empty table encodes as an object, or reads the current setting when omitted.
encodeEscapeForwardSlashfunctionSets whether / is escaped in JSON strings, or reads the current setting when omitted.
encodeIndentfunctionSets the indentation used for encoded containers, or reads the current indentation when omitted.
encodeInvalidNumbersfunctionConfigures encoding of NaN and infinities, or reads the current setting when omitted.
encodeJSONfunctionEncodes a Lua value as JSON text.
encodeKeepBufferfunctionSets whether the encoder reuses its internal buffer, or reads the current setting when omitted.
encodeMaxDepthfunctionSets the maximum nested-container depth accepted by the encoder, or reads the current limit when omitted.
encodeNumberPrecisionfunctionSets the significant-digit precision used to encode numbers, or reads the current precision when omitted.
encodeSkipUnsupportedValueTypesfunctionSets whether unsupported values are skipped instead of raising, or reads the current setting when omitted.
encodeSparseArrayfunctionConfigures how excessively sparse arrays are encoded, or reads the current settings when every argument is omitted.
fnv1a64functionComputes the 64-bit FNV-1a digest of bytes.
sha256functionComputes the SHA-256 digest of bytes.
uuid4functionGenerates a random version 4 UUID.
uuid7functionGenerates a time-ordered version 7 UUID.

Values

ValueKindDescription
arrayMtvariableMetatable marking a table to encode as a JSON array.
emptyArrayvariableSentinel that always encodes as an empty JSON array.
emptyArrayMtvariableMetatable marking a table to encode as an empty JSON array.
nullvariableSentinel that decodes from and encodes as JSON null.

Constructors#

newJSONconstructor#

Creates an independent JSON codec with its own settings.

local newJSON: function(): nupp.JSON

Returns

TypeDescription
nupp.JSON

the new codec

Functions#

adler32function#

Computes or continues an Adler-32 checksum.

local adler32: function(value: string | nupp.ByteView, previous: integer?): integer

Arguments

NameTypeDescription
valuestring | nupp.ByteView

the bytes to checksum

previousinteger?

a previous Adler-32 value, or 1 to start

Returns

TypeDescription
integer

the unsigned 32-bit checksum

crc32function#

Computes or continues a CRC-32 checksum.

local crc32: function(value: string | nupp.ByteView, previous: integer?): integer

Arguments

NameTypeDescription
valuestring | nupp.ByteView

the bytes to checksum

previousinteger?

a previous CRC-32 value, or 0 to start

Returns

TypeDescription
integer

the unsigned 32-bit checksum

decodeAllowCommentfunction#

Sets whether the decoder accepts comments, or reads the current setting when omitted.

local decodeAllowComment: function(setting: boolean | string?): boolean

Arguments

NameTypeDescription
settingboolean | string?

the new setting

Returns

TypeDescription
boolean

whether comments are accepted

decodeArrayWithArrayMtfunction#

Sets whether decoded arrays carry arrayMt, or reads the current setting when omitted.

local decodeArrayWithArrayMt: function(setting: boolean | string?): boolean

Arguments

NameTypeDescription
settingboolean | string?

the new setting

Returns

TypeDescription
boolean

whether decoded arrays carry arrayMt

decodeInvalidNumbersfunction#

Sets whether the decoder accepts NaN and infinities, or reads the current setting when omitted.

local decodeInvalidNumbers: function(setting: boolean | string?): boolean

Arguments

NameTypeDescription
settingboolean | string?

the new setting

Returns

TypeDescription
boolean

whether invalid numbers are accepted

decodeJSONfunction#

Decodes one JSON document into Lua values.

local decodeJSON: function(text: string): any

Arguments

NameTypeDescription
textstring

the JSON document to decode

Returns

TypeDescription
any

the decoded value

decodeMaxDepthfunction#

Sets the maximum nested-container depth accepted by the decoder, or reads the current limit when omitted.

local decodeMaxDepth: function(depth: integer?): integer

Arguments

NameTypeDescription
depthinteger?

the new maximum depth

Returns

TypeDescription
integer

the current maximum depth

encodeEmptyTableAsObjectfunction#

Sets whether an empty table encodes as an object, or reads the current setting when omitted.

local encodeEmptyTableAsObject: function(setting: boolean | string?): boolean

Arguments

NameTypeDescription
settingboolean | string?

true or "on" for {}, false or "off" for []

Returns

TypeDescription
boolean

whether empty tables encode as objects

encodeEscapeForwardSlashfunction#

Sets whether / is escaped in JSON strings, or reads the current setting when omitted.

local encodeEscapeForwardSlash: function(setting: boolean | string?): boolean

Arguments

NameTypeDescription
settingboolean | string?

the new setting

Returns

TypeDescription
boolean

whether forward slashes are escaped

encodeIndentfunction#

Sets the indentation used for encoded containers, or reads the current indentation when omitted.

local encodeIndent: function(indent: string?): string

Arguments

NameTypeDescription
indentstring?

the indentation string

Returns

TypeDescription
string

the current indentation string

encodeInvalidNumbersfunction#

Configures encoding of NaN and infinities, or reads the current setting when omitted.

local encodeInvalidNumbers: function(setting: boolean | string?): boolean | string

Arguments

NameTypeDescription
settingboolean | string?

false to reject them, true to emit them, or "null" to emit null

Returns

TypeDescription
boolean | string

the active invalid-number setting

encodeJSONfunction#

Encodes a Lua value as JSON text.

local encodeJSON: function(value: any): string

Arguments

NameTypeDescription
valueany

the value to encode

Returns

TypeDescription
string

the encoded JSON document

encodeKeepBufferfunction#

Sets whether the encoder reuses its internal buffer, or reads the current setting when omitted.

local encodeKeepBuffer: function(keep: boolean | string?): boolean

Arguments

NameTypeDescription
keepboolean | string?

the new setting

Returns

TypeDescription
boolean

whether the internal buffer is retained

encodeMaxDepthfunction#

Sets the maximum nested-container depth accepted by the encoder, or reads the current limit when omitted.

local encodeMaxDepth: function(depth: integer?): integer

Arguments

NameTypeDescription
depthinteger?

the new maximum depth

Returns

TypeDescription
integer

the current maximum depth

encodeNumberPrecisionfunction#

Sets the significant-digit precision used to encode numbers, or reads the current precision when omitted.

local encodeNumberPrecision: function(precision: integer?): integer

Arguments

NameTypeDescription
precisioninteger?

the new number of significant digits

Returns

TypeDescription
integer

the current precision

encodeSkipUnsupportedValueTypesfunction#

Sets whether unsupported values are skipped instead of raising, or reads the current setting when omitted.

local encodeSkipUnsupportedValueTypes: function(setting: boolean | string?): boolean

Arguments

NameTypeDescription
settingboolean | string?

the new setting

Returns

TypeDescription
boolean

whether unsupported values are skipped

encodeSparseArrayfunction#

Configures how excessively sparse arrays are encoded, or reads the current settings when every argument is omitted.

local encodeSparseArray: function(convert: boolean?, ratio: integer?, safe: integer?): (boolean, integer, integer)

Arguments

NameTypeDescription
convertboolean?

whether sparse arrays become JSON objects instead of raising

ratiointeger?

the maximum ratio between the highest index and item count

safeinteger?

the array size below which sparsity is always accepted

Returns

TypeDescription
boolean

whether sparse arrays are converted

integer

the current ratio limit

integer

the current safe size

fnv1a64function#

Computes the 64-bit FNV-1a digest of bytes.

local fnv1a64: function(value: string | nupp.ByteView): string

Arguments

NameTypeDescription
valuestring | nupp.ByteView

the bytes to hash

Returns

TypeDescription
string

the lowercase hexadecimal digest

sha256function#

Computes the SHA-256 digest of bytes.

local sha256: function(value: string | nupp.ByteView): string

Arguments

NameTypeDescription
valuestring | nupp.ByteView

the bytes to hash

Returns

TypeDescription
string

the lowercase hexadecimal digest

uuid4function#

Generates a random version 4 UUID.

local uuid4: function(): string

Returns

TypeDescription
string

the canonical lowercase UUID text

uuid7function#

Generates a time-ordered version 7 UUID.

local uuid7: function(): string

Returns

TypeDescription
string

the canonical lowercase UUID text

Values#

arrayMtvariable#

Metatable marking a table to encode as a JSON array.

local arrayMt: metatable<{any}>

emptyArrayvariable#

Sentinel that always encodes as an empty JSON array.

local emptyArray: any

emptyArrayMtvariable#

Metatable marking a table to encode as an empty JSON array.

local emptyArrayMt: metatable<{any}>

nullvariable#

Sentinel that decodes from and encodes as JSON null.

local null: any