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 errorif 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 printedjson_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.
Constants
Section titled “Constants”| 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)}Functions
Section titled “Functions”json_error
Section titled “json_error”fn json_error() -> *u8Reason for the last failed json_parse, and the byte offset where it happened.
json_error_pos
Section titled “json_error_pos”fn json_error_pos() -> i64json_new
Section titled “json_new”fn json_new(kind: i64) -> *Jsonjson_null
Section titled “json_null”fn json_null() -> *Jsonjson_bool
Section titled “json_bool”fn json_bool(b: bool) -> *Jsonjson_int
Section titled “json_int”fn json_int(n: i64) -> *Jsonjson_num
Section titled “json_num”fn json_num(x: f64) -> *Jsonjson_str_n
Section titled “json_str_n”fn json_str_n(s: *u8, len: i64) -> *JsonA string value from len bytes (may contain NUL).
json_str
Section titled “json_str”fn json_str(s: *u8) -> *Jsonjson_array
Section titled “json_array”fn json_array() -> *Jsonjson_object
Section titled “json_object”fn json_object() -> *Jsonjson_free
Section titled “json_free”fn json_free(v: *Json)json_kind
Section titled “json_kind”fn json_kind(v: *Json) -> i64json_is_null
Section titled “json_is_null”fn json_is_null(v: *Json) -> booljson_len
Section titled “json_len”fn json_len(v: *Json) -> i64json_at
Section titled “json_at”fn json_at(v: *Json, i: i64) -> *Jsonjson_key_at
Section titled “json_key_at”fn json_key_at(v: *Json, i: i64) -> *u8json_value_at
Section titled “json_value_at”fn json_value_at(v: *Json, i: i64) -> *Jsonjson_index_of
Section titled “json_index_of”fn json_index_of(v: *Json, key: *u8) -> i64json_get
Section titled “json_get”fn json_get(v: *Json, key: *u8) -> *JsonMember of an object, or null.
json_has
Section titled “json_has”fn json_has(v: *Json, key: *u8) -> booljson_as_str
Section titled “json_as_str”fn json_as_str(v: *Json) -> *u8json_as_bool
Section titled “json_as_bool”fn json_as_bool(v: *Json) -> booljson_as_f64
Section titled “json_as_f64”fn json_as_f64(v: *Json) -> f64json_as_i64
Section titled “json_as_i64”fn json_as_i64(v: *Json) -> i64Integer value; fractional numbers are truncated toward zero.
json_push
Section titled “json_push”fn json_push(arr: *Json, item: *Json)Appends item (ownership moves to the array).
json_set
Section titled “json_set”fn json_set(obj: *Json, key: *u8, val: *Json)Sets a member (replacing an existing one); ownership of val moves to the object.
json_parse
Section titled “json_parse”fn json_parse(text: *u8, len: i64) -> *JsonParses len bytes of JSON text; null (see json_error) on any syntax error or trailing garbage.
json_parse_str
Section titled “json_parse_str”fn json_parse_str(text: *u8) -> *JsonConvenience for NUL-terminated text.
json_stringify
Section titled “json_stringify”fn json_stringify(v: *Json, pretty: bool) -> *u8Serializes a value into a new NUL-terminated string (release with dealloc). pretty uses two-space indentation.