summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-07-12 01:32:47 -0700
committerKaz Kylheku <kaz@kylheku.com>2024-07-12 01:32:47 -0700
commit7dbdc52b4351a44b5d5e18a2fa73ec2f2c16d4c2 (patch)
tree6452046e87f6e1962857ba353849970fe7820208 /lib.c
parentb2bb2af765a550efd92adb25687cb76f955574e3 (diff)
downloadtxr-7dbdc52b4351a44b5d5e18a2fa73ec2f2c16d4c2.tar.gz
txr-7dbdc52b4351a44b5d5e18a2fa73ec2f2c16d4c2.tar.bz2
txr-7dbdc52b4351a44b5d5e18a2fa73ec2f2c16d4c2.zip
json: new special var *print-json-type*.
This variable controls whether we emit the "__type" key for structures. * lib.c (out_json_rec): React to the new variable, via the flag in the json_opts structure: include the "__type" key only if it is requested. (out_json, put_json): Initialize the type flag in the josn_opts according to the *print-json-type* dynamic variable. * stream.c (print_json_type_s): New symbol variable. (stream_init): print_json_type_s initialized, and corresponding special variable registered, with intial value t. * stream.h (struct json_opts): New bitfield member, type. (print_json_type_s): Declared. * txr.1: Documented.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c33
1 files changed, 25 insertions, 8 deletions
diff --git a/lib.c b/lib.c
index 3937fcc4..009e2596 100644
--- a/lib.c
+++ b/lib.c
@@ -15088,12 +15088,17 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
val iter;
val save_indent;
int force_br = 0;
+ int first = 1;
if (!jo.flat && jo.fmt == json_fmt_standard) {
put_string(lit("{\n"), out);
save_indent = inc_indent_abs(out, two);
- put_string(lit("\"__type\" : "), out);
- out_json_sym(ty, out, ctx);
+
+ if (jo.type) {
+ put_string(lit("\"__type\" : "), out);
+ out_json_sym(ty, out, ctx);
+ first = 0;
+ }
for (iter = sl; iter; iter = cdr(iter)) {
val slsym = car(iter);
@@ -15101,7 +15106,9 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
if (static_slot_p(ty, slsym))
continue;
- put_string(lit(",\n"), out);
+ if (!first)
+ put_string(lit(",\n"), out);
+ first = 0;
out_json_sym(slsym, out, ctx);
put_string(lit(" : "), out);
out_json_rec(slot(obj, slsym), out, jo, ctx);
@@ -15110,8 +15117,12 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
} else {
put_char(chr('{'), out);
save_indent = inc_indent(out, zero);
- put_string(lit("\"__type\":"), out);
- out_json_sym(ty, out, ctx);
+
+ if (jo.type) {
+ put_string(lit("\"__type\":"), out);
+ out_json_sym(ty, out, ctx);
+ first = 0;
+ }
for (iter = sl; iter; iter = cdr(iter)) {
val slsym = car(iter);
@@ -15119,7 +15130,9 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
if (static_slot_p(ty, slsym))
continue;
- put_string(lit(","), out);
+ if (!first)
+ put_string(lit(","), out);
+ first = 0;
if (width_check(out, nil))
force_br = 1;
out_json_sym(slsym, out, ctx);
@@ -15158,9 +15171,11 @@ static void out_json(val op, val obj, val out, struct strm_ctx *ctx)
val save_mode = test_set_indent_mode(out, num_fast(indent_off),
num_fast(indent_data));
val jfsym = cdr(lookup_var(nil, print_json_format_s));
+ val type = cdr(lookup_var(nil, print_json_type_s));
struct json_opts jo = {
if3(jfsym == standard_k, json_fmt_standard, json_fmt_default),
- 0
+ 0,
+ type != nil
};
if (op == sys_qquote_s)
put_char(chr('^'), out);
@@ -15861,9 +15876,11 @@ val put_json(val obj, val stream_in, val flat)
num_fast(indent_data)));
val isave = get_indent(stream);
val jfsym = cdr(lookup_var(nil, print_json_format_s));
+ val type = cdr(lookup_var(nil, print_json_type_s));
struct json_opts jo = {
if3(jfsym == standard_k, json_fmt_standard, json_fmt_default),
- flat != nil
+ flat != nil,
+ type != nil
};
uw_simple_catch_begin;
out_json_rec(obj, stream, jo, 0);