From: Vincent Bernat Date: Thu, 19 May 2016 18:15:24 +0000 (+0200) Subject: client: flush JSON object once it's done X-Git-Tag: 0.9.3~13 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=03c0c1d908bbe15fd8ed9db3b5dcd9dadde547a2;p=thirdparty%2Flldpd.git client: flush JSON object once it's done This enables `lldpcli -f json watch` to work as expected. See #179. --- diff --git a/src/client/jansson_writer.c b/src/client/jansson_writer.c index 88a1530b..c14ad8c4 100644 --- a/src/client/jansson_writer.c +++ b/src/client/jansson_writer.c @@ -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); diff --git a/src/client/jsonc_writer.c b/src/client/jsonc_writer.c index 88653830..90a2513b 100644 --- a/src/client/jsonc_writer.c +++ b/src/client/jsonc_writer.c @@ -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); }