]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
client: flush JSON object once it's done
authorVincent Bernat <vincent@bernat.im>
Thu, 19 May 2016 18:15:24 +0000 (20:15 +0200)
committerVincent Bernat <vincent@bernat.im>
Thu, 19 May 2016 18:15:24 +0000 (20:15 +0200)
This enables `lldpcli -f json watch` to work as expected. See #179.

src/client/jansson_writer.c
src/client/jsonc_writer.c

index 88a1530b4bb257d8e623e3f712c3ff1aae674b76..c14ad8c477caf3aed7532392288e69b3116c7edf 100644 (file)
@@ -89,19 +89,6 @@ jansson_data(struct writer *w, const char *data)
            json_string(data?data:""));
 }
 
-static void
-jansson_end(struct writer *w)
-{
-       struct json_writer_private *p = w->priv;
-       struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
-       if (current == NULL) {
-               log_warnx("lldpctl", "unbalanced tags");
-               return;
-       }
-       TAILQ_REMOVE(&p->els, current, next);
-       free(current);
-}
-
 /* When an array has only one member, just remove the array. When an object has
  * `value` as the only key, remove the object. Moreover, for an object, move the
  * `name` key outside (inside a new object). This is a recursive function. We
@@ -161,6 +148,35 @@ jansson_cleanup(json_t *el)
        return el;
 }
 
+static void
+jansson_end(struct writer *w)
+{
+       struct json_writer_private *p = w->priv;
+       struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
+       if (current == NULL) {
+               log_warnx("lldpctl", "unbalanced tags");
+               return;
+       }
+       TAILQ_REMOVE(&p->els, current, next);
+       free(current);
+
+       /* Display current object if last one */
+       if (TAILQ_NEXT(TAILQ_FIRST(&p->els), next) == NULL) {
+               struct json_element *root = TAILQ_FIRST(&p->els);
+               json_t *export = jansson_cleanup(root->el);
+               if (json_dumpf(export,
+                       p->fh,
+                       JSON_INDENT(2) | JSON_PRESERVE_ORDER) == -1)
+                       log_warnx("lldpctl", "unable to output JSON");
+               fprintf(p->fh,"\n");
+               json_decref(export);
+               json_decref(root->el);
+               root->el = json_object();
+               if (root->el == NULL)
+                       fatalx("lldpctl", "cannot create JSON root object");
+       }
+}
+
 static void
 jansson_finish(struct writer *w)
 {
@@ -171,16 +187,9 @@ jansson_finish(struct writer *w)
                log_warnx("lldpctl", "unbalanced tags");
                /* memory will leak... */
        } else {
-               struct json_element *first = TAILQ_FIRST(&p->els);
-               json_t *export = jansson_cleanup(first->el);
-               if (json_dumpf(export,
-                       p->fh,
-                       JSON_INDENT(2) | JSON_PRESERVE_ORDER) == -1)
-                       log_warnx("lldpctl", "unable to output JSON");
-               json_decref(first->el);
-               json_decref(export);
-               TAILQ_REMOVE(&p->els, first, next);
-               free(first);
+               struct json_element *root = TAILQ_FIRST(&p->els);
+               TAILQ_REMOVE(&p->els, root, next);
+               free(root);
        }
        free(p);
        free(w);
index 8865383008f054f27d7bad5cc75129be0fcef251..90a2513b1d1086929efe417ba109ade362355f04 100644 (file)
@@ -88,19 +88,6 @@ jsonc_data(struct writer *w, const char *data)
            json_object_new_string(data?data:""));
 }
 
-static void
-jsonc_end(struct writer *w)
-{
-       struct json_writer_private *p = w->priv;
-       struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
-       if (current == NULL) {
-               log_warnx("lldpctl", "unbalanced tags");
-               return;
-       }
-       TAILQ_REMOVE(&p->els, current, next);
-       free(current);
-}
-
 /* When an array has only one member, just remove the array. When an object has
  * `value` as the only key, remove the object. Moreover, for an object, move the
  * `name` key outside (inside a new object). This is a recursive function. We
@@ -157,6 +144,33 @@ jsonc_cleanup(json_object *el)
        return el;
 }
 
+static void
+jsonc_end(struct writer *w)
+{
+       struct json_writer_private *p = w->priv;
+       struct json_element *current = TAILQ_LAST(&p->els, json_element_list);
+       if (current == NULL) {
+               log_warnx("lldpctl", "unbalanced tags");
+               return;
+       }
+       TAILQ_REMOVE(&p->els, current, next);
+       free(current);
+
+       /* Display current object if last one */
+       if (TAILQ_NEXT(TAILQ_FIRST(&p->els), next) == NULL) {
+               struct json_element *root = TAILQ_FIRST(&p->els);
+               json_object *export = jsonc_cleanup(root->el);
+               int json_flags = (JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED);
+               const char *s = json_object_to_json_string_ext(export, json_flags);
+               fprintf(p->fh, "%s\n", s?s:"{}");
+               json_object_put(export);
+               json_object_put(root->el);
+               root->el = json_object_new_object();
+               if (root->el == NULL)
+                       fatalx("lldpctl", "cannot create JSON root object");
+       }
+}
+
 static void
 jsonc_finish(struct writer *w)
 {
@@ -167,17 +181,10 @@ jsonc_finish(struct writer *w)
                log_warnx("lldpctl", "unbalanced tags");
                /* memory will leak... */
        } else {
-               struct json_element *first = TAILQ_FIRST(&p->els);
-               json_object *export = jsonc_cleanup(first->el);
-               int json_flags = (JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED);
-               const char *s = json_object_to_json_string_ext(export, json_flags);
-               fprintf(p->fh, "%s", s?s:"{}");
-               json_object_put(first->el);
-               json_object_put(export);
-               TAILQ_REMOVE(&p->els, first, next);
-               free(first);
+               struct json_element *root = TAILQ_FIRST(&p->els);
+               TAILQ_REMOVE(&p->els, root, next);
+               free(root);
        }
-       fprintf(p->fh, "\n");
        free(p);
        free(w);
 }