2 * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "blobmsg_json.h"
19 static bool blobmsg_add_object(struct blob_buf *b, json_object *obj)
21 json_object_object_foreach(obj, key, val) {
22 if (!blobmsg_add_json_element(b, key, val))
28 static bool blobmsg_add_array(struct blob_buf *b, struct array_list *a)
32 for (i = 0, len = array_list_length(a); i < len; i++) {
33 if (!blobmsg_add_json_element(b, NULL, array_list_get_idx(a, i)))
40 bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object *obj)
48 switch (json_object_get_type(obj)) {
49 case json_type_object:
50 c = blobmsg_open_table(b, name);
51 ret = blobmsg_add_object(b, obj);
52 blobmsg_close_table(b, c);
55 c = blobmsg_open_array(b, name);
56 ret = blobmsg_add_array(b, json_object_get_array(obj));
57 blobmsg_close_array(b, c);
59 case json_type_string:
60 blobmsg_add_string(b, name, json_object_get_string(obj));
62 case json_type_boolean:
63 blobmsg_add_u8(b, name, json_object_get_boolean(obj));
66 blobmsg_add_u32(b, name, json_object_get_int(obj));
74 bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
79 obj = json_tokener_parse(str);
83 if (json_object_get_type(obj) != json_type_object)
86 ret = blobmsg_add_object(b, obj);
99 blobmsg_json_format_t custom_format;
105 static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
110 if (s->pos + len >= s->len) {
112 s->buf = realloc(s->buf, s->len);
116 memcpy(s->buf + s->pos, c, len);
121 static void add_separator(struct strbuf *s)
123 static char indent_chars[17] = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
124 static const char indent_space = ' ';
129 blobmsg_puts(s, &indent_space, 1);
133 indent = s->indent_level;
137 start = &indent_chars[sizeof(indent_chars) - indent - 1];
139 blobmsg_puts(s, start, indent + 1);
144 static void blobmsg_format_string(struct strbuf *s, const char *str)
146 const char *p, *last = str, *end = str + strlen(str);
147 char buf[8] = "\\u00";
149 blobmsg_puts(s, "\"", 1);
150 for (p = str; *p; p++) {
182 blobmsg_puts(s, last, p - last);
187 sprintf(buf + 4, "%02x", (unsigned char) *p);
192 blobmsg_puts(s, buf, len);
195 blobmsg_puts(s, last, end - last);
196 blobmsg_puts(s, "\"", 1);
199 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
201 static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head)
203 const char *data_str;
208 if (!blobmsg_check_attr(attr, false))
211 if (!array && blobmsg_name(attr)[0]) {
212 blobmsg_format_string(s, blobmsg_name(attr));
213 blobmsg_puts(s, ": ", 2);
216 data = blob_data(attr);
217 len = blob_len(attr);
219 data = blobmsg_data(attr);
220 len = blobmsg_data_len(attr);
222 if (s->custom_format) {
223 data_str = s->custom_format(s->priv, attr);
230 switch(blob_id(attr)) {
231 case BLOBMSG_TYPE_BOOL:
232 sprintf(buf, "%s", *(uint8_t *)data ? "true" : "false");
234 case BLOBMSG_TYPE_INT16:
235 sprintf(buf, "%d", be16_to_cpu(*(uint16_t *)data));
237 case BLOBMSG_TYPE_INT32:
238 sprintf(buf, "%d", be32_to_cpu(*(uint32_t *)data));
240 case BLOBMSG_TYPE_INT64:
241 sprintf(buf, "%lld", (long long int) be64_to_cpu(*(uint64_t *)data));
243 case BLOBMSG_TYPE_STRING:
244 blobmsg_format_string(s, data);
246 case BLOBMSG_TYPE_ARRAY:
247 blobmsg_format_json_list(s, data, len, true);
249 case BLOBMSG_TYPE_TABLE:
250 blobmsg_format_json_list(s, data, len, false);
255 blobmsg_puts(s, data_str, strlen(data_str));
258 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
260 struct blob_attr *pos;
264 blobmsg_puts(s, (array ? "[" : "{" ), 1);
267 __blob_for_each_attr(pos, attr, rem) {
269 blobmsg_puts(s, ",", 1);
273 blobmsg_format_element(s, pos, array, false);
278 blobmsg_puts(s, (array ? "]" : "}"), 1);
281 char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
285 s.len = blob_len(attr);
286 s.buf = malloc(s.len);
288 s.custom_format = cb;
294 s.indent_level = indent;
298 blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false);
300 blobmsg_format_element(&s, attr, false, false);
305 s.buf = realloc(s.buf, s.pos + 1);