ilusm.dev

json

Pure ilusm JSON encoder/decoder - encode any ilusm value (nil, bool, int, float, string, list, object) to a standards-compliant JSON string with proper backslash/quote/newline escaping; decode a JSON string back to the corresponding ilusm values, including deeply nested objects and arrays.

Load with: use json

What this module does

json is a zero-dependency, pure-ilusm implementation of JSON encode and decode. The encoder recurses into lists and objects, escaping ", \, \n, \r, and \t. The decoder is a recursive-descent parser that handles strings (with escape sequences), numbers (integers and floats), booleans (true/false), null, arrays, and objects, skipping whitespace between tokens.

Both encoder and decoder are exposed under the json namespace as json.enc/json.encode and json.dec/json.decode.

Quick example

use json

# Encode
s = json.enc({name: "Alice", scores: [10, 20, 30], active: tru})
# '{"name":"Alice","scores":[10,20,30],"active":true}'

# Decode
obj = json.dec(s)
prn(obj.name)          # "Alice"
prn(obj.scores[1])     # 20
prn(obj.active)        # tru

# Handles nil and nested structures
prn(json.enc({x: nil, y: {z: "deep"}}))
# '{"x":null,"y":{"z":"deep"}}'

Functions

Encoding

json.enc(v) / json.encode(v)

Encodes v to a JSON string. Type mapping: nil"null", tru/fls"true"/"false", integers and floats → their string representation, strings → double-quoted with escaping, lists → JSON arrays, objects → JSON objects with string keys. Any other type is coerced to a quoted string.

Decoding

json.dec(s) / json.decode(s)

Decodes a JSON string to an ilusm value. Strings are unescaped (\n, \t, \\, \"). Numbers are parsed as integers. true/false/null become tru/fls/nil. Arrays and objects are decoded recursively. Whitespace between tokens is skipped.

Notes

  • Numbers are decoded as integers via int() - floating-point values are truncated. Use host.json_dec if you need native float parsing.
  • Requires trl and txt.