From patchwork Tue Jun 21 15:19:12 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Schiffer X-Patchwork-Id: 638747 X-Patchwork-Delegate: blogic@openwrt.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (caladan.dune.hu [78.24.191.180]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rYrxp296Hz9sf9 for ; Wed, 22 Jun 2016 01:19:58 +1000 (AEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id A9A71B91B2C; Tue, 21 Jun 2016 17:19:25 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.1 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP; Tue, 21 Jun 2016 17:19:25 +0200 (CEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 16139B91B18 for ; Tue, 21 Jun 2016 17:19:17 +0200 (CEST) X-policyd-weight: using cached result; rate:hard: -6.1 Received: from chaos.universe-factory.net (chaos.universe-factory.net [37.72.148.22]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Tue, 21 Jun 2016 17:19:17 +0200 (CEST) Received: from localhost.localdomain (unknown [IPv6:fd1b:c28a:2fd6::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by chaos.universe-factory.net (Postfix) with ESMTPSA id 9785C1829FF; Tue, 21 Jun 2016 17:19:16 +0200 (CEST) From: Matthias Schiffer To: openwrt-devel@lists.openwrt.org, lede-dev@lists.infradead.org Date: Tue, 21 Jun 2016 17:19:12 +0200 Message-Id: X-Mailer: git-send-email 2.9.0 In-Reply-To: References: In-Reply-To: References: Subject: [OpenWrt-Devel] [PATCH libubox v2 3/3] blobmsg_json: add new functions blobmsg_format_json_value* X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" The current blobmsg_format_json* functions will return invalid JSON when the "list" argument is given as false (blobmsg_format_element() will output the name of the blob_attr as if the value is printed as part of a JSON object). To avoid breaking software relying on this behaviour, introduce new functions which will never print the blob_attr name and thus always produce valid JSON. Signed-off-by: Matthias Schiffer --- v2: no changes blobmsg_json.c | 61 ++++++++++++++++++++++++++++++++++++++++++++-------------- blobmsg_json.h | 14 ++++++++++++++ 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/blobmsg_json.c b/blobmsg_json.c index 2e318b2..78747d1 100644 --- a/blobmsg_json.c +++ b/blobmsg_json.c @@ -214,7 +214,7 @@ static void blobmsg_format_string(struct strbuf *s, const char *str) static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array); -static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head) +static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool without_name, bool head) { const char *data_str; char buf[32]; @@ -224,7 +224,7 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo if (!blobmsg_check_attr(attr, false)) return; - if (!array && blobmsg_name(attr)[0]) { + if (!without_name && blobmsg_name(attr)[0]) { blobmsg_format_string(s, blobmsg_name(attr)); blobmsg_puts(s, ": ", s->indent ? 2 : 1); } @@ -293,27 +293,30 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i blobmsg_puts(s, (array ? "]" : "}"), 1); } +static void setup_strbuf(struct strbuf *s, struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent) { + s->len = blob_len(attr); + s->buf = malloc(s->len); + s->pos = 0; + s->custom_format = cb; + s->priv = priv; + s->indent = false; + + if (indent >= 0) { + s->indent = true; + s->indent_level = indent; + } +} + char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent) { struct strbuf s; bool array; char *ret; - s.len = blob_len(attr); - s.pos = 0; - s.custom_format = cb; - s.priv = priv; - s.indent = false; - - s.buf = malloc(s.len); + setup_strbuf(&s, attr, cb, priv, indent); if (!s.buf) return NULL; - if (indent >= 0) { - s.indent = true; - s.indent_level = indent; - } - array = blob_is_extended(attr) && blobmsg_type(attr) == BLOBMSG_TYPE_ARRAY; @@ -337,3 +340,33 @@ char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_jso return ret; } + +char *blobmsg_format_json_value_with_cb(struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent) +{ + struct strbuf s; + char *ret; + + setup_strbuf(&s, attr, cb, priv, indent); + if (!s.buf) + return NULL; + + blobmsg_format_element(&s, attr, true, false); + + if (!s.len) { + free(s.buf); + return NULL; + } + + s.buf = realloc(s.buf, s.pos + 1); + s.buf[s.pos] = 0; + + ret = realloc(s.buf, s.pos + 1); + if (!ret) { + free(s.buf); + return NULL; + } + + ret[s.pos] = 0; + + return ret; +} diff --git a/blobmsg_json.h b/blobmsg_json.h index cd9ed33..9dfc02d 100644 --- a/blobmsg_json.h +++ b/blobmsg_json.h @@ -42,4 +42,18 @@ static inline char *blobmsg_format_json_indent(struct blob_attr *attr, bool list return blobmsg_format_json_with_cb(attr, list, NULL, NULL, indent); } +char *blobmsg_format_json_value_with_cb(struct blob_attr *attr, + blobmsg_json_format_t cb, void *priv, + int indent); + +static inline char *blobmsg_format_json_value(struct blob_attr *attr) +{ + return blobmsg_format_json_value_with_cb(attr, NULL, NULL, -1); +} + +static inline char *blobmsg_format_json_value_indent(struct blob_attr *attr, int indent) +{ + return blobmsg_format_json_value_with_cb(attr, NULL, NULL, indent); +} + #endif