blobmsg_json: allow signed output of integers
[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 static bool __blobmsg_add_json(struct blob_buf *b, json_object *obj)
75 {
76 bool ret = false;
77
78 if (is_error(obj))
79 return false;
80
81 if (json_object_get_type(obj) != json_type_object)
82 goto out;
83
84 ret = blobmsg_add_object(b, obj);
85
86 out:
87 json_object_put(obj);
88 return ret;
89 }
90
91 bool blobmsg_add_json_from_file(struct blob_buf *b, const char *file)
92 {
93 return __blobmsg_add_json(b, json_object_from_file(file));
94 }
95
96 bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
97 {
98 return __blobmsg_add_json(b, json_tokener_parse(str));
99 }
100
101
102 struct strbuf {
103 int len;
104 int pos;
105 char *buf;
106
107 blobmsg_json_format_t custom_format;
108 void *priv;
109 bool indent;
110 int indent_level;
111 };
112
113 static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
114 {
115 if (len <= 0)
116 return true;
117
118 if (s->pos + len >= s->len) {
119 s->len += 16 + len;
120 s->buf = realloc(s->buf, s->len);
121 if (!s->buf)
122 return false;
123 }
124 memcpy(s->buf + s->pos, c, len);
125 s->pos += len;
126 return true;
127 }
128
129 static void add_separator(struct strbuf *s)
130 {
131 static char indent_chars[17] = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
132 static const char indent_space = ' ';
133 int indent;
134 char *start;
135
136 if (!s->indent) {
137 blobmsg_puts(s, &indent_space, 1);
138 return;
139 }
140
141 indent = s->indent_level;
142 if (indent > 16)
143 indent = 16;
144
145 start = &indent_chars[sizeof(indent_chars) - indent - 1];
146 *start = '\n';
147 blobmsg_puts(s, start, indent + 1);
148 *start = '\t';
149 }
150
151
152 static void blobmsg_format_string(struct strbuf *s, const char *str)
153 {
154 const unsigned char *p, *last, *end;
155 char buf[8] = "\\u00";
156
157 end = (unsigned char *) str + strlen(str);
158 blobmsg_puts(s, "\"", 1);
159 for (p = (unsigned char *) str, last = p; *p; p++) {
160 char escape = '\0';
161 int len;
162
163 switch(*p) {
164 case '\b':
165 escape = 'b';
166 break;
167 case '\n':
168 escape = 'n';
169 break;
170 case '\t':
171 escape = 't';
172 break;
173 case '\r':
174 escape = 'r';
175 break;
176 case '"':
177 case '\\':
178 case '/':
179 escape = *p;
180 break;
181 default:
182 if (*p < ' ')
183 escape = 'u';
184 break;
185 }
186
187 if (!escape)
188 continue;
189
190 if (p > last)
191 blobmsg_puts(s, (char *) last, p - last);
192 last = p + 1;
193 buf[1] = escape;
194
195 if (escape == 'u') {
196 sprintf(buf + 4, "%02x", (unsigned char) *p);
197 len = 6;
198 } else {
199 len = 2;
200 }
201 blobmsg_puts(s, buf, len);
202 }
203
204 blobmsg_puts(s, (char *) last, end - last);
205 blobmsg_puts(s, "\"", 1);
206 }
207
208 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
209
210 static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head)
211 {
212 const char *data_str;
213 char buf[32];
214 void *data;
215 int len;
216
217 if (!blobmsg_check_attr(attr, false))
218 return;
219
220 if (!array && blobmsg_name(attr)[0]) {
221 blobmsg_format_string(s, blobmsg_name(attr));
222 blobmsg_puts(s, ": ", 2);
223 }
224 if (head) {
225 data = blob_data(attr);
226 len = blob_len(attr);
227 } else {
228 data = blobmsg_data(attr);
229 len = blobmsg_data_len(attr);
230
231 if (s->custom_format) {
232 data_str = s->custom_format(s->priv, attr);
233 if (data_str)
234 goto out;
235 }
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, "%lld", (long long int) 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
297 s.len = blob_len(attr);
298 s.buf = malloc(s.len);
299 s.pos = 0;
300 s.custom_format = cb;
301 s.priv = priv;
302 s.indent = false;
303
304 if (indent >= 0) {
305 s.indent = true;
306 s.indent_level = indent;
307 }
308
309 if (list)
310 blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false);
311 else
312 blobmsg_format_element(&s, attr, false, false);
313
314 if (!s.len)
315 return NULL;
316
317 s.buf = realloc(s.buf, s.pos + 1);
318 s.buf[s.pos] = 0;
319
320 return s.buf;
321 }