2 * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
18 #include "blobmsg_json.h"
22 bool blobmsg_add_object(struct blob_buf
*b
, json_object
*obj
)
24 json_object_object_foreach(obj
, key
, val
) {
25 if (!blobmsg_add_json_element(b
, key
, val
))
31 static bool blobmsg_add_array(struct blob_buf
*b
, struct array_list
*a
)
35 for (i
= 0, len
= array_list_length(a
); i
< len
; i
++) {
36 if (!blobmsg_add_json_element(b
, NULL
, array_list_get_idx(a
, i
)))
43 bool blobmsg_add_json_element(struct blob_buf
*b
, const char *name
, json_object
*obj
)
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
);
55 c
= blobmsg_open_array(b
, name
);
56 ret
= blobmsg_add_array(b
, json_object_get_array(obj
));
57 blobmsg_close_array(b
, c
);
59 case json_type_string
:
60 blobmsg_add_string(b
, name
, json_object_get_string(obj
));
62 case json_type_boolean
:
63 blobmsg_add_u8(b
, name
, json_object_get_boolean(obj
));
66 int64_t i64
= json_object_get_int64(obj
);
67 if (i64
>= INT32_MIN
&& i64
<= INT32_MAX
) {
68 blobmsg_add_u32(b
, name
, (uint32_t)i64
);
70 blobmsg_add_u64(b
, name
, (uint64_t)i64
);
74 case json_type_double
:
75 blobmsg_add_double(b
, name
, json_object_get_double(obj
));
78 blobmsg_add_field(b
, BLOBMSG_TYPE_UNSPEC
, name
, NULL
, 0);
86 static bool __blobmsg_add_json(struct blob_buf
*b
, json_object
*obj
)
93 if (json_object_get_type(obj
) != json_type_object
)
96 ret
= blobmsg_add_object(b
, obj
);
103 bool blobmsg_add_json_from_file(struct blob_buf
*b
, const char *file
)
105 return __blobmsg_add_json(b
, json_object_from_file(file
));
108 bool blobmsg_add_json_from_string(struct blob_buf
*b
, const char *str
)
110 return __blobmsg_add_json(b
, json_tokener_parse(str
));
119 blobmsg_json_format_t custom_format
;
125 static bool blobmsg_puts(struct strbuf
*s
, const char *c
, int len
)
133 if (s
->pos
+ len
>= s
->len
) {
134 new_len
= s
->len
+ 16 + len
;
135 new_buf
= realloc(s
->buf
, new_len
);
143 memcpy(s
->buf
+ s
->pos
, c
, len
);
148 static void add_separator(struct strbuf
*s
)
150 const char indent_chars
[] = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
156 len
= s
->indent_level
+ 1;
157 if (len
> sizeof(indent_chars
) - 1)
158 len
= sizeof(indent_chars
) - 1;
160 blobmsg_puts(s
, indent_chars
, len
);
164 static void blobmsg_format_string(struct strbuf
*s
, const char *str
)
166 const unsigned char *p
, *last
, *end
;
167 char buf
[8] = "\\u00";
169 end
= (unsigned char *) str
+ strlen(str
);
170 blobmsg_puts(s
, "\"", 1);
171 for (p
= (unsigned char *) str
, last
= p
; *p
; p
++) {
202 blobmsg_puts(s
, (char *) last
, p
- last
);
207 snprintf(buf
+ 4, sizeof(buf
) - 4, "%02x", (unsigned char) *p
);
212 blobmsg_puts(s
, buf
, len
);
215 blobmsg_puts(s
, (char *) last
, end
- last
);
216 blobmsg_puts(s
, "\"", 1);
219 static void blobmsg_format_json_list(struct strbuf
*s
, struct blob_attr
*attr
, int len
, bool array
);
221 static void blobmsg_format_element(struct strbuf
*s
, struct blob_attr
*attr
, bool without_name
, bool head
)
223 const char *data_str
;
228 if (!blobmsg_check_attr(attr
, false))
231 if (!without_name
&& blobmsg_name(attr
)[0]) {
232 blobmsg_format_string(s
, blobmsg_name(attr
));
233 blobmsg_puts(s
, ": ", s
->indent
? 2 : 1);
236 data
= blobmsg_data(attr
);
237 len
= blobmsg_data_len(attr
);
239 if (!head
&& s
->custom_format
) {
240 data_str
= s
->custom_format(s
->priv
, attr
);
246 switch(blob_id(attr
)) {
247 case BLOBMSG_TYPE_UNSPEC
:
248 snprintf(buf
, sizeof(buf
), "null");
250 case BLOBMSG_TYPE_BOOL
:
251 snprintf(buf
, sizeof(buf
), "%s", *(uint8_t *)data
? "true" : "false");
253 case BLOBMSG_TYPE_INT16
:
254 snprintf(buf
, sizeof(buf
), "%" PRId16
, (int16_t) be16_to_cpu(*(uint16_t *)data
));
256 case BLOBMSG_TYPE_INT32
:
257 snprintf(buf
, sizeof(buf
), "%" PRId32
, (int32_t) be32_to_cpu(*(uint32_t *)data
));
259 case BLOBMSG_TYPE_INT64
:
260 snprintf(buf
, sizeof(buf
), "%" PRId64
, (int64_t) be64_to_cpu(*(uint64_t *)data
));
262 case BLOBMSG_TYPE_DOUBLE
:
263 snprintf(buf
, sizeof(buf
), "%lf", blobmsg_get_double(attr
));
265 case BLOBMSG_TYPE_STRING
:
266 blobmsg_format_string(s
, data
);
268 case BLOBMSG_TYPE_ARRAY
:
269 blobmsg_format_json_list(s
, data
, len
, true);
271 case BLOBMSG_TYPE_TABLE
:
272 blobmsg_format_json_list(s
, data
, len
, false);
277 blobmsg_puts(s
, data_str
, strlen(data_str
));
280 static void blobmsg_format_json_list(struct strbuf
*s
, struct blob_attr
*attr
, int len
, bool array
)
282 struct blob_attr
*pos
;
286 blobmsg_puts(s
, (array
? "[" : "{" ), 1);
289 __blob_for_each_attr(pos
, attr
, rem
) {
291 blobmsg_puts(s
, ",", 1);
295 blobmsg_format_element(s
, pos
, array
, false);
300 blobmsg_puts(s
, (array
? "]" : "}"), 1);
303 static void setup_strbuf(struct strbuf
*s
, struct blob_attr
*attr
, blobmsg_json_format_t cb
, void *priv
, int indent
)
305 s
->len
= blob_len(attr
);
306 s
->buf
= malloc(s
->len
);
308 s
->custom_format
= cb
;
314 s
->indent_level
= indent
;
318 char *blobmsg_format_json_with_cb(struct blob_attr
*attr
, bool list
, blobmsg_json_format_t cb
, void *priv
, int indent
)
320 struct strbuf s
= {0};
324 setup_strbuf(&s
, attr
, cb
, priv
, indent
);
328 array
= blob_is_extended(attr
) &&
329 blobmsg_type(attr
) == BLOBMSG_TYPE_ARRAY
;
332 blobmsg_format_json_list(&s
, blobmsg_data(attr
), blobmsg_data_len(attr
), array
);
334 blobmsg_format_element(&s
, attr
, false, false);
341 ret
= realloc(s
.buf
, s
.pos
+ 1);
352 char *blobmsg_format_json_value_with_cb(struct blob_attr
*attr
, blobmsg_json_format_t cb
, void *priv
, int indent
)
354 struct strbuf s
= {0};
357 setup_strbuf(&s
, attr
, cb
, priv
, indent
);
361 blobmsg_format_element(&s
, attr
, true, false);
368 ret
= realloc(s
.buf
, s
.pos
+ 1);