The json API used by rpki-client and bgpctl does pretty-print the json
with every element on a new line. In some cases a more compact output for
some objects makes sense. This is what this diff implements.

This affects both the json output file and also the json filemode.
-- 
:wq Claudio

Index: json.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/json.c,v
retrieving revision 1.2
diff -u -p -r1.2 json.c
--- json.c      3 May 2023 07:56:05 -0000       1.2
+++ json.c      5 Jun 2023 12:58:08 -0000
@@ -38,6 +38,7 @@ enum json_type {
 static struct json_stack {
        const char      *name;
        unsigned int    count;
+       int             compact;
        enum json_type  type;
 } stack[JSON_MAX_STACK];
 
@@ -49,9 +50,18 @@ static FILE *jsonfh;
 static void
 do_comma_indent(void)
 {
-       if (stack[level].count++ > 0)
+       char sp = '\n';
+
+       if (stack[level].compact)
+               sp = ' ';
+
+       if (stack[level].count++ > 0) {
                if (!eb)
-                       eb = fprintf(jsonfh, ",\n") < 0;
+                       eb = fprintf(jsonfh, ",%c", sp) < 0;
+       }
+
+       if (stack[level].compact)
+               return;
        if (!eb)
                eb = fprintf(jsonfh, "\t%.*s", level, indent) < 0;
 }
@@ -107,6 +117,7 @@ void
 json_do_array(const char *name)
 {
        int i, l;
+       char sp = '\n';
 
        if ((l = do_find(ARRAY, name)) > 0) {
                /* array already in use, close element and move on */
@@ -118,10 +129,12 @@ json_do_array(const char *name)
        if (stack[level].type == ARRAY)
                json_do_end();
 
+       if (stack[level].compact)
+               sp = ' ';
        do_comma_indent();
        do_name(name);
        if (!eb)
-               eb = fprintf(jsonfh, "[\n") < 0;
+               eb = fprintf(jsonfh, "[%c", sp) < 0;
 
        if (++level >= JSON_MAX_STACK)
                errx(1, "json stack too deep");
@@ -129,12 +142,15 @@ json_do_array(const char *name)
        stack[level].name = name;
        stack[level].type = ARRAY;
        stack[level].count = 0;
+       /* inherit compact setting from above level */
+       stack[level].compact = stack[level - 1].compact;
 }
 
 void
-json_do_object(const char *name)
+json_do_object(const char *name, int compact)
 {
        int i, l;
+       char sp = '\n';
 
        if ((l = do_find(OBJECT, name)) > 0) {
                /* roll back to that object and close it */
@@ -142,10 +158,12 @@ json_do_object(const char *name)
                        json_do_end();
        }
 
+       if (compact)
+               sp = ' ';
        do_comma_indent();
        do_name(name);
        if (!eb)
-               eb = fprintf(jsonfh, "{\n") < 0;
+               eb = fprintf(jsonfh, "{%c", sp) < 0;
 
        if (++level >= JSON_MAX_STACK)
                errx(1, "json stack too deep");
@@ -153,23 +171,33 @@ json_do_object(const char *name)
        stack[level].name = name;
        stack[level].type = OBJECT;
        stack[level].count = 0;
+       stack[level].compact = compact;
 }
 
 void
 json_do_end(void)
 {
-       if (stack[level].type == ARRAY) {
-               if (!eb)
-                       eb = fprintf(jsonfh, "\n%.*s]", level, indent) < 0;
-       } else if (stack[level].type == OBJECT) {
+       char c;
+
+       if (stack[level].type == ARRAY)
+               c = ']';
+       else if (stack[level].type == OBJECT)
+               c = '}';
+       else
+               errx(1, "json bad stack state");
+
+       if (!stack[level].compact) {
                if (!eb)
-                       eb = fprintf(jsonfh, "\n%.*s}", level, indent) < 0;
+                       eb = fprintf(jsonfh, "\n%.*s%c", level, indent, c) < 0;
        } else {
-               errx(1, "json bad stack state");
+               if (!eb)
+                       eb = fprintf(jsonfh, " %c", c) < 0;
        }
+
        stack[level].name = NULL;
        stack[level].type = NONE;
        stack[level].count = 0;
+       stack[level].compact = 0;
 
        if (level-- <= 0)
                errx(1, "json stack underflow");
Index: json.h
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/json.h,v
retrieving revision 1.2
diff -u -p -r1.2 json.h
--- json.h      3 May 2023 07:56:05 -0000       1.2
+++ json.h      5 Jun 2023 11:08:30 -0000
@@ -22,7 +22,7 @@
 void   json_do_start(FILE *);
 int    json_do_finish(void);
 void   json_do_array(const char *);
-void   json_do_object(const char *);
+void   json_do_object(const char *, int);
 void   json_do_end(void);
 void   json_do_printf(const char *, const char *, ...)
            __attribute__((__format__ (printf, 2, 3)));
Index: output-json.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/output-json.c,v
retrieving revision 1.38
diff -u -p -r1.38 output-json.c
--- output-json.c       26 May 2023 14:57:38 -0000      1.38
+++ output-json.c       5 Jun 2023 11:11:12 -0000
@@ -37,7 +37,7 @@ outputheader_json(struct stats *st)
 
        gethostname(hn, sizeof hn);
 
-       json_do_object("metadata");
+       json_do_object("metadata", 0);
 
        json_do_string("buildmachine", hn);
        json_do_string("buildtime", tbuf);
@@ -87,7 +87,7 @@ print_vap(struct vap *v, enum afi afi)
        size_t i;
        int found = 0;
 
-       json_do_object("aspa");
+       json_do_object("aspa", 1);
        json_do_int("customer_asid", v->custasid);
        json_do_int("expires", v->expires);
 
@@ -108,7 +108,7 @@ output_aspa(struct vap_tree *vaps)
 {
        struct vap      *v;
 
-       json_do_object("provider_authorizations");
+       json_do_object("provider_authorizations", 0);
 
        json_do_array("ipv4");
        RB_FOREACH(v, vap_tree, vaps) {
@@ -138,7 +138,7 @@ output_json(FILE *out, struct vrp_tree *
        RB_FOREACH(v, vrp_tree, vrps) {
                ip_addr_print(&v->addr, v->afi, buf, sizeof(buf));
 
-               json_do_object("roa");
+               json_do_object("roa", 1);
                json_do_int("asn", v->asid);
                json_do_string("prefix", buf);
                json_do_int("maxLength", v->maxlength);
@@ -150,7 +150,7 @@ output_json(FILE *out, struct vrp_tree *
 
        json_do_array("bgpsec_keys");
        RB_FOREACH(b, brk_tree, brks) {
-               json_do_object("brks");
+               json_do_object("brks", 0);
                json_do_int("asn", b->asid);
                json_do_string("ski", b->ski);
                json_do_string("pubkey", b->pubkey);
Index: print.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/print.c,v
retrieving revision 1.39
diff -u -p -r1.39 print.c
--- print.c     30 May 2023 12:02:22 -0000      1.39
+++ print.c     5 Jun 2023 11:13:38 -0000
@@ -161,7 +161,7 @@ as_resources_print(struct cert_as *as, s
 
        for (i = 0; i < asz; i++) {
                if (outformats & FORMAT_JSON)
-                       json_do_object("resource");
+                       json_do_object("resource", 1);
                switch (as[i].type) {
                case CERT_AS_ID:
                        if (outformats & FORMAT_JSON) {
@@ -183,7 +183,7 @@ as_resources_print(struct cert_as *as, s
                        break;
                case CERT_AS_RANGE:
                        if (outformats & FORMAT_JSON) {
-                               json_do_object("asrange");
+                               json_do_object("asrange", 1);
                                json_do_uint("min", as[i].range.min);
                                json_do_uint("max", as[i].range.max);
                                json_do_end();
@@ -212,7 +212,7 @@ ip_resources_print(struct cert_ip *ips, 
 
        for (i = 0; i < ipsz; i++) {
                if (outformats & FORMAT_JSON)
-                       json_do_object("resource");
+                       json_do_object("resource", 1);
                switch (ips[i].type) {
                case CERT_IP_INHERIT:
                        if (outformats & FORMAT_JSON) {
@@ -240,7 +240,7 @@ ip_resources_print(struct cert_ip *ips, 
                        inet_ntop(sockt, ips[i].min, buf1, sizeof(buf1));
                        inet_ntop(sockt, ips[i].max, buf2, sizeof(buf2));
                        if (outformats & FORMAT_JSON) {
-                               json_do_object("ip_range");
+                               json_do_object("ip_range", 1);
                                json_do_string("min", buf1);
                                json_do_string("max", buf2);
                                json_do_end();
@@ -376,7 +376,7 @@ crl_print(const struct crl *p)
                x509_get_time(X509_REVOKED_get0_revocationDate(rev), &t);
                if (serial != NULL) {
                        if (outformats & FORMAT_JSON) {
-                               json_do_object("cert");
+                               json_do_object("cert", 1);
                                json_do_string("serial", serial);
                                json_do_string("date", time2str(t));
                                json_do_end();
@@ -436,7 +436,7 @@ mft_print(const X509 *x, const struct mf
                        errx(1, "base64_encode failure");
 
                if (outformats & FORMAT_JSON) {
-                       json_do_object("filehash");
+                       json_do_object("filehash", 1);
                        json_do_string("filename", p->files[i].file);
                        json_do_string("hash", hash);
                        json_do_end();
@@ -495,7 +495,7 @@ roa_print(const X509 *x, const struct ro
                    p->ips[i].afi, buf, sizeof(buf));
 
                if (outformats & FORMAT_JSON) {
-                       json_do_object("vrp");
+                       json_do_object("vrp", 1);
                        json_do_string("prefix", buf);
                        json_do_uint("asid", p->asid);
                        json_do_uint("maxlen", p->ips[i].maxlength);
@@ -591,7 +591,7 @@ rsc_print(const X509 *x, const struct rs
                        errx(1, "base64_encode failure");
 
                if (outformats & FORMAT_JSON) {
-                       json_do_object("filehash");
+                       json_do_object("filehash", 1);
                        if (p->files[i].filename)
                                json_do_string("filename",
                                    p->files[i].filename);
@@ -616,7 +616,7 @@ static void
 aspa_provider(uint32_t as, enum afi afi)
 {
        if (outformats & FORMAT_JSON) {
-               json_do_object("aspa");
+               json_do_object("aspa", 1);
                json_do_uint("asid", as);
                if (afi == AFI_IPV4)
                        json_do_string("afi_limit", "ipv4");
@@ -713,7 +713,7 @@ takey_print(char *name, const struct tak
                errx(1, "base64_encode failed in %s", __func__);
 
        if (outformats & FORMAT_JSON) {
-               json_do_object("takey");
+               json_do_object("takey", 0);
                json_do_string("name", name);
                json_do_array("comments");
                for (i = 0; i < t->commentsz; i++)
@@ -824,7 +824,7 @@ geofeed_print(const X509 *x, const struc
                ip_addr_print(&p->geoips[i].ip->ip, p->geoips[i].ip->afi, buf,
                    sizeof(buf));
                if (outformats & FORMAT_JSON) {
-                       json_do_object("geoip");
+                       json_do_object("geoip", 1);
                        json_do_string("prefix", buf);
                        json_do_string("location", p->geoips[i].loc);
                        json_do_end();

Reply via email to