Skip to content

Json

Module: std.json
Source: std/json.zn

Usage Example

use std.json;
fn main() {
let value = JsonValue.parse(r#"{"name":"zeen","tags":[1,2]}"#).unwrap();
let name = value.get("name").unwrap();
@println("name: {}", name.as_str().unwrap());
@println("tags: {}", value.get("tags").unwrap().len());
@println("{}", value.stringify());
}

JsonErrorKind

JSON parse error kinds


pub enum JsonErrorKind {
UnexpectedChar,
UnexpectedEnd,
TrailingData,
DepthExceeded,
InvalidNumber,
InvalidEscape,
}

JsonValue

JSON value, numbers kept as raw text


pub enum JsonValue {
Null,
Bool: bool,
Num: String,
Str: String,
Array: List[JsonValue],
Object: JsonObject,
}

Fields:

// Payloads per variant

Methods:

pub fn parse(text: []const char) Result[JsonValue, JsonError];
pub fn stringify(*const self) String;
pub fn get(*const self, key: []const char) Option[*const JsonValue];
pub fn at(*const self, index: usize) Option[*const JsonValue];
pub fn len(*const self) usize;
pub fn is_null(*const self) bool;
pub fn is_bool(*const self) bool;
pub fn is_num(*const self) bool;
pub fn is_str(*const self) bool;
pub fn is_array(*const self) bool;
pub fn is_object(*const self) bool;
pub fn as_bool(*const self) Option[bool];
pub fn as_num(*const self) Option[String];
pub fn as_i64(*const self) Option[i64];
pub fn as_f64(*const self) Option[f64];
pub fn as_str(*const self) Option[String];
pub fn write_into(*const self, out: *String);

JsonError

JSON parse error


Fields:

pub kind: JsonErrorKind,
pub pos: usize,

JsonObject

Object backing in insert order, fast lookup


Fields:

map: HashMap[String, JsonValue],
keys: List[String],

Constructors:

pub fn new() Self;

Methods:

pub fn insert(*self, key: String, value: JsonValue);
pub fn get(*const self, key: []const char) Option[*const JsonValue];
pub fn key_at(*const self, index: usize) *String;
pub fn get_at(*const self, index: usize) *const JsonValue;
pub fn len(*const self) usize;
pub fn write_into(*const self, out: *String);