add BLOBMSG_TYPE_BOOL as an alias for BLOBMSG_TYPE_INT8
[project/libubox.git] / blobmsg_json.c
1 /*
2 * blobmsg - library for generating/parsing structured blob messages
3 *
4 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 2.1
8 * as published by the Free Software Foundation
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include "blobmsg.h"
17 #include "blobmsg_json.h"
18
19 static 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;
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_BOOL:
232 sprintf(buf, "%s", *(uint8_t *)data ? "true" : "false");
233 break;
234 case BLOBMSG_TYPE_INT32:
235 sprintf(buf, "%d", *(uint32_t *)data);
236 break;
237 case BLOBMSG_TYPE_INT64:
238 sprintf(buf, "%lld", (long long int) *(uint64_t *)data);
239 break;
240 case BLOBMSG_TYPE_STRING:
241 blobmsg_format_string(s, data);
242 return;
243 case BLOBMSG_TYPE_ARRAY:
244 blobmsg_format_json_list(s, data, len, true);
245 return;
246 case BLOBMSG_TYPE_TABLE:
247 blobmsg_format_json_list(s, data, len, false);
248 return;
249 }
250
251 out:
252 blobmsg_puts(s, data_str, strlen(data_str));
253 }
254
255 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
256 {
257 struct blob_attr *pos;
258 bool first = true;
259 int rem = len;
260
261 blobmsg_puts(s, (array ? "[" : "{" ), 1);
262 s->indent_level++;
263 add_separator(s);
264 __blob_for_each_attr(pos, attr, rem) {
265 if (!first) {
266 blobmsg_puts(s, ",", 1);
267 add_separator(s);
268 }
269
270 blobmsg_format_element(s, pos, array, false);
271 first = false;
272 }
273 s->indent_level--;
274 add_separator(s);
275 blobmsg_puts(s, (array ? "]" : "}"), 1);
276 }
277
278 char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
279 {
280 struct strbuf s;
281
282 s.len = blob_len(attr);
283 s.buf = malloc(s.len);
284 s.pos = 0;
285 s.custom_format = cb;
286 s.priv = priv;
287 s.indent = false;
288
289 if (indent >= 0) {
290 s.indent = true;
291 s.indent_level = indent;
292 }
293
294 if (list)
295 blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false);
296 else
297 blobmsg_format_element(&s, attr, false, false);
298
299 if (!s.len)
300 return NULL;
301
302 s.buf = realloc(s.buf, s.pos + 1);
303 s.buf[s.pos] = 0;
304
305 return s.buf;
306 }