runqueue: add a simple task queueing/completion tracking implementation
[project/libubox.git] / blobmsg_json.c
1 /*
2  * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
3  *
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.
7  *
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.
15  */
16 #include "blobmsg.h"
17 #include "blobmsg_json.h"
18
19 bool blobmsg_add_object(struct blob_buf *b, json_object *obj)
20 {
21         json_object_object_foreach(obj, key, val) {
22                 if (!blobmsg_add_json_element(b, key, val))
23                         return false;
24         }
25         return true;
26 }
27
28 static bool blobmsg_add_array(struct blob_buf *b, struct array_list *a)
29 {
30         int i, len;
31
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)))
34                         return false;
35         }
36
37         return true;
38 }
39
40 bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object *obj)
41 {
42         bool ret = true;
43         void *c;
44
45         if (!obj)
46                 return false;
47
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);
53                 break;
54         case json_type_array:
55                 c = blobmsg_open_array(b, name);
56                 ret = blobmsg_add_array(b, json_object_get_array(obj));
57                 blobmsg_close_array(b, c);
58                 break;
59         case json_type_string:
60                 blobmsg_add_string(b, name, json_object_get_string(obj));
61                 break;
62         case json_type_boolean:
63                 blobmsg_add_u8(b, name, json_object_get_boolean(obj));
64                 break;
65         case json_type_int:
66                 blobmsg_add_u32(b, name, json_object_get_int(obj));
67                 break;
68         default:
69                 return false;
70         }
71         return ret;
72 }
73
74 bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
75 {
76         json_object *obj;
77         bool ret = false;
78
79         obj = json_tokener_parse(str);
80         if (is_error(obj))
81                 return false;
82
83         if (json_object_get_type(obj) != json_type_object)
84                 goto out;
85
86         ret = blobmsg_add_object(b, obj);
87
88 out:
89         json_object_put(obj);
90         return ret;
91 }
92
93
94 struct strbuf {
95         int len;
96         int pos;
97         char *buf;
98
99         blobmsg_json_format_t custom_format;
100         void *priv;
101         bool indent;
102         int indent_level;
103 };
104
105 static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
106 {
107         if (len <= 0)
108                 return true;
109
110         if (s->pos + len >= s->len) {
111                 s->len += 16 + len;
112                 s->buf = realloc(s->buf, s->len);
113                 if (!s->buf)
114                         return false;
115         }
116         memcpy(s->buf + s->pos, c, len);
117         s->pos += len;
118         return true;
119 }
120
121 static void add_separator(struct strbuf *s)
122 {
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 = ' ';
125         int indent;
126         char *start;
127
128         if (!s->indent) {
129                 blobmsg_puts(s, &indent_space, 1);
130                 return;
131         }
132
133         indent = s->indent_level;
134         if (indent > 16)
135                 indent = 16;
136
137         start = &indent_chars[sizeof(indent_chars) - indent - 1];
138         *start = '\n';
139         blobmsg_puts(s, start, indent + 1);
140         *start = '\t';
141 }
142
143
144 static void blobmsg_format_string(struct strbuf *s, const char *str)
145 {
146         const char *p, *last = str, *end = str + strlen(str);
147         char buf[8] = "\\u00";
148
149         blobmsg_puts(s, "\"", 1);
150         for (p = str; *p; p++) {
151                 char escape = '\0';
152                 int len;
153
154                 switch(*p) {
155                 case '\b':
156                         escape = 'b';
157                         break;
158                 case '\n':
159                         escape = 'n';
160                         break;
161                 case '\t':
162                         escape = 't';
163                         break;
164                 case '\r':
165                         escape = 'r';
166                         break;
167                 case '"':
168                 case '\\':
169                 case '/':
170                         escape = *p;
171                         break;
172                 default:
173                         if (*p < ' ')
174                                 escape = 'u';
175                         break;
176                 }
177
178                 if (!escape)
179                         continue;
180
181                 if (p > last)
182                         blobmsg_puts(s, last, p - last);
183                 last = p + 1;
184                 buf[1] = escape;
185
186                 if (escape == 'u') {
187                         sprintf(buf + 4, "%02x", (unsigned char) *p);
188                         len = 4;
189                 } else {
190                         len = 2;
191                 }
192                 blobmsg_puts(s, buf, len);
193         }
194
195         blobmsg_puts(s, last, end - last);
196         blobmsg_puts(s, "\"", 1);
197 }
198
199 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
200
201 static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head)
202 {
203         const char *data_str;
204         char buf[32];
205         void *data;
206         int len;
207
208         if (!blobmsg_check_attr(attr, false))
209                 return;
210
211         if (!array && blobmsg_name(attr)[0]) {
212                 blobmsg_format_string(s, blobmsg_name(attr));
213                 blobmsg_puts(s, ": ", 2);
214         }
215         if (head) {
216                 data = blob_data(attr);
217                 len = blob_len(attr);
218         } else {
219                 data = blobmsg_data(attr);
220                 len = blobmsg_data_len(attr);
221
222                 if (s->custom_format) {
223                         data_str = s->custom_format(s->priv, attr);
224                         if (data_str)
225                                 goto out;
226                 }
227         }
228
229         data_str = buf;
230         switch(blob_id(attr)) {
231         case BLOBMSG_TYPE_UNSPEC:
232                 sprintf(buf, "null");
233                 break;
234         case BLOBMSG_TYPE_BOOL:
235                 sprintf(buf, "%s", *(uint8_t *)data ? "true" : "false");
236                 break;
237         case BLOBMSG_TYPE_INT16:
238                 sprintf(buf, "%d", be16_to_cpu(*(uint16_t *)data));
239                 break;
240         case BLOBMSG_TYPE_INT32:
241                 sprintf(buf, "%d", be32_to_cpu(*(uint32_t *)data));
242                 break;
243         case BLOBMSG_TYPE_INT64:
244                 sprintf(buf, "%lld", (long long int) be64_to_cpu(*(uint64_t *)data));
245                 break;
246         case BLOBMSG_TYPE_STRING:
247                 blobmsg_format_string(s, data);
248                 return;
249         case BLOBMSG_TYPE_ARRAY:
250                 blobmsg_format_json_list(s, data, len, true);
251                 return;
252         case BLOBMSG_TYPE_TABLE:
253                 blobmsg_format_json_list(s, data, len, false);
254                 return;
255         }
256
257 out:
258         blobmsg_puts(s, data_str, strlen(data_str));
259 }
260
261 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
262 {
263         struct blob_attr *pos;
264         bool first = true;
265         int rem = len;
266
267         blobmsg_puts(s, (array ? "[" : "{" ), 1);
268         s->indent_level++;
269         add_separator(s);
270         __blob_for_each_attr(pos, attr, rem) {
271                 if (!first) {
272                         blobmsg_puts(s, ",", 1);
273                         add_separator(s);
274                 }
275
276                 blobmsg_format_element(s, pos, array, false);
277                 first = false;
278         }
279         s->indent_level--;
280         add_separator(s);
281         blobmsg_puts(s, (array ? "]" : "}"), 1);
282 }
283
284 char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
285 {
286         struct strbuf s;
287
288         s.len = blob_len(attr);
289         s.buf = malloc(s.len);
290         s.pos = 0;
291         s.custom_format = cb;
292         s.priv = priv;
293         s.indent = false;
294
295         if (indent >= 0) {
296                 s.indent = true;
297                 s.indent_level = indent;
298         }
299
300         if (list)
301                 blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false);
302         else
303                 blobmsg_format_element(&s, attr, false, false);
304
305         if (!s.len)
306                 return NULL;
307
308         s.buf = realloc(s.buf, s.pos + 1);
309         s.buf[s.pos] = 0;
310
311         return s.buf;
312 }