Skip to content

JSON

Source: std/json.mw.

Parses text into a tree of Json values and writes it back, with optional pretty-printing. On a parse error json_parse returns null; json_error and json_error_pos say why and where.

JSON (RFC 8259): a strict parser producing a tree of *Json values, a builder API and a serializer.

var v: *Json = json_parse(text, len); // null on error
if v == null { println(json_error()); }
var name: *Json = json_get(v, "name");
println(json_as_str(name));
var out: *u8 = json_stringify(v, true); // pretty printed
json_free(v);

Parsed integers that fit an i64 keep full precision (json_as_i64); everything else is a double. Builder functions take ownership of the values passed in.

Name Type Value Notes
JSON_NULL i64 0
JSON_BOOL i64 1
JSON_NUMBER i64 2
JSON_STRING i64 3
JSON_ARRAY i64 4
JSON_OBJECT i64 5
struct Json {
kind: i64;
flag: i64; // bool value, or 1 when the number is an exact integer
ival: i64;
num: f64;
str: *u8; // NUL-terminated copy of the string value
slen: i64;
items: *VecPtr; // array elements / object values
keys: *VecPtr; // object keys (*u8)
}
fn json_error() -> *u8

Reason for the last failed json_parse, and the byte offset where it happened.

fn json_error_pos() -> i64
fn json_new(kind: i64) -> *Json
fn json_null() -> *Json
fn json_bool(b: bool) -> *Json
fn json_int(n: i64) -> *Json
fn json_num(x: f64) -> *Json
fn json_str_n(s: *u8, len: i64) -> *Json

A string value from len bytes (may contain NUL).

fn json_str(s: *u8) -> *Json
fn json_array() -> *Json
fn json_object() -> *Json
fn json_free(v: *Json)
fn json_kind(v: *Json) -> i64
fn json_is_null(v: *Json) -> bool
fn json_len(v: *Json) -> i64
fn json_at(v: *Json, i: i64) -> *Json
fn json_key_at(v: *Json, i: i64) -> *u8
fn json_value_at(v: *Json, i: i64) -> *Json
fn json_index_of(v: *Json, key: *u8) -> i64
fn json_get(v: *Json, key: *u8) -> *Json

Member of an object, or null.

fn json_has(v: *Json, key: *u8) -> bool
fn json_as_str(v: *Json) -> *u8
fn json_as_bool(v: *Json) -> bool
fn json_as_f64(v: *Json) -> f64
fn json_as_i64(v: *Json) -> i64

Integer value; fractional numbers are truncated toward zero.

fn json_push(arr: *Json, item: *Json)

Appends item (ownership moves to the array).

fn json_set(obj: *Json, key: *u8, val: *Json)

Sets a member (replacing an existing one); ownership of val moves to the object.

fn json_parse(text: *u8, len: i64) -> *Json

Parses len bytes of JSON text; null (see json_error) on any syntax error or trailing garbage.

fn json_parse_str(text: *u8) -> *Json

Convenience for NUL-terminated text.

fn json_stringify(v: *Json, pretty: bool) -> *u8

Serializes a value into a new NUL-terminated string (release with dealloc). pretty uses two-space indentation.