6a6014b1f9837dd8537b4d845b4b8bf768859cd8
[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 };
102
103 static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
104 {
105         if (len <= 0)
106                 return true;
107
108         if (s->pos + len >= s->len) {
109                 s->len += 16;
110                 s->buf = realloc(s->buf, s->len);
111                 if (!s->buf)
112                         return false;
113         }
114         memcpy(s->buf + s->pos, c, len);
115         s->pos += len;
116         return true;
117 }
118
119 static void blobmsg_format_string(struct strbuf *s, const char *str)
120 {
121         const char *p, *last = str, *end = str + strlen(str);
122         char buf[8] = "\\u00";
123
124         blobmsg_puts(s, "\"", 1);
125         for (p = str; *p; p++) {
126                 char escape = '\0';
127                 int len;
128
129                 switch(*p) {
130                 case '\b':
131                         escape = 'b';
132                         break;
133                 case '\n':
134                         escape = 'n';
135                         break;
136                 case '\t':
137                         escape = 't';
138                         break;
139                 case '\r':
140                         escape = 'r';
141                         break;
142                 case '"':
143                 case '\\':
144                 case '/':
145                         escape = *p;
146                         break;
147                 default:
148                         if (*p < ' ')
149                                 escape = 'u';
150                         break;
151                 }
152
153                 if (!escape)
154                         continue;
155
156                 if (p > last)
157                         blobmsg_puts(s, last, p - last);
158                 last = p + 1;
159                 buf[1] = escape;
160
161                 if (escape == 'u') {
162                         sprintf(buf + 4, "%02x", (unsigned char) *p);
163                         len = 4;
164                 } else {
165                         len = 2;
166                 }
167                 blobmsg_puts(s, buf, len);
168         }
169
170         blobmsg_puts(s, last, end - last);
171         blobmsg_puts(s, "\"", 1);
172 }
173
174 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
175
176 static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head)
177 {
178         const char *data_str;
179         char buf[32];
180         void *data;
181         int len;
182
183         if (!blobmsg_check_attr(attr, false))
184                 return;
185
186         if (!array && blobmsg_name(attr)[0]) {
187                 blobmsg_format_string(s, blobmsg_name(attr));
188                 blobmsg_puts(s, ":", 1);
189         }
190         if (head) {
191                 data = blob_data(attr);
192                 len = blob_len(attr);
193         } else {
194                 data = blobmsg_data(attr);
195                 len = blobmsg_data_len(attr);
196
197                 if (s->custom_format) {
198                         data_str = s->custom_format(s->priv, attr);
199                         if (data_str)
200                                 goto out;
201                 }
202         }
203
204         data_str = buf;
205         switch(blob_id(attr)) {
206         case BLOBMSG_TYPE_INT8:
207                 sprintf(buf, "%d", *(uint8_t *)data);
208                 break;
209         case BLOBMSG_TYPE_INT16:
210                 sprintf(buf, "%d", *(uint16_t *)data);
211                 break;
212         case BLOBMSG_TYPE_INT32:
213                 sprintf(buf, "%d", *(uint32_t *)data);
214                 break;
215         case BLOBMSG_TYPE_INT64:
216                 sprintf(buf, "%lld", *(uint64_t *)data);
217                 break;
218         case BLOBMSG_TYPE_STRING:
219                 blobmsg_format_string(s, data);
220                 return;
221         case BLOBMSG_TYPE_ARRAY:
222                 blobmsg_format_json_list(s, data, len, true);
223                 return;
224         case BLOBMSG_TYPE_TABLE:
225                 blobmsg_format_json_list(s, data, len, false);
226                 return;
227         }
228
229 out:
230         blobmsg_puts(s, data_str, strlen(data_str));
231 }
232
233 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
234 {
235         struct blob_attr *pos;
236         bool first = true;
237         int rem = len;
238
239         blobmsg_puts(s, (array ? "[ " : "{ "), 2);
240         __blob_for_each_attr(pos, attr, rem) {
241                 if (!first)
242                         blobmsg_puts(s, ", ", 2);
243
244                 blobmsg_format_element(s, pos, array, false);
245                 first = false;
246         }
247         blobmsg_puts(s, (array ? " ]" : " }"), 2);
248 }
249
250 char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv)
251 {
252         struct strbuf s;
253
254         s.len = blob_len(attr);
255         s.buf = malloc(s.len);
256         s.pos = 0;
257         s.custom_format = cb;
258         s.priv = priv;
259
260         if (list)
261                 blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false);
262         else
263                 blobmsg_format_element(&s, attr, false, false);
264
265         if (!s.len)
266                 return NULL;
267
268         s.buf = realloc(s.buf, s.pos + 1);
269         s.buf[s.pos] = 0;
270
271         return s.buf;
272 }