diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2024-07-06 21:34:42 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2024-07-06 21:34:42 -0700 |
commit | c0ab748e25e8f283fbc37f0a8e22e9bda7f4207c (patch) | |
tree | 624b4844ec6c11b84dbff927f3a14e20dca2ee3b | |
parent | f2f9302bb016a03c19ec08364272ddd705ee4ea8 (diff) | |
download | txr-c0ab748e25e8f283fbc37f0a8e22e9bda7f4207c.tar.gz txr-c0ab748e25e8f283fbc37f0a8e22e9bda7f4207c.tar.bz2 txr-c0ab748e25e8f283fbc37f0a8e22e9bda7f4207c.zip |
json: fix flat-p argument in put-json and put-jsons.
The flat-p flag is not being passed through ther recursion.
Some of the formatting code emits newlines unconditionally.
Instead of passing down the enum json_fmt, we pass down a new
structure which carries the flat-p flag also.
* stream.h (struct json_opts): New struct.
* lib.c (out_json_rec): Take a struct json_opts argument
instead of enum json_fmt. Handle the flat flag to avoid
generating line breaks.
(out_json, put_json): Prepare json_opt structure and pass to
out_json_rec.
-rw-r--r-- | lib.c | 68 | ||||
-rw-r--r-- | stream.h | 5 |
2 files changed, 40 insertions, 33 deletions
@@ -14830,7 +14830,7 @@ static void out_json_sym(val sym, val out, struct strm_ctx *ctx) put_char(chr('"'), out); } -static void out_json_rec(val obj, val out, enum json_fmt jf, +static void out_json_rec(val obj, val out, struct json_opts jo, struct strm_ctx *ctx) { val self = lit("print"); @@ -14861,7 +14861,7 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, int force_br = 0; val iter, next; - if (jf == json_fmt_standard) { + if (!jo.flat && jo.fmt == json_fmt_standard) { put_string(lit("{\n"), out); save_indent = inc_indent_abs(out, two); } else { @@ -14872,18 +14872,18 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, for (iter = cddr(obj), next = nil; iter; iter = next) { val pair = car(iter); val k = car(pair), v = cadr(pair); - if (jf == json_fmt_standard || consp(k) || consp(v)) { - if (jf != json_fmt_standard && next) + if (jo.fmt == json_fmt_standard || consp(k) || consp(v)) { + if (jo.fmt != json_fmt_standard && next) put_char(chr(' '), out); - out_json_rec(k, out, jf, ctx); + out_json_rec(k, out, jo, ctx); put_string(lit(" : "), out); } else { - out_json_rec(k, out, jf, ctx); + out_json_rec(k, out, jo, ctx); put_char(chr(':'), out); } - out_json_rec(v, out, jf, ctx); + out_json_rec(v, out, jo, ctx); - if (jf == json_fmt_standard) { + if (!jo.flat && jo.fmt == json_fmt_standard) { if ((next = cdr(iter)) != 0) put_string(lit(",\n"), out); else @@ -14905,7 +14905,7 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, int force_br = 0; val iter, next; - if (jf == json_fmt_standard) { + if (!jo.flat && jo.fmt == json_fmt_standard) { put_string(lit("[\n"), out); save_indent = inc_indent_abs(out, two); } else { @@ -14916,8 +14916,8 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, for (iter = cadr(obj), next = nil; iter; iter = next) { val elem = car(iter); next = cdr(iter); - out_json_rec(elem, out, jf, ctx); - if (jf == json_fmt_standard) { + out_json_rec(elem, out, jo, ctx); + if (!jo.flat && jo.fmt == json_fmt_standard) { if (next) put_string(lit(",\n"), out); else @@ -14954,7 +14954,7 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, val elem; seq_iter_init(self, &si, obj); - if (jf == json_fmt_standard) { + if (!jo.flat && jo.fmt == json_fmt_standard) { put_string(lit("[\n"), out); save_indent = inc_indent_abs(out, two); } else { @@ -14965,8 +14965,8 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, if (seq_get(&si, &elem)) for (;;) { val nxelem; int more = seq_get(&si, &nxelem); - out_json_rec(elem, out, jf, ctx); - if (jf == json_fmt_standard) { + out_json_rec(elem, out, jo, ctx); + if (!jo.flat && jo.fmt == json_fmt_standard) { if (more) put_string(lit(",\n"), out); else @@ -14996,7 +14996,7 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, us_hash_iter_init(&hi, obj); - if (jf == json_fmt_standard) { + if (!jo.flat && jo.fmt == json_fmt_standard) { put_string(lit("{\n"), out); save_indent = inc_indent_abs(out, two); } else { @@ -15007,24 +15007,24 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, for (next = nil, cell = hash_iter_next(&hi); cell; cell = next) { val k = car(cell), v = cdr(cell); - if (jf == json_fmt_standard || consp(k) || consp(v)) { - if (jf != json_fmt_standard && next) + if (jo.fmt == json_fmt_standard || consp(k) || consp(v)) { + if (jo.fmt != json_fmt_standard && next) put_char(chr(' '), out); - out_json_rec(k, out, jf, ctx); + out_json_rec(k, out, jo, ctx); put_string(lit(" : "), out); } else { - out_json_rec(k, out, jf, ctx); + out_json_rec(k, out, jo, ctx); put_char(chr(':'), out); } - out_json_rec(v, out, jf, ctx); - if (jf == json_fmt_standard) { + out_json_rec(v, out, jo, ctx); + if (!jo.flat && jo.fmt == json_fmt_standard) { if ((next = hash_iter_next(&hi)) != 0) put_string(lit(",\n"), out); else put_char(chr('\n'), out); } else if ((next = hash_iter_next(&hi)) != 0) { put_char(chr(','), out); - if (jf == json_fmt_standard) + if (!jo.flat && jo.fmt == json_fmt_standard) put_char(chr('\n'), out); else if (width_check(out, nil)) force_br = 1; @@ -15043,7 +15043,7 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, val save_indent; int force_br = 0; - if (jf == json_fmt_standard) { + 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); @@ -15058,7 +15058,7 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, put_string(lit(",\n"), out); out_json_sym(slsym, out, ctx); put_string(lit(" : "), out); - out_json_rec(slot(obj, slsym), out, jf, ctx); + out_json_rec(slot(obj, slsym), out, jo, ctx); } put_string(lit("\n"), out); } else { @@ -15078,7 +15078,7 @@ static void out_json_rec(val obj, val out, enum json_fmt jf, force_br = 1; out_json_sym(slsym, out, ctx); put_string(lit(":"), out); - out_json_rec(slot(obj, slsym), out, jf, ctx); + out_json_rec(slot(obj, slsym), out, jo, ctx); } } @@ -15112,12 +15112,13 @@ 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)); - enum json_fmt jf = if3(jfsym == standard_k, - json_fmt_standard, - json_fmt_default); + struct json_opts jo = { + if3(jfsym == standard_k, json_fmt_standard, json_fmt_default), + 0 + }; if (op == sys_qquote_s) put_char(chr('^'), out); - out_json_rec(obj, out, jf, ctx); + out_json_rec(obj, out, jo, ctx); set_indent_mode(out, save_mode); } @@ -15814,11 +15815,12 @@ 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)); - enum json_fmt jf = if3(jfsym == standard_k, - json_fmt_standard, - json_fmt_default); + struct json_opts jo = { + if3(jfsym == standard_k, json_fmt_standard, json_fmt_default), + flat != nil + }; uw_simple_catch_begin; - out_json_rec(obj, stream, jf, 0); + out_json_rec(obj, stream, jo, 0); uw_unwind { set_indent_mode(stream, imode); set_indent(stream, isave); @@ -139,6 +139,11 @@ enum json_fmt { json_fmt_standard }; +struct json_opts { + enum json_fmt fmt : 4; + unsigned flat : 1; +}; + loc lookup_var_l(val env, val sym); extern val from_start_k, from_current_k, from_end_k; |