add support for json-formatting blobmsg elements

This commit is contained in:
Felix Fietkau 2011-01-30 14:15:57 +01:00
parent ff585b97c0
commit da2876acd9
2 changed files with 9 additions and 6 deletions

View file

@ -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;

View file

@ -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); \