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