Module encoding/json

encoding/json

JSON parsing and serialization.

Provides a dynamically-typed JSON value tree.

Example

{ json_parse, json_stringify, JsonValue } :: import "std/encoding/json";
{ Exception } :: import "std/error";

given(exn) := Exception(throw : ((err) -> { escape (); }));
v := json_parse(`{"x": 1}`);
println(json_stringify(v));

Types

JsonError enum
JsonError

JSON parsing error type.

Variants

VariantFieldsDescription
UnexpectedCharch: u8, pos: usize

Encountered an unexpected character at the given position.

UnexpectedEnd

Input ended unexpectedly.

InvalidNumber

Failed to parse a numeric literal.

InvalidEscape

Invalid escape sequence in a string.

InvalidUnicode

Invalid Unicode escape in a string.

Othermsg: String

Other error with a descriptive message.

Trait Implementations

impl(JsonError, ToString(...))
to_string : (self -> match(self, .UnexpectedChar(ch, pos) => `unexpected character at position ${pos}`, .UnexpectedEnd => `unexpected end of input`, .InvalidNumber => `invalid number`, .InvalidEscape => `invalid escape sequence`, .InvalidUnicode => `invalid unicode`, .Other(msg) => `JSON error: ${msg}` ) )
impl(JsonError, Error())
JsonValue enum
JsonValue

Dynamically-typed JSON value tree.

Variants

VariantFieldsDescription
Null

JSON null.

Boolvalue: bool

JSON boolean.

Numbervalue: f64

JSON number (IEEE 754 double).

Strvalue: String

JSON string.

Arrayitems: ArrayList(enum(Null, Bool(value: bool), Number(value: f64), Str(value: String)))

JSON array of values.

Objectkeys: ArrayList(String), values: ArrayList(enum(Null, Bool(value: bool), Number(value: f64), Str(value: String)))

JSON object with ordered key-value pairs.

impl(JsonValue, ...)
get : (JsonValue) fn(self : JsonValue, key : String) -> Option(JsonValue)

Get a field from an Object value by key. Returns .None if not an Object or key is missing.

Parameters

NameTypeNotes
selfJsonValue
keyString

Returns: Option(JsonValue)

at : (JsonValue) fn(self : JsonValue, index : usize) -> Option(JsonValue)

Get an element from an Array value by index.

Parameters

NameTypeNotes
selfJsonValue
indexusize

Returns: Option(JsonValue)

as_bool : (JsonValue) fn(self : JsonValue) -> Option(bool)

Extract the boolean value, or .None if not a Bool.

Parameters

NameTypeNotes
selfJsonValue

Returns: Option(bool)

as_number : (JsonValue) fn(self : JsonValue) -> Option(f64)

Extract the numeric value, or .None if not a Number.

Parameters

NameTypeNotes
selfJsonValue

Returns: Option(f64)

as_string : (JsonValue) fn(self : JsonValue) -> Option(String)

Extract the string value, or .None if not a Str.

Parameters

NameTypeNotes
selfJsonValue

Returns: Option(String)

as_array : (JsonValue) fn(self : JsonValue) -> Option(ArrayList(enum(Null, Bool(value: bool), Number(value: f64), Str(value: String))))

Extract the array items, or .None if not an Array.

Parameters

NameTypeNotes
selfJsonValue

Returns: Option(ArrayList(enum(Null, Bool(value: bool), Number(value: f64), Str(value: String))))

as_object : (JsonValue) fn(self : JsonValue) -> Option(ArrayList(JsonKV))

Extract object entries as ArrayList(JsonKV), or .None if not an Object.

Parameters

NameTypeNotes
selfJsonValue

Returns: Option(ArrayList(JsonKV))

JsonKV struct
JsonKV

Key-value pair for JSON object entries.

Fields

NameTypeDescription
keyString
valueJsonValue

Functions

json_parse function
fn(s : str, using(exn : Exception)) -> JsonValue

Parse a JSON string into a JsonValue tree. Throws via Exception on invalid input.

Parameters

NameTypeNotes
sstr

Effects

NameTypeNotes
exnExceptioncomptime, implicit

Returns: JsonValue

json_stringify function
fn(value : JsonValue) -> String

Serialize a JsonValue to a compact JSON string.

Parameters

NameTypeNotes
valueJsonValue

Returns: String

fn(value : JsonValue, _indent : usize) -> String

Serialize a JsonValue to a pretty-printed JSON string.

Parameters

NameTypeNotes
valueJsonValue
_indentusize

Returns: String