summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2025-01-16 19:17:42 -0800
committerKaz Kylheku <kaz@kylheku.com>2025-01-16 19:17:42 -0800
commitfce395ac290830a9a4c474f40d22376317749559 (patch)
treeaa416a1864c08b3a50232277b25cb811048a163e
parent362666bc7ad99b2747058472d6f47e2972e9e7af (diff)
downloadtxr-fce395ac290830a9a4c474f40d22376317749559.tar.gz
txr-fce395ac290830a9a4c474f40d22376317749559.tar.bz2
txr-fce395ac290830a9a4c474f40d22376317749559.zip
json: flat must override all effects of :standard
* stream.h (struct json_opts): Member flat removed. I noticed that !jo.flat was always being tested together with jo.fmt == json_fmt_standard. Except for a few places where the code only tested for json_fmt_standard, resulting in flat output, but some extra spaces. What distiguishes flat mode now is simply that we disable stream indentation. * lib.c (out_json_rec): Remove tests for !jo.flat. (out_json): Remove initialization of jo.flat member. In this function we set up indentation on the stream resulting in multi-line mode (existing behavior). (put_json): Remove initialization of jo.flat member. If flat mode is requested, then it overrides the format to json_fmt_default. I.e. json_fmt_standard coresponding to :standard is only in effect if flat is not requested. In this function we set up indentation on the stream if flat mode isn't requested, otherwise we disable indentation (existing behavior, enough to make flat work). * tests/010/json.tl: Tests for flat mode, :standard formatting, and combinaton of both.
-rw-r--r--lib.c24
-rw-r--r--stream.h1
-rw-r--r--tests/010/json.tl29
3 files changed, 40 insertions, 14 deletions
diff --git a/lib.c b/lib.c
index 32b701a7..8b0ce7e9 100644
--- a/lib.c
+++ b/lib.c
@@ -14897,7 +14897,7 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
int force_br = 0;
val iter, next;
- if (!jo.flat && jo.fmt == json_fmt_standard) {
+ if (jo.fmt == json_fmt_standard) {
put_string(lit("{\n"), out);
save_indent = inc_indent_abs(out, two);
} else {
@@ -14919,7 +14919,7 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
}
out_json_rec(v, out, jo, ctx);
- if (!jo.flat && jo.fmt == json_fmt_standard) {
+ if (jo.fmt == json_fmt_standard) {
if ((next = cdr(iter)) != 0)
put_string(lit(",\n"), out);
else
@@ -14941,7 +14941,7 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
int force_br = 0;
val iter, next;
- if (!jo.flat && jo.fmt == json_fmt_standard) {
+ if (jo.fmt == json_fmt_standard) {
put_string(lit("[\n"), out);
save_indent = inc_indent_abs(out, two);
} else {
@@ -14953,7 +14953,7 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
val elem = car(iter);
next = cdr(iter);
out_json_rec(elem, out, jo, ctx);
- if (!jo.flat && jo.fmt == json_fmt_standard) {
+ if (jo.fmt == json_fmt_standard) {
if (next)
put_string(lit(",\n"), out);
else
@@ -14990,7 +14990,7 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
val elem;
seq_iter_init(self, &si, obj);
- if (!jo.flat && jo.fmt == json_fmt_standard) {
+ if (jo.fmt == json_fmt_standard) {
put_string(lit("[\n"), out);
save_indent = inc_indent_abs(out, two);
} else {
@@ -15002,7 +15002,7 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
val nxelem;
int more = seq_get(&si, &nxelem);
out_json_rec(elem, out, jo, ctx);
- if (!jo.flat && jo.fmt == json_fmt_standard) {
+ if (jo.fmt == json_fmt_standard) {
if (more)
put_string(lit(",\n"), out);
else
@@ -15032,7 +15032,7 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
us_hash_iter_init(&hi, obj);
- if (!jo.flat && jo.fmt == json_fmt_standard) {
+ if (jo.fmt == json_fmt_standard) {
put_string(lit("{\n"), out);
save_indent = inc_indent_abs(out, two);
} else {
@@ -15053,14 +15053,14 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
put_char(chr(':'), out);
}
out_json_rec(v, out, jo, ctx);
- if (!jo.flat && jo.fmt == json_fmt_standard) {
+ if (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 (!jo.flat && jo.fmt == json_fmt_standard)
+ if (jo.fmt == json_fmt_standard)
put_char(chr('\n'), out);
else if (width_check(out, nil))
force_br = 1;
@@ -15080,7 +15080,7 @@ static void out_json_rec(val obj, val out, struct json_opts jo,
int force_br = 0;
int first = 1;
- if (!jo.flat && jo.fmt == json_fmt_standard) {
+ if (jo.fmt == json_fmt_standard) {
put_string(lit("{\n"), out);
save_indent = inc_indent_abs(out, two);
@@ -15164,7 +15164,6 @@ static void out_json(val op, val obj, val out, struct strm_ctx *ctx)
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,
type != nil
};
if (op == sys_qquote_s)
@@ -15869,8 +15868,7 @@ val put_json(val obj, val stream_in, val flat_in)
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,
+ if3(jfsym == standard_k && !flat, json_fmt_standard, json_fmt_default),
type != nil
};
uw_simple_catch_begin;
diff --git a/stream.h b/stream.h
index a2a25073..a0042933 100644
--- a/stream.h
+++ b/stream.h
@@ -141,7 +141,6 @@ enum json_fmt {
struct json_opts {
enum json_fmt fmt : 4;
- unsigned flat : 1;
unsigned type : 1;
};
diff --git a/tests/010/json.tl b/tests/010/json.tl
index b7117d8a..57c9a8c5 100644
--- a/tests/010/json.tl
+++ b/tests/010/json.tl
@@ -207,3 +207,32 @@
(get-json "0") 0.0
(get-json "12345") 12345.0
(get-json "12345678900000000000000000") 1.23456789E25)
+
+(let ((obj #H(() ("a" #(1 2 3))
+ ("b" #H(() ("c" "d")
+ ("e" t) ("f" nil) ("g" #(4 5 6)))))))
+ (mtest
+ (tojson obj) "{\"a\":[1,2,3],\"b\":{\"c\":\"d\",\"f\":false,\"g\":[4,5,6],\"e\":true}}"
+ (tojson obj t) "{\"a\":[1,2,3],\"b\":{\"c\":\"d\",\"f\":false,\"g\":[4,5,6],\"e\":true}}"
+ (let ((*print-json-format* :standard))
+ (tojson obj))
+ "{\n \
+ \ \"a\" : [\n\
+ \ 1,\n\
+ \ 2,\n\
+ \ 3\n\
+ \ ],\n\
+ \ \"b\" : {\n\
+ \ \"c\" : \"d\",\n\
+ \ \"f\" : false,\n\
+ \ \"g\" : [\n\
+ \ 4,\n\
+ \ 5,\n\
+ \ 6\n\
+ \ ],\n\
+ \ \"e\" : true\n\
+ \ }\n\
+ }"
+ (let ((*print-json-format* :standard))
+ (tojson obj t))
+ "{\"a\":[1,2,3],\"b\":{\"c\":\"d\",\"f\":false,\"g\":[4,5,6],\"e\":true}}"))