move json formatting to the blobmsg_json library
[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 (!array && blobmsg_name(attr)[0]) {
184 blobmsg_format_string(s, blobmsg_name(attr));
185 blobmsg_puts(s, ":", 1);
186 }
187 if (head) {
188 data = blob_data(attr);
189 len = blob_len(attr);
190 } else {
191 data = blobmsg_data(attr);
192 len = blobmsg_data_len(attr);
193
194 if (s->custom_format) {
195 data_str = s->custom_format(s->priv, attr);
196 if (data_str)
197 goto out;
198 }
199 }
200
201 data_str = buf;
202 switch(blob_id(attr)) {
203 case BLOBMSG_TYPE_INT8:
204 sprintf(buf, "%d", *(uint8_t *)data);
205 break;
206 case BLOBMSG_TYPE_INT16:
207 sprintf(buf, "%d", *(uint16_t *)data);
208 break;
209 case BLOBMSG_TYPE_INT32:
210 sprintf(buf, "%d", *(uint32_t *)data);
211 break;
212 case BLOBMSG_TYPE_INT64:
213 sprintf(buf, "%lld", *(uint64_t *)data);
214 break;
215 case BLOBMSG_TYPE_STRING:
216 blobmsg_format_string(s, data);
217 return;
218 case BLOBMSG_TYPE_ARRAY:
219 blobmsg_format_json_list(s, data, len, true);
220 return;
221 case BLOBMSG_TYPE_TABLE:
222 blobmsg_format_json_list(s, data, len, false);
223 return;
224 }
225
226 out:
227 blobmsg_puts(s, data_str, strlen(data_str));
228 }
229
230 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
231 {
232 struct blob_attr *pos;
233 bool first = true;
234 int rem = len;
235
236 blobmsg_puts(s, (array ? "[ " : "{ "), 2);
237 __blob_for_each_attr(pos, attr, rem) {
238 if (!first)
239 blobmsg_puts(s, ", ", 2);
240
241 blobmsg_format_element(s, pos, array, false);
242 first = false;
243 }
244 blobmsg_puts(s, (array ? " ]" : " }"), 2);
245 }
246
247 char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv)
248 {
249 struct strbuf s;
250
251 s.len = blob_len(attr);
252 s.buf = malloc(s.len);
253 s.pos = 0;
254 s.custom_format = cb;
255 s.priv = priv;
256
257 if (list)
258 blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false);
259 else
260 blobmsg_format_element(&s, attr, false, false);
261
262 if (!s.len)
263 return NULL;
264
265 s.buf = realloc(s.buf, s.pos + 1);
266 return s.buf;
267 }