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
| Variant | Fields | Description |
|---|---|---|
UnexpectedChar | ch: 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. | |
Other | msg: 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
| Variant | Fields | Description |
|---|---|---|
Null | JSON null. | |
Bool | value: bool | JSON boolean. |
Number | value: f64 | JSON number (IEEE 754 double). |
Str | value: String | JSON string. |
Array | items: ArrayList(enum(Null, Bool(value: bool), Number(value: f64), Str(value: String))) | JSON array of values. |
Object | keys: 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)at : (JsonValue) fn(self : JsonValue, index : usize) -> Option(JsonValue)as_bool : (JsonValue) fn(self : JsonValue) -> Option(bool)as_number : (JsonValue) fn(self : JsonValue) -> Option(f64)as_string : (JsonValue) fn(self : JsonValue) -> Option(String)as_array : (JsonValue) fn(self : JsonValue) -> Option(ArrayList(enum(Null, Bool(value: bool), Number(value: f64), Str(value: String))))