int len;
int pos;
char *buf;
+
+ blobmsg_json_format_t custom_format;
+ void *priv;
};
static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head)
{
+ const char *data_str;
char buf[32];
void *data;
int len;
} else {
data = blobmsg_data(attr);
len = blobmsg_data_len(attr);
+
+ if (s->custom_format) {
+ data_str = s->custom_format(s->priv, attr);
+ if (data_str)
+ goto out;
+ }
}
+ data_str = buf;
switch(blob_id(attr)) {
case BLOBMSG_TYPE_INT8:
sprintf(buf, "%d", *(uint8_t *)data);
blobmsg_format_json_list(s, data, len, false);
return;
}
- blobmsg_puts(s, buf, strlen(buf));
+
+out:
+ blobmsg_puts(s, data_str, strlen(data_str));
}
static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
blobmsg_puts(s, (array ? " ]" : " }"), 2);
}
-char *blobmsg_format_json(struct blob_attr *attr, bool list)
+char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv)
{
struct strbuf s;
s.len = blob_len(attr);
s.buf = malloc(s.len);
s.pos = 0;
+ s.custom_format = cb;
+ s.priv = priv;
if (list)
blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false);
void *blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen);
void blobmsg_add_string_buffer(struct blob_buf *buf);
-char *blobmsg_format_json(struct blob_attr *attr, bool list);
+/* blobmsg to json formatting */
+
+typedef const char *(*blobmsg_json_format_t)(void *priv, struct blob_attr *attr);
+
+char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list,
+ blobmsg_json_format_t cb, void *priv);
+
+static inline char *blobmsg_format_json(struct blob_attr *attr, bool list)
+{
+ return blobmsg_format_json_with_cb(attr, list, NULL, NULL);
+}
+
#define blobmsg_for_each_attr(pos, attr, rem) \
for (rem = blobmsg_data_len(attr), pos = blobmsg_data(attr); \