From 8f79bdd128e7ffcd471ad5f00e33a49619c41e6e Mon Sep 17 00:00:00 2001
From: Kaz Kylheku <kaz@kylheku.com>
Date: Fri, 28 May 2021 21:00:29 -0700
Subject: json: tojson function.

* eval.c (eval_init): tojson intrinsic registered.

* lib.c (tojson): New function.

* lib.h (tojson): Declared.

* txr.1: Documented.

* share/txr/stdlib/doc-syms.tl: Updated.
---
 txr.1 | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

(limited to 'txr.1')

diff --git a/txr.1 b/txr.1
index 94a88cc1..dffd9b4b 100644
--- a/txr.1
+++ b/txr.1
@@ -71808,6 +71808,72 @@ etc.
 
 .SS* Data Interchange Support
 
+.coNP Function @ tojson
+.synb
+.mets (tojson < obj <> [ flat-p ])
+.syne
+.desc
+The
+.code tojson
+function converts
+.meta obj
+into JSON notation, returned as a character string.
+
+If
+.meta obj
+is
+.code json
+macro syntax generated by the parser, or else generated programmatically in a compatible way,
+then this is converted back into JSON syntax.
+
+Otherwise if
+.meta obj
+conforms to the conventions by which JSON objects are represented in Lisp,
+it is converted to JSON syntax also.
+
+The behavior is unspecified if
+.meta obj
+or any component of
+.meta obj
+is an object incompatible with the JSON representation conventions.
+An exception may be thrown.
+
+An object conforms to the JSON representation conventions if:
+.RS
+.IP 1.
+It is one of the symbols
+.codn nil ,
+.code t
+or
+.codn null ,
+which map to the JSON keywords
+.codn false ,
+.code true
+and
+.codn null ,
+respectively.
+.IP 2.
+It is a floating-point number.
+.IP 3.
+It is a character string.
+.IP 4.
+It is a vector of JSON-conforming objects.
+.IP 5.
+It is a hash table whose keys and values are JSON-conforming objects.
+.RE
+.IP
+Note that if unless the keys in a hash table are all strings, nonstandard JSON
+is produced, since RFC 8259 requires JSON object keys to be strings.
+
+If the
+.code flat-p
+argument is present and has a true value, then the JSON is generated
+without any line breaks or indentation.
+
+Otherwise, the JSON is potentially subject to such formatting.
+
+Even if the JSON data contains line breaks, it does not end in a line break.
+
 .coNP Macro @ json
 .synb
 .mets (json [quote | sys:qquote] << object )
-- 
cgit v1.2.3