aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2022-10-27 20:19:21 -0400
committerBruce Hill <bruce@bruce-hill.com>2022-10-27 20:19:21 -0400
commit69740ed659fd8aae986bc2f269ee100a579b5efd (patch)
tree8e6cc8613108f6e990292c154e2301668e54c8f6
parent07f335f36719bcb48a0f2f6a1501054852b53f28 (diff)
Fix json output
-rw-r--r--json.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/json.c b/json.c
index e3731d5..17533ca 100644
--- a/json.c
+++ b/json.c
@@ -27,10 +27,13 @@ static int _json_match(const char *text, match_t *m, int comma, bool verbose)
if (comma) printf(",\n");
comma = 0;
printf("{");
- if (m->pat->type == BP_TAGGED)
+ if (m->pat->type == BP_TAGGED) {
printf("\"tag\":\"%.*s\"", (int)m->pat->args.capture.namelen, m->pat->args.capture.name);
+ comma = 1;
+ }
if (verbose) {
- printf(",\"rule\":\"");
+ if (comma) printf(",");
+ printf("\"rule\":\"");
for (const char *c = m->pat->start; c < m->pat->end; c++) {
switch (*c) {
case '"': printf("\\\""); break;
@@ -42,8 +45,10 @@ static int _json_match(const char *text, match_t *m, int comma, bool verbose)
}
printf("\",");
printf("\"range\":[%ld,%ld]", m->start - text, m->end - text);
+ comma = 1;
} else {
- printf(",\"text\":\"");
+ if (comma) printf(",");
+ printf("\"text\":\"");
for (const char *c = m->start; c < m->end; c++) {
switch (*c) {
case '"': printf("\\\""); break;
@@ -54,10 +59,13 @@ static int _json_match(const char *text, match_t *m, int comma, bool verbose)
}
}
printf("\"");
+ comma = 1;
}
if (m->children) {
- printf(",\"children\":[");
+ if (comma) printf(",");
+ printf("\"children\":[");
+ comma = 0;
for (int i = 0; m->children && m->children[i]; i++)
comma |= _json_match(text, m->children[i], comma, verbose);
printf("]");