blobmsg_json.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_null:
  66. blobmsg_add_field(b, BLOBMSG_TYPE_UNSPEC, name, NULL, 0);
  67. break;
  68. default:
  69. return false;
  70. }
  71. return ret;
  72. }
  73. static bool __blobmsg_add_json(struct blob_buf *b, json_object *obj)
  74. {
  75. bool ret = false;
  76. if (!obj)
  77. return false;
  78. if (json_object_get_type(obj) != json_type_object)
  79. goto out;
  80. ret = blobmsg_add_object(b, obj);
  81. out:
  82. json_object_put(obj);
  83. return ret;
  84. }
  85. bool blobmsg_add_json_from_file(struct blob_buf *b, const char *file)
  86. {
  87. return __blobmsg_add_json(b, json_object_from_file(file));
  88. }
  89. bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
  90. {
  91. return __blobmsg_add_json(b, json_tokener_parse(str));
  92. }
  93. struct strbuf {
  94. int len;
  95. int pos;
  96. char *buf;
  97. blobmsg_json_format_t custom_format;
  98. void *priv;
  99. bool indent;
  100. int indent_level;
  101. };
  102. static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
  103. {
  104. size_t new_len;
  105. char *new_buf;
  106. if (len <= 0)
  107. return true;
  108. if (s->pos + len >= s->len) {
  109. new_len = s->len + 16 + len;
  110. new_buf = realloc(s->buf, new_len);
  111. if (!new_buf)
  112. return false;
  113. s->len = new_len;
  114. s->buf = new_buf;
  115. }
  116. memcpy(s->buf + s->pos, c, len);
  117. s->pos += len;
  118. return true;
  119. }
  120. static void add_separator(struct strbuf *s)
  121. {
  122. const char *indent_chars = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  123. int len;
  124. if (!s->indent)
  125. return;
  126. len = s->indent_level + 1;
  127. if (len > strlen(indent_chars))
  128. len = strlen(indent_chars);
  129. blobmsg_puts(s, indent_chars, len);
  130. }
  131. static void blobmsg_format_string(struct strbuf *s, const char *str)
  132. {
  133. const unsigned char *p, *last, *end;
  134. char buf[8] = "\\u00";
  135. end = (unsigned char *) str + strlen(str);
  136. blobmsg_puts(s, "\"", 1);
  137. for (p = (unsigned char *) str, last = p; *p; p++) {
  138. char escape = '\0';
  139. int len;
  140. switch(*p) {
  141. case '\b':
  142. escape = 'b';
  143. break;
  144. case '\n':
  145. escape = 'n';
  146. break;
  147. case '\t':
  148. escape = 't';
  149. break;
  150. case '\r':
  151. escape = 'r';
  152. break;
  153. case '"':
  154. case '\\':
  155. case '/':
  156. escape = *p;
  157. break;
  158. default:
  159. if (*p < ' ')
  160. escape = 'u';
  161. break;
  162. }
  163. if (!escape)
  164. continue;
  165. if (p > last)
  166. blobmsg_puts(s, (char *) last, p - last);
  167. last = p + 1;
  168. buf[1] = escape;
  169. if (escape == 'u') {
  170. sprintf(buf + 4, "%02x", (unsigned char) *p);
  171. len = 6;
  172. } else {
  173. len = 2;
  174. }
  175. blobmsg_puts(s, buf, len);
  176. }
  177. blobmsg_puts(s, (char *) last, end - last);
  178. blobmsg_puts(s, "\"", 1);
  179. }
  180. static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
  181. static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head)
  182. {
  183. const char *data_str;
  184. char buf[32];
  185. void *data;
  186. int len;
  187. if (!blobmsg_check_attr(attr, false))
  188. return;
  189. if (!array && blobmsg_name(attr)[0]) {
  190. blobmsg_format_string(s, blobmsg_name(attr));
  191. blobmsg_puts(s, ": ", s->indent ? 2 : 1);
  192. }
  193. data = blobmsg_data(attr);
  194. len = blobmsg_data_len(attr);
  195. if (!head && s->custom_format) {
  196. data_str = s->custom_format(s->priv, attr);
  197. if (data_str)
  198. goto out;
  199. }
  200. data_str = buf;
  201. switch(blob_id(attr)) {
  202. case BLOBMSG_TYPE_UNSPEC:
  203. sprintf(buf, "null");
  204. break;
  205. case BLOBMSG_TYPE_BOOL:
  206. sprintf(buf, "%s", *(uint8_t *)data ? "true" : "false");
  207. break;
  208. case BLOBMSG_TYPE_INT16:
  209. sprintf(buf, "%d", be16_to_cpu(*(uint16_t *)data));
  210. break;
  211. case BLOBMSG_TYPE_INT32:
  212. sprintf(buf, "%d", (int32_t) be32_to_cpu(*(uint32_t *)data));
  213. break;
  214. case BLOBMSG_TYPE_INT64:
  215. sprintf(buf, "%" PRId64, (int64_t) be64_to_cpu(*(uint64_t *)data));
  216. break;
  217. case BLOBMSG_TYPE_STRING:
  218. blobmsg_format_string(s, data);
  219. return;
  220. case BLOBMSG_TYPE_ARRAY:
  221. blobmsg_format_json_list(s, data, len, true);
  222. return;
  223. case BLOBMSG_TYPE_TABLE:
  224. blobmsg_format_json_list(s, data, len, false);
  225. return;
  226. }
  227. out:
  228. blobmsg_puts(s, data_str, strlen(data_str));
  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. blobmsg_puts(s, (array ? "[" : "{" ), 1);
  236. s->indent_level++;
  237. add_separator(s);
  238. __blob_for_each_attr(pos, attr, rem) {
  239. if (!first) {
  240. blobmsg_puts(s, ",", 1);
  241. add_separator(s);
  242. }
  243. blobmsg_format_element(s, pos, array, false);
  244. first = false;
  245. }
  246. s->indent_level--;
  247. add_separator(s);
  248. blobmsg_puts(s, (array ? "]" : "}"), 1);
  249. }
  250. char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
  251. {
  252. struct strbuf s;
  253. bool array;
  254. char *ret;
  255. s.len = blob_len(attr);
  256. s.pos = 0;
  257. s.custom_format = cb;
  258. s.priv = priv;
  259. s.indent = false;
  260. s.buf = malloc(s.len);
  261. if (!s.buf)
  262. return NULL;
  263. if (indent >= 0) {
  264. s.indent = true;
  265. s.indent_level = indent;
  266. }
  267. array = blob_is_extended(attr) &&
  268. blobmsg_type(attr) == BLOBMSG_TYPE_ARRAY;
  269. if (list)
  270. blobmsg_format_json_list(&s, blobmsg_data(attr), blobmsg_data_len(attr), array);
  271. else
  272. blobmsg_format_element(&s, attr, false, false);
  273. if (!s.len) {
  274. free(s.buf);
  275. return NULL;
  276. }
  277. ret = realloc(s.buf, s.pos + 1);
  278. if (!ret) {
  279. free(s.buf);
  280. return NULL;
  281. }
  282. ret[s.pos] = 0;
  283. return ret;
  284. }