From: Felix Fietkau Date: Sun, 30 Jan 2011 13:15:57 +0000 (+0100) Subject: add support for json-formatting blobmsg elements X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=da2876acd93e2471db455c253b20619e523a9694;p=oweals%2Flibubox.git add support for json-formatting blobmsg elements --- diff --git a/blobmsg.c b/blobmsg.c index 69a160b..742a93b 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -100,7 +100,7 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo void *data; int len; - if (!array) { + if (!array && blobmsg_name(attr)[0]) { blobmsg_format_string(s, blobmsg_name(attr)); blobmsg_puts(s, ":", 1); } @@ -155,7 +155,7 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i blobmsg_puts(s, (array ? " ]" : " }"), 2); } -char *blobmsg_format_json(struct blob_attr *attr, bool named) +char *blobmsg_format_json(struct blob_attr *attr, bool list) { struct strbuf s; @@ -163,7 +163,10 @@ char *blobmsg_format_json(struct blob_attr *attr, bool named) s.buf = malloc(s.len); s.pos = 0; - blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), !named); + if (list) + blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false); + else + blobmsg_format_element(&s, attr, false, false); if (!s.len) return NULL; diff --git a/blobmsg.h b/blobmsg.h index 554bed8..7c63dbf 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -49,10 +49,10 @@ static inline int blobmsg_hdrlen(int namelen) return BLOBMSG_PADDING(sizeof(struct blobmsg_hdr) + namelen + 1); } -static inline void *blobmsg_name(struct blob_attr *attr) +static inline char *blobmsg_name(struct blob_attr *attr) { struct blobmsg_hdr *hdr = blob_data(attr); - return &hdr->name; + return (char *) hdr->name; } static inline void *blobmsg_data(struct blob_attr *attr) @@ -139,7 +139,7 @@ static inline int blobmsg_buf_init(struct blob_buf *buf) return blob_buf_init(buf, BLOBMSG_TYPE_TABLE); } -char *blobmsg_format_json(struct blob_attr *attr, bool named); +char *blobmsg_format_json(struct blob_attr *attr, bool list); #define blobmsg_for_each_attr(pos, attr, rem) \ for (rem = blobmsg_data_len(attr), pos = blobmsg_data(attr); \