rontolisp:json-stringify
(rontolisp:json-stringify value)
Serializes a Lisp value into a JSON document string, modeled on JavaScript's
JSON.stringify. Both object representations produced by
rontolisp:json-parse serialize back to JSON
objects: a property list whose keys are keywords, and a hash table (keys may
be strings, symbols, keywords or numbers).
Value mapping
| Lisp | JSON |
|---|---|
nil | null |
t | true |
| integer, float | number |
| ratio | number (converted with float) |
| string | string (quote, backslash and control characters are escaped) |
| keyword, symbol, character | string (a keyword drops its leading colon) |
| non-empty list of alternating keyword/value pairs | object |
| any other list | array |
| hash table | object (iteration order is unspecified) |
Anything else (functions, streams, promises, arrays) signals an error.
A value parsed from JSON round-trips structurally:
Limitations
- A list is serialized as an object exactly when it looks like a keyword
property list (
(:a 1 :b 2)); a genuine array whose elements alternate keywords and values is indistinguishable and becomes an object. nilalways serializes asnull— there is no way to produce an empty array[]or (from a plist) an empty object{}; build an empty hash table for the latter.- Hash-table key order in the output is backend-specific (unspecified), like
maphash. - Non-ASCII characters are emitted verbatim (never
\uXXXX-escaped), which is valid JSON. - On the WASM backends a float with magnitude 2³¹ or larger cannot be serialized (the float formatter traps); see the WASM guide.
Backend support
Works on every backend and in every WASM mode (Preview 1 included), like
rontolisp:json-parse: the serializer is written
in rontolisp itself and is compiled into the program when used.