Trame

json: JSON read and written

tjson :: #import "trame:json" (or the relative path of the submodule). Pure Formula, any target. The design is in src/md/features/json.md.

Parse with std:encoding/json (json.parse_string(text, parse_integers = true)); read the value with the path reads of this package; write JSON with its quoter and its writer.

Reads

A path is a list of steps: a string is a key of an object, an int an index of an array. A missing key, an index out of range, a step into a value that has no such step, or a value of another type gives the default. No read panics.

ProcedureResult
get(v, ..path) -> json.Valuethe value; nil (no variant) when the path is missing, json.Null for an explicit null
has(v, ..path) -> boolwhether the path names a value (a null is one)
text(v, ..path, default = "") -> stringa string
number(v, ..path, default = 0.0) -> f64an integer or a float
integer(v, ..path, default = 0) -> i64an integer, or a float that is whole and in the range of i64 (3.0)
boolean(v, ..path, default = false) -> boola boolean
array(v, ..path) -> json.Arrayan array, or nil (a for over it does nothing)
object(v, ..path) -> json.Objectan object, or nil
Formula
v, err := json.parse_string(body, parse_integers = true, allocator = context.temp_allocator)
status := tjson.text(v, "status", default = "idle")
first := tjson.text(v, "data", 0, "id")
for it in tjson.array(v, "data") {
	started := tjson.number(it, "time", "started")
}
if _, is_null := tjson.get(v, "exit").(json.Null); is_null { /* sent as null */ }

The quoter

ProcedureResult
quote(s, allocator = context.temp_allocator) -> strings as a JSON string literal, quotes included (temp memory by default: it goes into a request's body, made at once with fmt.tprintf)
quote_into(b, s)the same, written to a strings.Builder
write_float(b, f)f in its shortest form that reads back to the same bits; null for NaN and the infinities

The escapes: " and \; \b \f \n \r \t; the other control characters and DEL as \u00XX (upper-case hex). Text past ASCII stays as UTF-8. A byte that is no UTF-8 is written as U+FFFD, so the output is always valid UTF-8. The same escapes are valid in a TOML basic string. The runs with nothing to escape are copied whole, 16 bytes at a time.

write_float writes a whole float with no decimal point (2). A reader that keeps the type of a number needs .0 added (config's export adds it).

The writer

Writer writes one document to a strings.Builder. The program begins and ends the objects and arrays and gives each member its key before its value; the writer places the commas, the colons and, with an indent, the line breaks.

ProcedureResult
writer_make(b, indent = "") -> Writera writer to b; with an indent (" "), one item per line and ": " after a key
begin_object(w), end_object(w){ and }; an empty object is {}
begin_array(w), end_array(w)[ and ]; an empty array is []
key(w, k)the key of the next member of the current object
write_text(w, s)a string (quote_into)
write_integer(w, n)an i64
write_number(w, f)an f64 (write_float)
write_boolean(w, on), write_null(w)true / false, null
write_raw(w, json_text)JSON text the caller made, as it is
write_value(w, v)a parsed json.Value, whole; the keys of an object sorted
to_text(v, indent = "", allocator = context.temp_allocator) -> stringa parsed value as JSON text
display(v, words = DISPLAY_WORDS, indent = " ", allocator = context.temp_allocator) -> stringa value as a person reads it: a string without quotes, a number as written, a boolean and null as words say (Display_Words{yes, no, null}, the program's language), an object or an array as indented JSON
Formula
b := strings.builder_make(context.temp_allocator)
w := tjson.writer_make(&b)
tjson.begin_object(&w)
tjson.key(&w, "sessionID"); tjson.write_text(&w, id)
tjson.key(&w, "parts")
tjson.begin_array(&w)
for p in parts { tjson.write_text(&w, p) }
tjson.end_array(&w)
tjson.end_object(&w)
// {"sessionID":"ses_1","parts":["a","b"]}

The writer does not check the grammar: a key in an array or a member with no key is the program's error. It tracks WRITER_DEPTH_MAX (64) levels of nesting and asserts past them.

Edit this page on GitHub