Rat docs

JSON

Two members on the json namespace: json.parse turns a JSON string into a Rat value, json.stringify goes the other way. Numbers preserve their int / float distinction on parse (no silent promotion), and stringify accepts an optional indent argument for pretty-printed output. Both calls run identically server-side and inside browser handlers.

json.stringify on an object

Compact form, no spaces

json.stringify(value) renders any Rat value as a JSON string. Objects, arrays, strings, numbers, booleans, and null all map straight onto their JSON counterparts. Functions render as a sentinel "<fn>" string so the call never panics on lossy values.

> page
person: {first: 'Grace', last: 'Hopper'}

<p> [json.stringify(person)]
Result

{"first":"Grace","last":"Hopper"}

json.stringify on an array

Source order preserved

Array order in the output matches the source. Object key order is alphabetical on the way out — if you need source order preserved, build a deterministic keys list and write a small custom renderer.

<p> [json.stringify(nums)]
Result

[10,20,30]

json.stringify with indent

Second arg: int spaces, or a literal indent string

Passing a second argument enables pretty-printing. An integer N indents with N spaces per level; a string is used verbatim as the indent (use '\t' for tab-indented output).

<pre> [json.stringify(person, 2)]
Result
{
  "first": "Grace",
  "last": "Hopper"
}

json.parse — string to value

Result is a regular Rat value

json.parse(s) reads a JSON string and returns the corresponding Rat value. Member access, array indexing, and iteration all work on the result like any other object or array.

<p> [json.parse('{"n": 42}').n]
<p> [json.parse('[1, 2, 3]')[1]]
Result

n = 42

[1, 2, 3]1 = [json.parse('[1, 2, 3]')[1]]

Round-trip in a handler

Same call, runs in the browser too

The namespace works identically on the client side. Click the button below; the handler stringifies the current person object and logs it to the console.

<button on_click[log[json.stringify(person)]]> log person as JSON
Result

Composing with http.post

Request body is an object → auto-JSON; response is a string

http.post auto-JSON-encodes an object body, so you rarely have to call json.stringify for outbound payloads. On the inbound side, resp.body arrives as a string — call json.parse to lift it back into a value.

> server
resp: http.post('https://api.example.com/echo', {hello: 'rat'})
data: json.parse(resp.body)

> page
<p> server saw: [data.hello]

Next: Date — epoch seconds, token layouts, calendar-aware arithmetic.