diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2022-10-27 20:19:21 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2022-10-27 20:19:21 -0400 |
| commit | 69740ed659fd8aae986bc2f269ee100a579b5efd (patch) | |
| tree | 8e6cc8613108f6e990292c154e2301668e54c8f6 /json.c | |
| parent | 07f335f36719bcb48a0f2f6a1501054852b53f28 (diff) | |
Fix json output
Diffstat (limited to 'json.c')
| -rw-r--r-- | json.c | 16 |
1 files changed, 12 insertions, 4 deletions
@@ -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("]"); |
