Nox
Download

Standard library

nox.json

Parse and generate JSON. Parsing is done by a battle-tested native parser; the result is a tree of JsonValue nodes that you inspect with typed accessors. For generating JSON there are two styles: dump an existing tree, or build text incrementally with JsonWriter.

Text
import nox.json
from nox.json import JsonError, JsonWriter

Capability: none.

JsonValue is part of the prelude (it needs no import); JsonError is raised on malformed input.

Parsing#

Function Description
parse(s) parses s into a JsonValue; raises JsonError for invalid JSON (any depth of nesting is supported)

Inspecting a JsonValue#

Every node is exactly one of null, boolean, number, string, array or object. Test the kind first, then read it:

Function Description
is_null(v), is_bool(v), is_number(v), is_string(v), is_array(v), is_object(v) kind tests
as_bool(v), as_number(v), as_string(v) the value; as_number is a float (integers above 2^53 lose precision as a float: use as_int)
as_int(v) the exact int value of a number (a JsonError if it is not an integer or does not fit in int); integers of 2^53 and above keep their exact text, so 9007199254740993 is not rounded
number_text(v) the number's decimal text (the exact text for integers of 2^53 and above, and 1.0 / 100.0 for a number written with a fraction or exponent); dump writes such numbers unchanged
array_len(v), array_get(v, i) array length and element
object_len(v), object_key(v, i), object_value(v, i) an object's i-th entry, in document order
Nox
import nox.json
from nox.json import JsonError

v: JsonValue = nox.json.parse("{\"name\": \"ada\", \"age\": 36, \"tags\": [\"a\", \"b\"], \"ok\": true, \"none\": null, \"pi\": 3.5}")
print(nox.json.is_object(v), nox.json.object_len(v))
i: int = 0
while i < nox.json.object_len(v):
    k: str = nox.json.object_key(v, i)
    x: JsonValue = nox.json.object_value(v, i)
    if nox.json.is_string(x):
        print(k, "string", nox.json.as_string(x))
    elif nox.json.is_number(x):
        print(k, "number", nox.json.as_number(x))
    elif nox.json.is_bool(x):
        print(k, "bool", nox.json.as_bool(x))
    elif nox.json.is_array(x):
        print(k, "array", nox.json.array_len(x), nox.json.as_string(nox.json.array_get(x, 1)))
    elif nox.json.is_null(x):
        print(k, "null")
    i += 1
try:
    nox.json.parse("{bad")
except JsonError as e:
    print("JsonError")
Output
True 6
name string ada
age number 36.0
tags array 2 b
ok bool True
none null
pi number 3.5
JsonError

To look a key up by name use get (a linear scan in document order; there is no hash index). For large documents prefer the position-based functions below.

Checked readers#

Function Description
get(v, key), get_or_null(v, key) an object's value by key (JsonError if missing / a JSON null)
expect_object(v, what), expect_array(v) JsonError unless the node has that kind
read_int(v), read_float(v), read_bool(v), read_str(v) the value, or JsonError when the node has another kind

They are what the code generated by @derive("json") calls; you can use them directly.

Position-based traversal (no nodes)#

array_get, object_key and object_value build a JsonValue (and a str for keys) for every step. On large documents that allocation is the cost of a traversal. The pos_* functions walk the parsed document by position (an int) instead: nothing is allocated except the strings you ask for, so a traversal is several times faster. They do not replace the node API: pos_value(v, p) turns a position into a node whenever you need one.

v is any node of the document (usually the parse result); positions are only valid in that document. A wrong position never crashes: it raises JsonError("invalid position"). A scalar root (parse("5")) has no positions: use as_*.

Function Description
pos_root(v) position of v itself
pos_kind(v, p) the kind: 0 null, 1 bool, 2 number, 3 string, 4 array, 5 object
pos_len(v, p), pos_at(v, p, i) length of an array/object, and the position of the i-th element (for an object: of the i-th entry's value; a negative i counts from the end)
pos_get(v, p, key), pos_find(v, p, key) the position of an object's value by key (pos_get raises JsonError if it is missing, pos_find returns -1)
pos_key_is(v, p, i, key), pos_key(v, p, i) compare the i-th key with key without allocating, or read it as a str
pos_int, pos_number, pos_bool, pos_str the value at a position, with the same rules as as_int / as_number / as_bool / as_string (and a JsonError for another kind)
pos_value(v, p), pos_dump(v, p) the node at a position, and its compact JSON text
Nox
import nox.json

v: JsonValue = nox.json.parse("[{\"id\": 1, \"name\": \"ada\", \"score\": 2.5}, {\"id\": 2, \"name\": \"bob\", \"score\": 4.0}]")
root: int = nox.json.pos_root(v)
total: int = 0
best: str = ""
i: int = 0
while i < nox.json.pos_len(v, root):
    row: int = nox.json.pos_at(v, root, i)
    total += nox.json.pos_int(v, nox.json.pos_get(v, row, "id"))
    if nox.json.pos_number(v, nox.json.pos_get(v, row, "score")) > 3.0:
        best = nox.json.pos_str(v, nox.json.pos_get(v, row, "name"))
    i += 1
print(total, best)
print(nox.json.pos_dump(v, nox.json.pos_at(v, root, 0)))
Output
3 bob
{"id":1,"name":"ada","score":2.5}

Dumping a tree#

Function Description
dump(v) compact JSON text. A number keeps the way it was written: 1 stays 1, 1.0 and 1e2 come back as 1.0 and 100.0 (like Python's json.dumps), so a parse/dump round trip never turns a float into an int
dump_pretty(v, indent) indented JSON, indent spaces per level
dump_string(s) a JSON string literal for s, with all required escapes
dump_number(n) a JSON number for a float (whole values print without .0)
dump_array(v), dump_object(v) compact dump of an array / object node
dump_pretty_at(v, indent, level), dump_pretty_array(...), dump_pretty_object(...) the recursive pretty-printing helpers behind dump_pretty
indent_str(indent, level) the indentation string for a level
Nox
import nox.json

v: JsonValue = nox.json.parse("{\"name\": \"ada\", \"tags\": [\"a\", \"b\"], \"pi\": 3.5}")
print(nox.json.dump(v))
print(nox.json.dump_pretty(v, 2))
print(nox.json.dump_string("a\"b\n"), nox.json.dump_number(3.0), nox.json.dump_number(2.5))
Output
{"name":"ada","tags":["a","b"],"pi":3.5}
{
  "name": "ada",
  "tags": [
    "a",
    "b"
  ],
  "pi": 3.5
}
"a\"b\n" 3 2.5

JsonWriter — building JSON without a tree#

JsonWriter appends pieces and joins them once at the end, so building a large document is linear in its size. Open containers with begin_object / begin_array and close them in order; inside an object call write_key before each value.

Method Description
JsonWriter() an empty writer
begin_object(), end_object(), begin_array(), end_array() containers
write_key(key) the key of the next value (inside an object)
write_string(s), write_int(n), write_float(n), write_bool(b), write_null() scalar values
write_value(v) embeds an existing JsonValue subtree
build() the finished JSON text
Nox
from nox.json import JsonWriter

w: JsonWriter = JsonWriter()
w.begin_object()
w.write_key("id")
w.write_int(7)
w.write_key("items")
w.begin_array()
w.write_string("x")
w.write_float(1.5)
w.write_bool(False)
w.write_null()
w.end_array()
w.end_object()
print(w.build())
Output
{"id":7,"items":["x",1.5,false,null]}

The writer does not validate your nesting: mismatched end_* calls or a missing write_key produce malformed text.

Notes#

  • Numbers are doubles: integers above 2⁵³ lose precision. Strings are decoded to UTF-8 str.
  • The names decode, encode, encode_pretty and the other encode_* forms were removed in 2.0 (Migrating).