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