Kid

Syntax

Kid (Keen Interface for Data) is a serialization format like JSON.
It is equivalent to JSON and differs only in syntax.

Kid looks exactly like Keen. It is just limited to data expressions with no variables, control flow, etc.

Valid Kid expressions are:

  • null, booleans, numbers and strings, using the exact same syntax as JSON. (() also returns null.)
  • Comma-separated or multi-line new expressions, corresponding to JSON arrays.
  • Named-new expressions, corresponding to JSON objects.
import keen/parse main void() kid-string = quote null: () bool: false number: 1 string: "hello" array: 1, 2 another-array: - 1 - 2 object: x: 1, y: 2 another-object: x: 1 y: 2 value json = null: () bool: false number: 1 string: "hello" array: 1, 2 another-array: - 1 - 2 object: x: 1, y: 2 another-object: x: 1 y: 2 parsed = parse kid, kid-string log value == parsed log value

The json type is the type of a parsed JSON value.
Since Kid has the same data model as JSON, a parsed Kid value is also of type json.

Keen and Kid

In the above example, value is created using a variety of functions (such as named-new) that return json.
Conveniently, the syntax is exactly the same as Kid.

This is also true for typed objects. Below, the syntax to construct a point in Keen is the exactly the same same as the syntax for the Kid expression that converts to it.

import keen/parse main void() kid-string = quote x: 1 y: 2 value point = x: 1 y: 2 parsed = to::point parse kid, kid-string log value == parsed log parsed point record(x float, y float) == bool(a point, b point) to json(a point) to point(a json) a["x"].to, a["y"].to

Since the syntax is the same, you can cut and paste data between Keen and Kid files.

It's also useful to be able to output a value in a format that is valid source code.

Empty values

Keen has a single syntax for empty values: ().
JSON has 3: null, [], and {}.
Usually you should just use () (which means null) and expect the user to treat it the same as other empty values.
In cases where it does matter, use empty-array or empty-object. This works the same in both Keen and Kid.

import keen/parse main void() kid-string = quote null: () array: empty-array object: empty-object value json = null: () array: empty-array object: empty-object parsed = parse kid, kid-string log value == parsed log parsed

Using JSON values

subscript gets a value from a json value. If it is not an object containing the key, this returns null (displayed as ()).

There are many functions named to that convert json to various types.
These typically throw an exception if the json value does not look like what is expected. (Though as mentioned above, null is typically allowed as an empty array or object.)
See keen/json to see them.

main void() j json = n: 1 s: "ok" log j["n"] log to::nat j["n"] log to::symbol j["s"] log j["missing-key"] log to::symbol j["n"] # throws