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.
18 #include "blobmsg_json.h"
23 #include <json/json.h>
26 bool blobmsg_add_object(struct blob_buf *b, json_object *obj)
28 json_object_object_foreach(obj, key, val) {
29 if (!blobmsg_add_json_element(b, key, val))
35 static bool blobmsg_add_array(struct blob_buf *b, struct array_list *a)
39 for (i = 0, len = array_list_length(a); i < len; i++) {
40 if (!blobmsg_add_json_element(b, NULL, array_list_get_idx(a, i)))
47 bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object *obj)
52 switch (json_object_get_type(obj)) {
53 case json_type_object:
54 c = blobmsg_open_table(b, name);
55 ret = blobmsg_add_object(b, obj);
56 blobmsg_close_table(b, c);
59 c = blobmsg_open_array(b, name);
60 ret = blobmsg_add_array(b, json_object_get_array(obj));
61 blobmsg_close_array(b, c);
63 case json_type_string:
64 blobmsg_add_string(b, name, json_object_get_string(obj));
66 case json_type_boolean:
67 blobmsg_add_u8(b, name, json_object_get_boolean(obj));
70 int64_t i64 = json_object_get_int64(obj);
71 if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
72 blobmsg_add_u32(b, name, (uint32_t)i64);
74 blobmsg_add_u64(b, name, (uint64_t)i64);
78 case json_type_double:
79 blobmsg_add_double(b, name, json_object_get_double(obj));
82 blobmsg_add_field(b, BLOBMSG_TYPE_UNSPEC, name, NULL, 0);
90 static bool __blobmsg_add_json(struct blob_buf *b, json_object *obj)
97 if (json_object_get_type(obj) != json_type_object)
100 ret = blobmsg_add_object(b, obj);
103 json_object_put(obj);
107 bool blobmsg_add_json_from_file(struct blob_buf *b, const char *file)
109 return __blobmsg_add_json(b, json_object_from_file(file));
112 bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
114 return __blobmsg_add_json(b, json_tokener_parse(str));
123 blobmsg_json_format_t custom_format;
129 static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
137 if (s->pos + len >= s->len) {
138 new_len = s->len + 16 + len;
139 new_buf = realloc(s->buf, new_len);
147 memcpy(s->buf + s->pos, c, len);
152 static void add_separator(struct strbuf *s)
154 const char *indent_chars = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
160 len = s->indent_level + 1;
161 if (len > strlen(indent_chars))
162 len = strlen(indent_chars);
164 blobmsg_puts(s, indent_chars, len);
168 static void blobmsg_format_string(struct strbuf *s, const char *str)
170 const unsigned char *p, *last, *end;
171 char buf[8] = "\\u00";
173 end = (unsigned char *) str + strlen(str);
174 blobmsg_puts(s, "\"", 1);
175 for (p = (unsigned char *) str, last = p; *p; p++) {
206 blobmsg_puts(s, (char *) last, p - last);
211 snprintf(buf + 4, sizeof(buf) - 4, "%02x", (unsigned char) *p);
216 blobmsg_puts(s, buf, len);
219 blobmsg_puts(s, (char *) last, end - last);
220 blobmsg_puts(s, "\"", 1);
223 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
225 static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool without_name, bool head)
227 const char *data_str;
232 if (!blobmsg_check_attr(attr, false))
235 if (!without_name && blobmsg_name(attr)[0]) {
236 blobmsg_format_string(s, blobmsg_name(attr));
237 blobmsg_puts(s, ": ", s->indent ? 2 : 1);
240 data = blobmsg_data(attr);
241 len = blobmsg_data_len(attr);
243 if (!head && s->custom_format) {
244 data_str = s->custom_format(s->priv, attr);
250 switch(blob_id(attr)) {
251 case BLOBMSG_TYPE_UNSPEC:
252 snprintf(buf, sizeof(buf), "null");
254 case BLOBMSG_TYPE_BOOL:
255 snprintf(buf, sizeof(buf), "%s", *(uint8_t *)data ? "true" : "false");
257 case BLOBMSG_TYPE_INT16:
258 snprintf(buf, sizeof(buf), "%" PRId16, (int16_t) be16_to_cpu(*(uint16_t *)data));
260 case BLOBMSG_TYPE_INT32:
261 snprintf(buf, sizeof(buf), "%" PRId32, (int32_t) be32_to_cpu(*(uint32_t *)data));
263 case BLOBMSG_TYPE_INT64:
264 snprintf(buf, sizeof(buf), "%" PRId64, (int64_t) be64_to_cpu(*(uint64_t *)data));
266 case BLOBMSG_TYPE_DOUBLE:
267 snprintf(buf, sizeof(buf), "%lf", blobmsg_get_double(attr));
269 case BLOBMSG_TYPE_STRING:
270 blobmsg_format_string(s, data);
272 case BLOBMSG_TYPE_ARRAY:
273 blobmsg_format_json_list(s, data, len, true);
275 case BLOBMSG_TYPE_TABLE:
276 blobmsg_format_json_list(s, data, len, false);
281 blobmsg_puts(s, data_str, strlen(data_str));
284 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
286 struct blob_attr *pos;
290 blobmsg_puts(s, (array ? "[" : "{" ), 1);
293 __blob_for_each_attr(pos, attr, rem) {
295 blobmsg_puts(s, ",", 1);
299 blobmsg_format_element(s, pos, array, false);
304 blobmsg_puts(s, (array ? "]" : "}"), 1);
307 static void setup_strbuf(struct strbuf *s, struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
309 s->len = blob_len(attr);
310 s->buf = malloc(s->len);
312 s->custom_format = cb;
318 s->indent_level = indent;
322 char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
324 struct strbuf s = {0};
328 setup_strbuf(&s, attr, cb, priv, indent);
332 array = blob_is_extended(attr) &&
333 blobmsg_type(attr) == BLOBMSG_TYPE_ARRAY;
336 blobmsg_format_json_list(&s, blobmsg_data(attr), blobmsg_data_len(attr), array);
338 blobmsg_format_element(&s, attr, false, false);
345 ret = realloc(s.buf, s.pos + 1);
356 char *blobmsg_format_json_value_with_cb(struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
358 struct strbuf s = {0};
361 setup_strbuf(&s, attr, cb, priv, indent);
365 blobmsg_format_element(&s, attr, true, false);
372 ret = realloc(s.buf, s.pos + 1);