Moved. This library now lives in aoughwl/std (directory
jsonref/), with its full history. This repository is kept as a redirect; new work happens inaoughwl/std.
jsonref is a plain, ergonomic JSON library for Nimony: a reference
JsonValue tree with a recursive-descent parser, a compact serializer, safe
nested access, and value builders. No dependencies beyond Nimony's std/strutils.
Nimony ships a NIF-backed std/json, but its document is move-only and only
exposes root-level key lookup — awkward to thread through an ordinary API
where you want req{"params"}{"name"}.getStr(""). jsonref owns a normal
ref value tree instead, so JSON reads and builds the way you'd expect.
In keeping with the aoughwl stack: errors are values, never exceptions.
parseJson reports failure through an err out-parameter; nothing raises.
Requires the Nimony compiler (~/nimony/bin/nimony). No external dependency.
git clone https://github.com/aoughwl/jsonref
cd jsonref
./build.sh # runs tests/tjson.nimPoint elsewhere with NIMONY=/path/to/nimony ./build.sh.
import jsonref
var err = ""
let v = parseJson("""{"user":{"name":"ada"},"tags":["x","y"]}""", err)
assert err.len == 0
echo v{"user"}{"name"}.getStr("") # ada — missing paths chain to null safely
echo v{"tags"}.at(0).getStr # x
echo v{"nope"}{"deep"}.getStr("fb") # fb — no fault, no nil check
let o = newJObject()
o["ok"] = newJBool(true)
o["n"] = newJInt(42)
let a = newJArray(); a.add(newJString("z")); o["items"] = a
echo $o # {"ok":true,"n":42,"items":["z"]}| Group | API |
|---|---|
| parse | parseJson(s, err): JsonValue — error by value, never raised |
| serialize | $v — compact; numbers round-trip by their source lexeme |
| navigate | v{"key"} (object), v.at(i) (array) — missing paths yield a JNull |
| read | getStr, getInt, getBool, len, hasKey, isNull, items, pairs |
| build | newJObject, newJArray, newJString, newJInt, newJBool, newJNull, newJRawNumber, obj["k"] = v, arr.add v |
JsonValue is a ref object variant over jnNull / jnBool / jnNumber / jnString / jnArray / jnObject. Numbers are stored as their source lexeme, so any JSON
number re-serializes byte-for-byte; getInt decodes on demand. Object members
keep insertion order.
src/jsonref.nim the library (import this)
tests/tjson.nim round-trip, nested access, builders, unicode-escape tests
- aowlmcp — MCP servers in Nimony.
See LICENSE.