blobmsg_json.c 7.8 KB

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