Standard library / JSON · on the one-page reference
JSON
Experimental — new, manually tested rather than conformance-suite covered.
Automatic (de)serialization for Lumen's record types, so no
custom per-field (de)serialization code was needed. Deviation from
Node: JSON.parse<T> takes an explicit type
argument (there's no dynamic/any value to hand back). Invalid
input throws a catchable Error (like JS's
SyntaxError) — wrap it in try/catch.
Works for primitives, named record types, and arrays of either;
Map/Set/tuples are not supported as
T this pass (see the roadmap).
JSON.stringify(value)
T → string — T inferred from
value, the same as every other Lumen builtin. Empty string
on an encode failure.
type Person = { name: string, age: int, active: bool };
const p: Person = { name: "Ada", age: 30, active: true };
console.log(JSON.stringify(p)); // {"name":"Ada","age":30,"active":true}
JSON.parse<T>(text)
string → T — the first explicit type argument on a
namespace call in Lumen (every other generic call site is a free
function/class). Throws a catchable Error on invalid input.
type Person = { name: string, age: int, active: bool };
const p: Person = JSON.parse<Person>('{"name":"Grace","age":45,"active":false}');
console.log(p.name); // Grace
try {
const broken: Person = JSON.parse<Person>("not json");
} catch (e) {
console.log(e.message); // JSON.parse: invalid JSON (...)
}