Esc

to_json_value

procedureextensionsince 0.36.1Source
  • macOS: to_json_value works on macOS
  • Linux: to_json_value works on Linux
  • Windows: to_json_value works on Windows
  • Wasm: to_json_value works on Wasm

Encodes value into a Json_Value, the inverse of from_json on a value: the tree holds what parsing to_json's output gives, so it can be edited, stored in a larger tree with json_set, and decoded again. The generated encoder builds the tree directly (a Json_Writer in tree mode), so nothing is written out and parsed. Strings are copies, with invalid UTF-8 replaced as the text encoder does. Release the tree with free_json.

Signature

to_json_value :: (value: $T) -> Json_Value
Example↗
Point :: struct {
    x: s32;
    y: s32;
}

tree := to_json_value(Point.{ 3, 4 });
assert(json_count(*tree) == 2);
x, _ := json_as_s64(json_get(*tree, "x"));
assert(x == 3);

json_set(*tree, "y", json_int(9));
point, _ := from_json(Point, *tree);
assert(point.y == 9);
free_json(*tree);

See also