▣
module · drop-in viewer
json_utils
Safe JSON decoding helpers and a WASM-safe number formatter. Wraps `Json.decode` with `pcall` so engine-provided JSON strings can't propagate parse errors into call sites. Also handles the engine convention of writing the literal string `"null"` when a JSON value is absent.
by·posted 2mo ago
What it does
json_utils
Safe JSON decoding helpers and a WASM-safe number formatter. Wraps
Json.decode with pcall so engine-provided JSON strings can't
propagate parse errors into call sites. Also handles the engine
convention of writing the literal string "null" when a JSON value is
absent.
Exports
M.decodeOrNil(jsonStr: string?) -> any— safe decode; returnsnilon empty input,"null", or parse failure.M.decodeOrEmptyTable(jsonStr: string?) -> table— safe decode; returns{}on any failure or non-table result.M.safeDecode(jsonStr: string?) -> any— historical alias fordecodeOrNil.M.formatNumber(n: number, decimals?: number) -> string— truncate a number to N decimal places using only string ops. WASM-safe.
Usage
local JsonUtils = require("@builtin::modules.json_utils")
local payload = JsonUtils.decodeOrNil(engineJsonString)
local table = JsonUtils.decodeOrEmptyTable(maybeMissing)
local label = JsonUtils.formatNumber(3.14159, 2) -- "3.14"
Notes
decodeOrNilshort-circuits onnil, empty string, and the literal string"null". The Json.decode call itself is wrapped inpcall, so malformed input is never observable to callers.decodeOrEmptyTableis intentionally strict: non-table decode results (numbers, strings, booleans, nil) collapse to{}. UsedecodeOrNilif you need to distinguish those.formatNumberusestostring+string.subso it avoids thestring.formatnumeric specifiers that don't work reliably under WASM in Luau. Passdecimals = 0to drop the fractional part entirely; a count below0reads the same as0, and a fractional count counts the whole places in it.formatNumbertruncates the mantissa of a valuetostringrenders in exponent form (1.5e-07) and keeps the exponent, so the result still reads back throughtonumber.
Discussion
Scoped to this part · feeds back into the world's score.
Sign in to post.sign in
No comments yet. Be the first.