Skip to content
volt
GitHub

std::json

JSON values, read from text (parse) and written back (text).

Source: std/json.volt

Types

enum std::json::value

<A: std::mem::allocator = std::mem::default_allocator> enum value

a JSON value; an object keeps its members in order. Its strings and arrays come from A

Variant
NULLnull
BOOL: booltrue or false
NUM: f64a number (JSON has one kind)
STR: std::string<A>a string, unescaped
ARR: std::vec<value<A>, A>an array
OBJ: std::vec<member<A>, A>an object: its members in order

Methods

<A: std::mem::allocator, B: std::mem::allocator = std::mem::default_allocator> attach fn text(this: value<A>&, allocator: B = {}) -> std::string<B>

the value as compact JSON text

<A: std::mem::allocator, B: std::mem::allocator> attach fn write(this: value<A>&, out: std::string<B>&) -> void

append the value to out as compact JSON text

<A: std::mem::allocator> attach fn get(this: value<A>&, key: str) -> value<A>&

an object's member called key (null when there's none)

<A: std::mem::allocator> attach fn at(this: value<A>&, i: usize) -> value<A>&

an array's element i (null when there's none)

<A: std::mem::allocator> attach fn len(this: value<A>&) -> usize

how many elements (an array) or members (an object)

<A: std::mem::allocator> attach fn as_str(this: value<A>&) -> str?

a string's text (null for anything else)

<A: std::mem::allocator> attach fn as_num(this: value<A>&) -> f64?

a number (null for anything else)

<A: std::mem::allocator> attach fn as_bool(this: value<A>&) -> bool?

true or false (null for anything else)

<A: std::mem::allocator> attach fn is_null(this: value<A>&) -> bool

is it null? (a missing member or element is too)

<A: std::mem::allocator> attach fn set(this: value<A>&, key: str, v: value<A>) -> void

set an object's member (replacing one with the same name)

<A: std::mem::allocator> attach fn add(this: value<A>&, v: value<A>) -> void

add an element to an array

struct std::json::member

<A: std::mem::allocator = std::mem::default_allocator> struct member

one member of an object

Field
name: std::string<A>the key
item: value<A>its value

error std::json::json_error

error json_error

why parse failed

Variant
SYNTAXthe text isn't JSON

Functions

std::json::parse

<A: std::mem::allocator = std::mem::default_allocator> fn parse(text: str, allocator: A = {}) -> json_error!value<A>

the text parsed; whitespace around the value is fine, anything else after it isn't

std::json::object

<A: std::mem::allocator = std::mem::default_allocator> fn object(allocator: A = {}) -> value<A>

an empty object (add members with set)

std::json::array

<A: std::mem::allocator = std::mem::default_allocator> fn array(allocator: A = {}) -> value<A>

an empty array (add elements with add)

std::json::string

<A: std::mem::allocator = std::mem::default_allocator> fn string(s: str, allocator: A = {}) -> value<A>

a string value holding a copy of s

std::json::number

<A: std::mem::allocator = std::mem::default_allocator> fn number(x: f64, allocator: A = {}) -> value<A>

a number value (the allocator only picks which value<A> it is)

std::json::boolean

<A: std::mem::allocator = std::mem::default_allocator> fn boolean(b: bool, allocator: A = {}) -> value<A>

true or false (the allocator only picks which value<A> it is)

std::json::null_value

<A: std::mem::allocator = std::mem::default_allocator> fn null_value(allocator: A = {}) -> value<A>

null (the allocator only picks which value<A> it is)