qjson.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * QObject JSON integration
  3. *
  4. * Copyright IBM, Corp. 2009
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "qapi/qmp/json-parser.h"
  16. #include "qapi/qmp/qjson.h"
  17. #include "qapi/qmp/qbool.h"
  18. #include "qapi/qmp/qdict.h"
  19. #include "qapi/qmp/qlist.h"
  20. #include "qapi/qmp/qnum.h"
  21. #include "qapi/qmp/qstring.h"
  22. #include "qemu/unicode.h"
  23. typedef struct JSONParsingState
  24. {
  25. JSONMessageParser parser;
  26. QObject *result;
  27. Error *err;
  28. } JSONParsingState;
  29. static void consume_json(void *opaque, QObject *json, Error *err)
  30. {
  31. JSONParsingState *s = opaque;
  32. assert(!json != !err);
  33. assert(!s->result || !s->err);
  34. if (s->result) {
  35. qobject_unref(s->result);
  36. s->result = NULL;
  37. error_setg(&s->err, "Expecting at most one JSON value");
  38. }
  39. if (s->err) {
  40. qobject_unref(json);
  41. error_free(err);
  42. return;
  43. }
  44. s->result = json;
  45. s->err = err;
  46. }
  47. /*
  48. * Parse @string as JSON value.
  49. * If @ap is non-null, interpolate %-escapes.
  50. * Takes ownership of %p arguments.
  51. * On success, return the JSON value.
  52. * On failure, store an error through @errp and return NULL.
  53. * Ownership of %p arguments becomes indeterminate then. To avoid
  54. * leaks, callers passing %p must terminate on error, e.g. by passing
  55. * &error_abort.
  56. */
  57. static QObject *qobject_from_jsonv(const char *string, va_list *ap,
  58. Error **errp)
  59. {
  60. JSONParsingState state = {};
  61. json_message_parser_init(&state.parser, consume_json, &state, ap);
  62. json_message_parser_feed(&state.parser, string, strlen(string));
  63. json_message_parser_flush(&state.parser);
  64. json_message_parser_destroy(&state.parser);
  65. if (!state.result && !state.err) {
  66. error_setg(&state.err, "Expecting a JSON value");
  67. }
  68. error_propagate(errp, state.err);
  69. return state.result;
  70. }
  71. QObject *qobject_from_json(const char *string, Error **errp)
  72. {
  73. return qobject_from_jsonv(string, NULL, errp);
  74. }
  75. /*
  76. * Parse @string as JSON value with %-escapes interpolated.
  77. * Abort on error. Do not use with untrusted @string.
  78. * Return the resulting QObject. It is never null.
  79. */
  80. QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
  81. {
  82. va_list ap_copy;
  83. QObject *obj;
  84. /* va_copy() is needed when va_list is an array type */
  85. va_copy(ap_copy, ap);
  86. obj = qobject_from_jsonv(string, &ap_copy, &error_abort);
  87. va_end(ap_copy);
  88. assert(obj);
  89. return obj;
  90. }
  91. /*
  92. * Parse @string as JSON value with %-escapes interpolated.
  93. * Abort on error. Do not use with untrusted @string.
  94. * Return the resulting QObject. It is never null.
  95. */
  96. QObject *qobject_from_jsonf_nofail(const char *string, ...)
  97. {
  98. QObject *obj;
  99. va_list ap;
  100. va_start(ap, string);
  101. obj = qobject_from_vjsonf_nofail(string, ap);
  102. va_end(ap);
  103. return obj;
  104. }
  105. /*
  106. * Parse @string as JSON object with %-escapes interpolated.
  107. * Abort on error. Do not use with untrusted @string.
  108. * Return the resulting QDict. It is never null.
  109. */
  110. QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
  111. {
  112. QDict *qdict;
  113. qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
  114. assert(qdict);
  115. return qdict;
  116. }
  117. /*
  118. * Parse @string as JSON object with %-escapes interpolated.
  119. * Abort on error. Do not use with untrusted @string.
  120. * Return the resulting QDict. It is never null.
  121. */
  122. QDict *qdict_from_jsonf_nofail(const char *string, ...)
  123. {
  124. QDict *qdict;
  125. va_list ap;
  126. va_start(ap, string);
  127. qdict = qdict_from_vjsonf_nofail(string, ap);
  128. va_end(ap);
  129. return qdict;
  130. }
  131. typedef struct ToJsonIterState
  132. {
  133. int indent;
  134. int pretty;
  135. int count;
  136. QString *str;
  137. } ToJsonIterState;
  138. static void to_json(const QObject *obj, QString *str, int pretty, int indent);
  139. static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
  140. {
  141. ToJsonIterState *s = opaque;
  142. QString *qkey;
  143. int j;
  144. if (s->count) {
  145. qstring_append(s->str, s->pretty ? "," : ", ");
  146. }
  147. if (s->pretty) {
  148. qstring_append(s->str, "\n");
  149. for (j = 0 ; j < s->indent ; j++)
  150. qstring_append(s->str, " ");
  151. }
  152. qkey = qstring_from_str(key);
  153. to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
  154. qobject_unref(qkey);
  155. qstring_append(s->str, ": ");
  156. to_json(obj, s->str, s->pretty, s->indent);
  157. s->count++;
  158. }
  159. static void to_json_list_iter(QObject *obj, void *opaque)
  160. {
  161. ToJsonIterState *s = opaque;
  162. int j;
  163. if (s->count) {
  164. qstring_append(s->str, s->pretty ? "," : ", ");
  165. }
  166. if (s->pretty) {
  167. qstring_append(s->str, "\n");
  168. for (j = 0 ; j < s->indent ; j++)
  169. qstring_append(s->str, " ");
  170. }
  171. to_json(obj, s->str, s->pretty, s->indent);
  172. s->count++;
  173. }
  174. static void to_json(const QObject *obj, QString *str, int pretty, int indent)
  175. {
  176. switch (qobject_type(obj)) {
  177. case QTYPE_QNULL:
  178. qstring_append(str, "null");
  179. break;
  180. case QTYPE_QNUM: {
  181. QNum *val = qobject_to(QNum, obj);
  182. char *buffer = qnum_to_string(val);
  183. qstring_append(str, buffer);
  184. g_free(buffer);
  185. break;
  186. }
  187. case QTYPE_QSTRING: {
  188. QString *val = qobject_to(QString, obj);
  189. const char *ptr;
  190. int cp;
  191. char buf[16];
  192. char *end;
  193. ptr = qstring_get_str(val);
  194. qstring_append(str, "\"");
  195. for (; *ptr; ptr = end) {
  196. cp = mod_utf8_codepoint(ptr, 6, &end);
  197. switch (cp) {
  198. case '\"':
  199. qstring_append(str, "\\\"");
  200. break;
  201. case '\\':
  202. qstring_append(str, "\\\\");
  203. break;
  204. case '\b':
  205. qstring_append(str, "\\b");
  206. break;
  207. case '\f':
  208. qstring_append(str, "\\f");
  209. break;
  210. case '\n':
  211. qstring_append(str, "\\n");
  212. break;
  213. case '\r':
  214. qstring_append(str, "\\r");
  215. break;
  216. case '\t':
  217. qstring_append(str, "\\t");
  218. break;
  219. default:
  220. if (cp < 0) {
  221. cp = 0xFFFD; /* replacement character */
  222. }
  223. if (cp > 0xFFFF) {
  224. /* beyond BMP; need a surrogate pair */
  225. snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
  226. 0xD800 + ((cp - 0x10000) >> 10),
  227. 0xDC00 + ((cp - 0x10000) & 0x3FF));
  228. } else if (cp < 0x20 || cp >= 0x7F) {
  229. snprintf(buf, sizeof(buf), "\\u%04X", cp);
  230. } else {
  231. buf[0] = cp;
  232. buf[1] = 0;
  233. }
  234. qstring_append(str, buf);
  235. }
  236. };
  237. qstring_append(str, "\"");
  238. break;
  239. }
  240. case QTYPE_QDICT: {
  241. ToJsonIterState s;
  242. QDict *val = qobject_to(QDict, obj);
  243. s.count = 0;
  244. s.str = str;
  245. s.indent = indent + 1;
  246. s.pretty = pretty;
  247. qstring_append(str, "{");
  248. qdict_iter(val, to_json_dict_iter, &s);
  249. if (pretty) {
  250. int j;
  251. qstring_append(str, "\n");
  252. for (j = 0 ; j < indent ; j++)
  253. qstring_append(str, " ");
  254. }
  255. qstring_append(str, "}");
  256. break;
  257. }
  258. case QTYPE_QLIST: {
  259. ToJsonIterState s;
  260. QList *val = qobject_to(QList, obj);
  261. s.count = 0;
  262. s.str = str;
  263. s.indent = indent + 1;
  264. s.pretty = pretty;
  265. qstring_append(str, "[");
  266. qlist_iter(val, (void *)to_json_list_iter, &s);
  267. if (pretty) {
  268. int j;
  269. qstring_append(str, "\n");
  270. for (j = 0 ; j < indent ; j++)
  271. qstring_append(str, " ");
  272. }
  273. qstring_append(str, "]");
  274. break;
  275. }
  276. case QTYPE_QBOOL: {
  277. QBool *val = qobject_to(QBool, obj);
  278. if (qbool_get_bool(val)) {
  279. qstring_append(str, "true");
  280. } else {
  281. qstring_append(str, "false");
  282. }
  283. break;
  284. }
  285. default:
  286. abort();
  287. }
  288. }
  289. QString *qobject_to_json(const QObject *obj)
  290. {
  291. QString *str = qstring_new();
  292. to_json(obj, str, 0, 0);
  293. return str;
  294. }
  295. QString *qobject_to_json_pretty(const QObject *obj)
  296. {
  297. QString *str = qstring_new();
  298. to_json(obj, str, 1, 0);
  299. return str;
  300. }