module yeti.json

Simple library for parsing and constructing JSON.

Examples

Parsing can be done using jsonParse function:
 load yeti.json;
 js = jsonParse '{"foo":[1,2,3],"bar":{"nope":true},"baz":null}';

The parsed value can be accessed with various js-prefixed functions in the module, for example:
 for (js |> jsGet 'foo' |> jsList) println;

Which would print the numbers 1, 2, 3 on separate lines.

The JSON string can be formatted using string function, or any library function that does this implicitly, for example the following println statements are equivalent:
 println (string js);
 println js;

typedef json = [yeti/json:json]<>

typedef json_variant =
Boolean boolean |
List list<json> |
Null () |
Number number |
Object hash<string, json> |
String string

Module signature

{
jsGet field value is string → json → json,
Retrieves a field value from the JSON value. The jsNull value is returned, if the value isn't a JSON object, or the field doesn't exists in the object.
field-field name
value-JSON value (expected to be an object)
jsKeys value is json → list<string>,
Retrieves list of field names from JSON value. Empty list is returned, if the value isn't a JSON object.
value-JSON value (expected to be an object)
jsList value is json → list<json>,
Retrieves list from JSON value. If the value is not a list, then empty list [] is returned. Application of defined? function to the returned list returns false, if the JSON value wasn't a list.
value-JSON value (expected to be a string)
jsNull is json,
JSON null value.
jsNull? value is json → boolean,
Returns true if the value is JSON null.
value-JSON value
jsNum default value is (json → number) → json → number,
Retrieves number from JSON value. If the value is not a number, then the result of applying the default function to the value is returned.
default-function to apply, if the value is not a number.
value-JSON value (expected to be number)
jsOfBool boolean is boolean → json,
Create a JSON value from boolean.
boolean-a boolean to use
jsOfList list is list?<json> → json,
Create a JSON value from list.
list-list of JSON values
jsOfNum number is number → json,
Create a JSON value from number.
number-a number to use
jsOfObj fields is hash<string, json> → json,
Create a JSON object from fields in a hash table representation.
fields-Fields for the JSON object
jsOfStr string is string → json,
Create a JSON value from string.
string-a string to use
jsStr default value is (json → string) → json → string,
Retrieves string from JSON value. If the value is not a string, then the result of applying the default function to the value is returned.
default-function to apply, if the value is not a string.
value-JSON value (expected to be string)
jsTrue? default value is json → boolean,
Retrieves boolean from JSON value. If the value is not a boolean, then undef_bool is returned (which acts as false, when used in condition, but can be detected using defined? function).
default-function to apply, if the value is not a number.
value-JSON value (expected to be boolean)
jsValue value is json → json_variant,
Retrieves variant representation of the JSON value.
value-JSON value
jsonParse string is string → json
Parses a string into JSON value.
string-a string to parse

Notes

The reverse operation of formatting JSON value into string can be achieved simply by appling the string function from standard library to a JSON value.
}