std.json - ZZ
JSON parse/stringify/get/as_* with an opaque json handle. Buffers (bytes) serialize as int arrays. json.parse is fallible (.err on invalid JSON) and never panics.
import std.jsonPurpose
Section titled “Purpose”std.json parses JSON strings into an opaque json handle, extracts typed fields, and serializes values back to JSON. Extraction functions (as_str, as_int, …) are total but panic on type mismatch — guard with is_* or use ?-able get first.
Conceptual model
Section titled “Conceptual model”json is an opaque handle — you cannot inspect it directly. You navigate via get/deep_get (object) or indexing conventions, then extract via as_str/as_int/as_float/as_bool. Type checks (is_null, is_bool, is_number, is_string, is_array, is_object, is_empty) let you branch safely before extraction.
Quickstart
Section titled “Quickstart”import std.json
func main() -> Result<int, str> { data := json.parse(`{"name": "Alice", "age": 30, "tags": ["a","b"]}`)? println(json.as_str(json.get(data, "name")?)) // Alice println(json.as_int(json.get(data, "age")?)) // 30 person := {"name": "Bob", "age": 25} println(json.stringify(person)?) // {"name":"Bob","age":25} return .ok(0)}
// Safe navigation with type checksfunc safe_nav() -> Result<int, str> { data := json.parse(`{"name": "Alice"}`)? if json.is_object(data) && json.has(data, "name") { println(json.as_str(json.get(data, "name")?)) } return .ok(0)}Error model
Section titled “Error model”parsereturns.err(message)on invalid JSON — never panics.as_*panic on type mismatch — preferis_*guards orget+?.get/deep_getreturn.erron missing key —?-able.parse_or/parse_or_nullnever fail (tolerant).
Gotchas
Section titled “Gotchas”jsonis opaque — inspect only viaas_*,get,keys,is_*.bytesserialize to JSON as int arrays.json.stringifyis generic overT— dicts, arrays, scalars all work.as_*panics on mismatch — always check withis_*first in production code.
Production patterns
Section titled “Production patterns”import std.json
func main() -> Result<int, str> { // Tolerant parse user_input := `{"address": {"city": "NYC"}}` data := json.parse_or(user_input, json.null())
// Deep path with default city := json.path_get_or(data, "address.city", json.null()) if json.is_string(city) { println(json.as_str(city)) }
// Type-safe extraction if json.is_object(data) && json.has(data, "email") { _ = json.as_str(json.get(data, "email")?) } return .ok(0)}Client
Section titled “Client”headers optional, defaults {}. body may be str or bytes.
| Call | Notes |
|---|---|
get(url, headers={}) |
GET |
post/put(url, body, headers={}) |
body str/bytes |
delete(url, headers={}) |
DELETE |
fetch(url, method="GET", headers={}, body="", timeout_ms=30000) |
any verb, timeout |
post_json(url, body: T, headers={}) |
serializes any value, sets Content-Type |
fetch_insecure(...) |
skips TLS verify — self-signed fixtures only |
match http.get("https://api.example.com/users") { .ok(res) => println("users: {res.text()}"), .err(e) => println("failed: {e}"),}
func main() -> Result<int, str> { res := http.get("https://api.example.com/users")? body := res.json()? println(json.stringify(body) ?? "{}") return .ok(0)}listen blocks with keep-alive + graceful SIGINT/SIGTERM drain.
listen_cfg adds read_ms, max_reqs_conn, max_body_bytes, shutdown_ms.
API reference
Section titled “API reference”28 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.
See also
Section titled “See also”std.map— Pure dictionary helpers over plain maps: lookup, merge and keys.std.set— Arrays with uniqueness: membership, union, intersection and difference.std.dec— Exact decimal arithmetic carried as strings, so money never becomes a float.std.csv— An RFC 4180 subset: read rows, write rows, handle quotes and commas.std.property— Seeded generators and shrinking for property-based tests.
Full index: API reference · Standard library