module std::jsonsource
JSON values (RFC 8259): parsing, writing, reading and building.
A value is a Json: objects keep their members in order, as a vector,
which a lookup scans; JSON's objects are small in practice, and the
order a document was written in survives a round trip. Numbers without
a fraction or exponent read as Int (an i64), the others as Float,
read correctly rounded (std::float_digits), one too large or too small
for an f64 as infinity or zero, and written as the shortest decimal
reading back to the same float. Text is UTF-8, its escapes decoded on reading and
the needed ones written back.
const value = if (parse_json(&text) is ::Some |move v|) move v else { ... };
const id = value.try_get(&"id");
var reply = Json::object();
reply.set(&"result", Json::int(42));
const text = reply.to_json();
JsonJsonMemberkeyvalueJson::objectJson::arrayJson::textJson::intJson::boolsetpushgetattry_gettry_atlengthas_intas_floatas_textas_boolis_nullJsonReaderJsonReader::ofintfloattextboolhasobjectlistintstextsitemsfailjsonfailedfinishto_jsonwrite_jsonwrite_textparse_json
type Jsonsource
exp type Json = enum
Variants
NullBool: boolInt: i64Float: f64Text: StringArray: Vector<Json>Object: Vector<JsonMember>
Functions
exp fn Json::object() -> Jsonexp fn Json::array() -> Jsonexp fn Json::text(text: &[u8]) -> Jsonexp fn Json::int(value: i64) -> Jsonexp fn Json::bool(value: bool) -> Jsonexp fn set(self json: &var Json, key: &[u8], value: Json)exp fn push(self json: &var Json, value: Json)exp fn get(self json: &Json, key: &[u8]) -> &Jsonexp fn at(self json: &Json, index: u64) -> &Jsonexp fn try_get(self json: &Json, key: &[u8]) -> Option<&Json>exp fn try_at(self json: &Json, index: u64) -> Option<&Json>exp fn length(self json: &Json) -> u64exp fn as_int(self json: &Json) -> Option<i64>exp fn as_float(self json: &Json) -> Option<f64>exp fn as_text(self json: &Json) -> Option<&[u8]>exp fn as_bool(self json: &Json) -> Option<bool>exp fn is_null(self json: &Json) -> boolexp fn to_json(self json: &Json) -> String
type JsonMembersource
exp type JsonMember = struct
Functions
fn keysource
exp fn key(self member: &JsonMember) -> &[u8]
An object's member's key and value, as ::Object |&members| gives them.
fn valuesource
exp fn value(self member: &JsonMember) -> &Json
fn Json::objectsource
exp fn Json::object() -> Json
fn Json::arraysource
exp fn Json::array() -> Json
fn Json::textsource
exp fn Json::text(text: &[u8]) -> Json
fn Json::intsource
exp fn Json::int(value: i64) -> Json
fn Json::boolsource
exp fn Json::bool(value: bool) -> Json
fn setsource
exp fn set(self json: &var Json, key: &[u8], value: Json)
Sets an object's member, replacing one of the same key; does nothing to another value.
fn pushsource
exp fn push(self json: &var Json, value: Json)
Appends to an array; does nothing to another value.
fn getsource
exp fn get(self json: &Json, key: &[u8]) -> &Json
An object's member; a fault when it has none of the key or is no object.
fn atsource
exp fn at(self json: &Json, index: u64) -> &Json
An array's element; a fault past its end or on another value.
fn try_getsource
exp fn try_get(self json: &Json, key: &[u8]) -> Option<&Json>
An object's member, or None when it has none of the key or is no object.
fn try_atsource
exp fn try_at(self json: &Json, index: u64) -> Option<&Json>
An array's element, or None past its end or on another value.
fn lengthsource
exp fn length(self json: &Json) -> u64
An array's elements or an object's members; 0 for another value.
fn as_intsource
exp fn as_int(self json: &Json) -> Option<i64>
fn as_floatsource
exp fn as_float(self json: &Json) -> Option<f64>
fn as_textsource
exp fn as_text(self json: &Json) -> Option<&[u8]>
fn as_boolsource
exp fn as_bool(self json: &Json) -> Option<bool>
fn is_nullsource
exp fn is_null(self json: &Json) -> bool
type JsonReadersource
exp type JsonReader = struct
Reads a document into a program's own types, a call a member, keeping
the first error and where it was. After an error every call gives a
default (0, false, an empty string or list) and records nothing more,
so a reader is written straight through and finish says how it went.
import std::result;
import std::string;
import std::vector;
const text = "{\"shows\": [{\"title\": \"Tosca\", \"price_cents\": 2450}, {\"title\": \"Aida\"}]}";
const json = if (parse_json(&text) is ::Some |move j|) move j else Json::Null;
var r = JsonReader::of(&json);
const titles = r.list(&"shows", (show) => show.text(&"title"));
expect_equal(&titles.length(), &2);
var prices = r.list(&"shows", (show) => show.int(&"price_cents"));
match (r.finish(move prices)) {
::Ok |&p| {
expect(false);
}
::Err |&why| {
expect_equal(&why.view(), &"shows[1].price_cents: missing");
}
}
Functions
exp fn JsonReader::of(json: &Json) -> JsonReaderexp fn int(self r: &var JsonReader, key: &[u8]) -> i64exp fn float(self r: &var JsonReader, key: &[u8]) -> f64exp fn text(self r: &var JsonReader, key: &[u8]) -> Stringexp fn bool(self r: &var JsonReader, key: &[u8]) -> boolexp fn has(self r: &JsonReader, key: &[u8]) -> boolexp fn object<T>(self r: &var JsonReader, key: &[u8], read: |&| (&var JsonReader) -> T) -> Texp fn list<T>(self r: &var JsonReader, key: &[u8], read: |&| (&var JsonReader) -> T) -> Vector<T>exp fn ints(self r: &var JsonReader, key: &[u8]) -> Vector<i64>exp fn texts(self r: &var JsonReader, key: &[u8]) -> Vector<String>exp fn items<T>(self r: &var JsonReader, read: |&| (&var JsonReader) -> T) -> Vector<T>exp fn fail(self r: &var JsonReader, key: &[u8], why: &[u8])exp fn json(self r: &JsonReader) -> &Jsonexp fn failed(self r: &JsonReader) -> boolexp fn finish<T>(self r: &var JsonReader, value: T) -> Result<T, String>
fn JsonReader::ofsource
exp fn JsonReader::of(json: &Json) -> JsonReader
A reader of the value.
fn intsource
exp fn int(self r: &var JsonReader, key: &[u8]) -> i64
The member as a whole number: a float with no fraction is one.
fn floatsource
exp fn float(self r: &var JsonReader, key: &[u8]) -> f64
The member as a number, whole or not.
fn textsource
exp fn text(self r: &var JsonReader, key: &[u8]) -> String
fn boolsource
exp fn bool(self r: &var JsonReader, key: &[u8]) -> bool
fn hassource
exp fn has(self r: &JsonReader, key: &[u8]) -> bool
Whether the member is there and not null: for members a document may leave out.
fn objectsource
exp fn object<T>(self r: &var JsonReader, key: &[u8], read: |&| (&var JsonReader) -> T) -> T
The member, an object, read by read with a reader of its own.
fn listsource
exp fn list<T>(self r: &var JsonReader, key: &[u8], read: |&| (&var JsonReader) -> T) -> Vector<T>
The member, a list, each item read by read with a reader of its own.
fn intssource
exp fn ints(self r: &var JsonReader, key: &[u8]) -> Vector<i64>
The member, a list of whole numbers, as "seats": [12, 13].
import std::option;
import std::string;
import std::vector;
const text = "{\"seats\": [12, 13], \"tags\": [\"jazz\", \"late\"], \"rows\": [1, \"B\"]}";
const json = if (parse_json(&text) is ::Some |move j|) move j else Json::Null;
var r = JsonReader::of(&json);
const seats = r.ints(&"seats");
const tags = r.texts(&"tags");
expect_equal(&seats.get(1), &13);
expect_equal(&tags.get(0).view(), &"jazz");
const rows = r.ints(&"rows");
expect_equal(&rows.length(), &1);
if (r.finish(0) is ::Err |&why|) {
expect_equal(&why.view(), &"rows[1]: not a number");
}
fn textssource
exp fn texts(self r: &var JsonReader, key: &[u8]) -> Vector<String>
The member, a list of strings, as "tags": ["jazz", "late"].
fn itemssource
exp fn items<T>(self r: &var JsonReader, read: |&| (&var JsonReader) -> T) -> Vector<T>
The value itself, a list, each item read by read: a document that is
a list at the top.
fn failsource
exp fn fail(self r: &var JsonReader, key: &[u8], why: &[u8])
Records an error of the program's own about the member, as `price_cents: below 0`, unless one came first.
fn jsonsource
exp fn json(self r: &JsonReader) -> &Json
The value read: an item of a list that is no object.
fn failedsource
exp fn failed(self r: &JsonReader) -> bool
Whether an error was recorded.
fn finishsource
exp fn finish<T>(self r: &var JsonReader, value: T) -> Result<T, String>
The value read, or the first error with its path.
fn to_jsonsource
exp fn to_json(self json: &Json) -> String
The value as compact JSON text.
fn write_jsonsource
exp fn write_json(json: &Json, out: &var String)
fn write_textsource
exp fn write_text(text: &[u8], out: &var String)
A string, quoted, with its quotes, backslashes and control bytes escaped.
fn parse_jsonsource
exp fn parse_json(text: &[u8]) -> Option<Json>
The value the whole text holds, white space around it aside, or None when it is not JSON. Nesting stops at 512 levels.