beetlebug▾
/

ztcl::json encode

Writes a Tcl value as JSON text, with a schema or a typed tree.

ztcl::json encode -schema SCHEMA VALUE
ztcl::json encode -typed TREE
ztcl::json encode -auto VALUE

Description

A Tcl value has no visible type, so encode needs to be told the JSON type of each part: a schema with -schema, a typed tree of parse -typed with -typed, or a guess for each value with -auto. The text has no spaces.

A schema is one of these words: string, int (also above 64 bits), number (Inf and NaN are errors), bool (from each boolean of Tcl), null, raw (the value is JSON text, with no check), object (a dict with a guess for each value), {object SCHEMA} (a dict with a schema for each key, in that order), {list WORD} (a list with WORD for each element), and {optional WORD} (in an object schema: the key can be absent).

A guess makes a number of the canonical text of an integer or a double, a bool of true and false, and a string of each other value. In {object SCHEMA} a missing key and a key that SCHEMA does not name are errors.

Options

OptionTypeDefaultDescription
-schemaschemanoneWrites VALUE with the JSON types of a schema.
-typedtyped treenoneWrites a typed tree of parse -typed.
-autodictnoneWrites a dict with a guess for each value; the same as -schema object.

-schema SCHEMA

Writes VALUE with the JSON types of a schema.

Writes VALUE with the schema words above. A value that does not fit its word is an error with the code ZT JSON VALUE, and a word that is not a schema is an error with the code ZT JSON SCHEMA.

% ztcl::json encode -schema bool yes
true

% ztcl::json encode -schema object {a 1 b x c true}
{"a":1,"b":"x","c":true}
% ztcl::json encode -schema int x
expected an integer but got "x"
-errorcode ZT JSON VALUE

% ztcl::json encode -schema number inf
json has no number "inf"
-errorcode ZT JSON VALUE

% ztcl::json encode -schema bogus 1
bad schema "bogus"
-errorcode ZT JSON SCHEMA

-typed TREE

Writes a typed tree of parse -typed.

Writes TREE, a tree of {object ...}, {array ...}, {string S}, {number TEXT}, {bool B}, and null, as parse -typed gives it.

% ztcl::json encode -typed {object {a {number 1} b {string x}}}
{"a":1,"b":"x"}
% ztcl::json encode -typed {bogus 1}
bad typed value "bogus 1"
-errorcode ZT JSON TYPED

-auto VALUE

Writes a dict with a guess for each value; the same as -schema object.

-auto VALUE is -schema object VALUE: VALUE is a dict, and each value gets the guess above.

% ztcl::json encode -auto {a 1 b x}
{"a":1,"b":"x"}
% ztcl::json encode -auto {a}
missing value to go with key
-errorcode TCL VALUE DICTIONARY

Returns

The JSON text.

Examples

Each example of this page runs after package require ztcl::json.

% ztcl::json encode -schema {object {n int s string}} {n 5 s hi}
{"n":5,"s":"hi"}

% ztcl::json encode -schema {list int} {1 2 3}
[1,2,3]

% ztcl::json encode -schema {object {a {optional int}}} {}
{}

% ztcl::json encode -schema string 5
"5"

Errors

% ztcl::json encode -schema {object {n int}} {}
missing key "n"
-errorcode ZT JSON SCHEMA

% ztcl::json encode -schema {object {n int}} {n 5 m 6}
key "m" is not in the schema
-errorcode ZT JSON SCHEMA

% ztcl::json encode x
wrong # args: should be "ztcl::json encode -schema schema value | -typed tree | -auto value"
-errorcode TCL WRONGARGS

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also