Skip to content

JSON ​

Parse and stringify JSON.

Always available

No import needed. These names are in scope in every file.

JsonMode enum ​

ts
enum JsonMode

Dialect accepted by JSON.parse(text, mode).

  • JsonMode.AUTO: The default: standard JSON plus the lenient forms listed on JSON.parse.
  • JsonMode.STRICT: RFC 8259 only. Comments, single quotes, unquoted keys, trailing commas and hex numbers throw SyntaxError.
  • JsonMode.JSON5: The JSON5 spec (json5.org): AUTO's forms plus Infinity, NaN, a leading +, leading or trailing decimal points and string line continuation.

JSON static class ​

ts
static class JSON

Final: it cannot be subclassed.

JSON namespace. Static helpers for converting between TKS values and JSON text. Pair with Files.readText and Files.writeText to load and save data files on disk.

JSON.parse ​

ts
static parse(text: string, mode?: JsonMode): any

Parse text as JSON and return the resulting value.

Accepts strict JSON plus the following JSONC extensions: line comments (two slashes through end of line), block comments (slash-star to star-slash), single-quoted strings, trailing commas in arrays and objects, and unquoted JSON5 identifier keys such as { foo: 1 }. Inside strings the escape sequences \n, \r, \t, \", \', \\, and \/ are recognised; any other backslash escape passes through verbatim.

Returns undefined when text is empty, malformed, or not a string. Numeric literals that round-trip through 64-bit integer decode as int; every other number decodes as float. JSON objects parse as plain instances. JSON arrays parse as Array<any>.

mode picks the dialect (JsonMode.AUTO when omitted, which is the behaviour described above). JsonMode.STRICT accepts RFC 8259 only; JsonMode.JSON5 accepts the JSON5 spec. The second parameter differs from JavaScript's JSON.parse(text, reviver): there is no reviver.

The compiler rewrites JSON.parse<StructName>(text) into a second-argument call carrying the struct type ID, so user code never passes that argument directly.

Example

ts
const data: any = JSON.parse('{"name":"ada","id":7}');
console.log(data.name, data.id); // ada 7

JSON.stringify ​

ts
static stringify(value: any, replacer?: Function | Array<any> | null | undefined, space?: int | string): string

Serialize value to a JSON string.

replacer controls which properties are emitted. A function is called as (key: string, value: any): any for every property and may return a transformed value or undefined to omit it. An array whitelists object keys: string entries are matched verbatim and integer entries coerce to their decimal string form. null, undefined, or any other value disables replacement entirely.

space controls indentation. An integer is clamped to the range 0..10 and emits that many spaces per nesting level. A string is truncated to its first 10 characters and used verbatim as the indent unit. A falsy or missing space produces a compact, single-line result.

NaN and Infinity serialize as null, matching the JS spec. undefined values, function values, and entries the replacer drops are omitted from objects and serialized as null inside arrays.

Example

ts
const text: string = JSON.stringify({ name: "ada", id: 7 });
console.log(text); // {"name":"ada","id":7}

Generated from lang/types/builtin_json.d.tks in the ThicketScript repository.