summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-05-28 21:00:29 -0700
committerKaz Kylheku <kaz@kylheku.com>2021-05-28 21:00:29 -0700
commit8f79bdd128e7ffcd471ad5f00e33a49619c41e6e (patch)
tree5b84236d77340fa0001d81064e33392e9410b325 /lib.c
parent936314c70f9356db993cfbe781919026e2df042c (diff)
downloadtxr-8f79bdd128e7ffcd471ad5f00e33a49619c41e6e.tar.gz
txr-8f79bdd128e7ffcd471ad5f00e33a49619c41e6e.tar.bz2
txr-8f79bdd128e7ffcd471ad5f00e33a49619c41e6e.zip
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.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index e1879df6..75cbc739 100644
--- a/lib.c
+++ b/lib.c
@@ -13454,6 +13454,27 @@ val tostringp(val obj)
return get_string_from_stream(ss);
}
+val tojson(val obj, val flat)
+{
+ if (consp(obj) && eq(car(obj), json_s)) {
+ val ss = make_string_output_stream();
+ if (default_null_arg(flat))
+ set_indent_mode(ss, num_fast(indent_foff));
+ obj_print(obj, ss, nil);
+ return get_string_from_stream(ss);
+ } else {
+ val json = cons(json_s, cons(quote_s, cons(obj, nil)));
+ val ss = make_string_output_stream();
+ if (default_null_arg(flat))
+ set_indent_mode(ss, num_fast(indent_foff));
+ obj_print(json, ss, nil);
+ rcyc_pop(&json);
+ rcyc_pop(&json);
+ rcyc_pop(&json);
+ return get_string_from_stream(ss);
+ }
+}
+
val display_width(val obj)
{
if (stringp(obj)) {