From 8f79bdd128e7ffcd471ad5f00e33a49619c41e6e Mon Sep 17 00:00:00 2001 From: Kaz Kylheku 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. --- lib.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'lib.c') 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)) { -- cgit v1.2.3