2
0

qobject-input-visitor.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * Input Visitor
  3. *
  4. * Copyright (C) 2012-2017 Red Hat, Inc.
  5. * Copyright IBM, Corp. 2011
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. *
  10. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  11. * See the COPYING.LIB file in the top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qapi/error.h"
  16. #include "qapi/qobject-input-visitor.h"
  17. #include "qapi/visitor-impl.h"
  18. #include "qemu/queue.h"
  19. #include "qemu-common.h"
  20. #include "qapi/qmp/qjson.h"
  21. #include "qapi/qmp/types.h"
  22. #include "qapi/qmp/qerror.h"
  23. #include "qemu/cutils.h"
  24. #include "qemu/option.h"
  25. typedef struct StackObject {
  26. const char *name; /* Name of @obj in its parent, if any */
  27. QObject *obj; /* QDict or QList being visited */
  28. void *qapi; /* sanity check that caller uses same pointer */
  29. GHashTable *h; /* If @obj is QDict: unvisited keys */
  30. const QListEntry *entry; /* If @obj is QList: unvisited tail */
  31. unsigned index; /* If @obj is QList: list index of @entry */
  32. QSLIST_ENTRY(StackObject) node; /* parent */
  33. } StackObject;
  34. struct QObjectInputVisitor {
  35. Visitor visitor;
  36. /* Root of visit at visitor creation. */
  37. QObject *root;
  38. bool keyval; /* Assume @root made with keyval_parse() */
  39. /* Stack of objects being visited (all entries will be either
  40. * QDict or QList). */
  41. QSLIST_HEAD(, StackObject) stack;
  42. GString *errname; /* Accumulator for full_name() */
  43. };
  44. static QObjectInputVisitor *to_qiv(Visitor *v)
  45. {
  46. return container_of(v, QObjectInputVisitor, visitor);
  47. }
  48. static const char *full_name_nth(QObjectInputVisitor *qiv, const char *name,
  49. int n)
  50. {
  51. StackObject *so;
  52. char buf[32];
  53. if (qiv->errname) {
  54. g_string_truncate(qiv->errname, 0);
  55. } else {
  56. qiv->errname = g_string_new("");
  57. }
  58. QSLIST_FOREACH(so , &qiv->stack, node) {
  59. if (n) {
  60. n--;
  61. } else if (qobject_type(so->obj) == QTYPE_QDICT) {
  62. g_string_prepend(qiv->errname, name ?: "<anonymous>");
  63. g_string_prepend_c(qiv->errname, '.');
  64. } else {
  65. snprintf(buf, sizeof(buf),
  66. qiv->keyval ? ".%u" : "[%u]",
  67. so->index);
  68. g_string_prepend(qiv->errname, buf);
  69. }
  70. name = so->name;
  71. }
  72. assert(!n);
  73. if (name) {
  74. g_string_prepend(qiv->errname, name);
  75. } else if (qiv->errname->str[0] == '.') {
  76. g_string_erase(qiv->errname, 0, 1);
  77. } else if (!qiv->errname->str[0]) {
  78. return "<anonymous>";
  79. }
  80. return qiv->errname->str;
  81. }
  82. static const char *full_name(QObjectInputVisitor *qiv, const char *name)
  83. {
  84. return full_name_nth(qiv, name, 0);
  85. }
  86. static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv,
  87. const char *name,
  88. bool consume)
  89. {
  90. StackObject *tos;
  91. QObject *qobj;
  92. QObject *ret;
  93. if (QSLIST_EMPTY(&qiv->stack)) {
  94. /* Starting at root, name is ignored. */
  95. assert(qiv->root);
  96. return qiv->root;
  97. }
  98. /* We are in a container; find the next element. */
  99. tos = QSLIST_FIRST(&qiv->stack);
  100. qobj = tos->obj;
  101. assert(qobj);
  102. if (qobject_type(qobj) == QTYPE_QDICT) {
  103. assert(name);
  104. ret = qdict_get(qobject_to_qdict(qobj), name);
  105. if (tos->h && consume && ret) {
  106. bool removed = g_hash_table_remove(tos->h, name);
  107. assert(removed);
  108. }
  109. } else {
  110. assert(qobject_type(qobj) == QTYPE_QLIST);
  111. assert(!name);
  112. if (tos->entry) {
  113. ret = qlist_entry_obj(tos->entry);
  114. if (consume) {
  115. tos->entry = qlist_next(tos->entry);
  116. }
  117. } else {
  118. ret = NULL;
  119. }
  120. if (consume) {
  121. tos->index++;
  122. }
  123. }
  124. return ret;
  125. }
  126. static QObject *qobject_input_get_object(QObjectInputVisitor *qiv,
  127. const char *name,
  128. bool consume, Error **errp)
  129. {
  130. QObject *obj = qobject_input_try_get_object(qiv, name, consume);
  131. if (!obj) {
  132. error_setg(errp, QERR_MISSING_PARAMETER, full_name(qiv, name));
  133. }
  134. return obj;
  135. }
  136. static const char *qobject_input_get_keyval(QObjectInputVisitor *qiv,
  137. const char *name,
  138. Error **errp)
  139. {
  140. QObject *qobj;
  141. QString *qstr;
  142. qobj = qobject_input_get_object(qiv, name, true, errp);
  143. if (!qobj) {
  144. return NULL;
  145. }
  146. qstr = qobject_to_qstring(qobj);
  147. if (!qstr) {
  148. switch (qobject_type(qobj)) {
  149. case QTYPE_QDICT:
  150. case QTYPE_QLIST:
  151. error_setg(errp, "Parameters '%s.*' are unexpected",
  152. full_name(qiv, name));
  153. return NULL;
  154. default:
  155. /* Non-string scalar (should this be an assertion?) */
  156. error_setg(errp, "Internal error: parameter %s invalid",
  157. full_name(qiv, name));
  158. return NULL;
  159. }
  160. }
  161. return qstring_get_str(qstr);
  162. }
  163. static void qdict_add_key(const char *key, QObject *obj, void *opaque)
  164. {
  165. GHashTable *h = opaque;
  166. g_hash_table_insert(h, (gpointer) key, NULL);
  167. }
  168. static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv,
  169. const char *name,
  170. QObject *obj, void *qapi)
  171. {
  172. GHashTable *h;
  173. StackObject *tos = g_new0(StackObject, 1);
  174. assert(obj);
  175. tos->name = name;
  176. tos->obj = obj;
  177. tos->qapi = qapi;
  178. if (qobject_type(obj) == QTYPE_QDICT) {
  179. h = g_hash_table_new(g_str_hash, g_str_equal);
  180. qdict_iter(qobject_to_qdict(obj), qdict_add_key, h);
  181. tos->h = h;
  182. } else {
  183. assert(qobject_type(obj) == QTYPE_QLIST);
  184. tos->entry = qlist_first(qobject_to_qlist(obj));
  185. tos->index = -1;
  186. }
  187. QSLIST_INSERT_HEAD(&qiv->stack, tos, node);
  188. return tos->entry;
  189. }
  190. static void qobject_input_check_struct(Visitor *v, Error **errp)
  191. {
  192. QObjectInputVisitor *qiv = to_qiv(v);
  193. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  194. GHashTableIter iter;
  195. const char *key;
  196. assert(tos && !tos->entry);
  197. g_hash_table_iter_init(&iter, tos->h);
  198. if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
  199. error_setg(errp, "Parameter '%s' is unexpected",
  200. full_name(qiv, key));
  201. }
  202. }
  203. static void qobject_input_stack_object_free(StackObject *tos)
  204. {
  205. if (tos->h) {
  206. g_hash_table_unref(tos->h);
  207. }
  208. g_free(tos);
  209. }
  210. static void qobject_input_pop(Visitor *v, void **obj)
  211. {
  212. QObjectInputVisitor *qiv = to_qiv(v);
  213. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  214. assert(tos && tos->qapi == obj);
  215. QSLIST_REMOVE_HEAD(&qiv->stack, node);
  216. qobject_input_stack_object_free(tos);
  217. }
  218. static void qobject_input_start_struct(Visitor *v, const char *name, void **obj,
  219. size_t size, Error **errp)
  220. {
  221. QObjectInputVisitor *qiv = to_qiv(v);
  222. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  223. if (obj) {
  224. *obj = NULL;
  225. }
  226. if (!qobj) {
  227. return;
  228. }
  229. if (qobject_type(qobj) != QTYPE_QDICT) {
  230. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  231. full_name(qiv, name), "object");
  232. return;
  233. }
  234. qobject_input_push(qiv, name, qobj, obj);
  235. if (obj) {
  236. *obj = g_malloc0(size);
  237. }
  238. }
  239. static void qobject_input_start_list(Visitor *v, const char *name,
  240. GenericList **list, size_t size,
  241. Error **errp)
  242. {
  243. QObjectInputVisitor *qiv = to_qiv(v);
  244. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  245. const QListEntry *entry;
  246. if (list) {
  247. *list = NULL;
  248. }
  249. if (!qobj) {
  250. return;
  251. }
  252. if (qobject_type(qobj) != QTYPE_QLIST) {
  253. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  254. full_name(qiv, name), "array");
  255. return;
  256. }
  257. entry = qobject_input_push(qiv, name, qobj, list);
  258. if (entry && list) {
  259. *list = g_malloc0(size);
  260. }
  261. }
  262. static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail,
  263. size_t size)
  264. {
  265. QObjectInputVisitor *qiv = to_qiv(v);
  266. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  267. assert(tos && tos->obj && qobject_type(tos->obj) == QTYPE_QLIST);
  268. if (!tos->entry) {
  269. return NULL;
  270. }
  271. tail->next = g_malloc0(size);
  272. return tail->next;
  273. }
  274. static void qobject_input_check_list(Visitor *v, Error **errp)
  275. {
  276. QObjectInputVisitor *qiv = to_qiv(v);
  277. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  278. assert(tos && tos->obj && qobject_type(tos->obj) == QTYPE_QLIST);
  279. if (tos->entry) {
  280. error_setg(errp, "Only %u list elements expected in %s",
  281. tos->index + 1, full_name_nth(qiv, NULL, 1));
  282. }
  283. }
  284. static void qobject_input_start_alternate(Visitor *v, const char *name,
  285. GenericAlternate **obj, size_t size,
  286. bool promote_int, Error **errp)
  287. {
  288. QObjectInputVisitor *qiv = to_qiv(v);
  289. QObject *qobj = qobject_input_get_object(qiv, name, false, errp);
  290. if (!qobj) {
  291. *obj = NULL;
  292. return;
  293. }
  294. *obj = g_malloc0(size);
  295. (*obj)->type = qobject_type(qobj);
  296. if (promote_int && (*obj)->type == QTYPE_QINT) {
  297. (*obj)->type = QTYPE_QFLOAT;
  298. }
  299. }
  300. static void qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj,
  301. Error **errp)
  302. {
  303. QObjectInputVisitor *qiv = to_qiv(v);
  304. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  305. QInt *qint;
  306. if (!qobj) {
  307. return;
  308. }
  309. qint = qobject_to_qint(qobj);
  310. if (!qint) {
  311. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  312. full_name(qiv, name), "integer");
  313. return;
  314. }
  315. *obj = qint_get_int(qint);
  316. }
  317. static void qobject_input_type_int64_keyval(Visitor *v, const char *name,
  318. int64_t *obj, Error **errp)
  319. {
  320. QObjectInputVisitor *qiv = to_qiv(v);
  321. const char *str = qobject_input_get_keyval(qiv, name, errp);
  322. if (!str) {
  323. return;
  324. }
  325. if (qemu_strtoi64(str, NULL, 0, obj) < 0) {
  326. /* TODO report -ERANGE more nicely */
  327. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  328. full_name(qiv, name), "integer");
  329. }
  330. }
  331. static void qobject_input_type_uint64(Visitor *v, const char *name,
  332. uint64_t *obj, Error **errp)
  333. {
  334. /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
  335. QObjectInputVisitor *qiv = to_qiv(v);
  336. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  337. QInt *qint;
  338. if (!qobj) {
  339. return;
  340. }
  341. qint = qobject_to_qint(qobj);
  342. if (!qint) {
  343. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  344. full_name(qiv, name), "integer");
  345. return;
  346. }
  347. *obj = qint_get_int(qint);
  348. }
  349. static void qobject_input_type_uint64_keyval(Visitor *v, const char *name,
  350. uint64_t *obj, Error **errp)
  351. {
  352. QObjectInputVisitor *qiv = to_qiv(v);
  353. const char *str = qobject_input_get_keyval(qiv, name, errp);
  354. if (!str) {
  355. return;
  356. }
  357. if (qemu_strtou64(str, NULL, 0, obj) < 0) {
  358. /* TODO report -ERANGE more nicely */
  359. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  360. full_name(qiv, name), "integer");
  361. }
  362. }
  363. static void qobject_input_type_bool(Visitor *v, const char *name, bool *obj,
  364. Error **errp)
  365. {
  366. QObjectInputVisitor *qiv = to_qiv(v);
  367. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  368. QBool *qbool;
  369. if (!qobj) {
  370. return;
  371. }
  372. qbool = qobject_to_qbool(qobj);
  373. if (!qbool) {
  374. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  375. full_name(qiv, name), "boolean");
  376. return;
  377. }
  378. *obj = qbool_get_bool(qbool);
  379. }
  380. static void qobject_input_type_bool_keyval(Visitor *v, const char *name,
  381. bool *obj, Error **errp)
  382. {
  383. QObjectInputVisitor *qiv = to_qiv(v);
  384. const char *str = qobject_input_get_keyval(qiv, name, errp);
  385. if (!str) {
  386. return;
  387. }
  388. if (!strcmp(str, "on")) {
  389. *obj = true;
  390. } else if (!strcmp(str, "off")) {
  391. *obj = false;
  392. } else {
  393. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  394. full_name(qiv, name), "'on' or 'off'");
  395. }
  396. }
  397. static void qobject_input_type_str(Visitor *v, const char *name, char **obj,
  398. Error **errp)
  399. {
  400. QObjectInputVisitor *qiv = to_qiv(v);
  401. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  402. QString *qstr;
  403. *obj = NULL;
  404. if (!qobj) {
  405. return;
  406. }
  407. qstr = qobject_to_qstring(qobj);
  408. if (!qstr) {
  409. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  410. full_name(qiv, name), "string");
  411. return;
  412. }
  413. *obj = g_strdup(qstring_get_str(qstr));
  414. }
  415. static void qobject_input_type_str_keyval(Visitor *v, const char *name,
  416. char **obj, Error **errp)
  417. {
  418. QObjectInputVisitor *qiv = to_qiv(v);
  419. const char *str = qobject_input_get_keyval(qiv, name, errp);
  420. *obj = g_strdup(str);
  421. }
  422. static void qobject_input_type_number(Visitor *v, const char *name, double *obj,
  423. Error **errp)
  424. {
  425. QObjectInputVisitor *qiv = to_qiv(v);
  426. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  427. QInt *qint;
  428. QFloat *qfloat;
  429. if (!qobj) {
  430. return;
  431. }
  432. qint = qobject_to_qint(qobj);
  433. if (qint) {
  434. *obj = qint_get_int(qobject_to_qint(qobj));
  435. return;
  436. }
  437. qfloat = qobject_to_qfloat(qobj);
  438. if (qfloat) {
  439. *obj = qfloat_get_double(qobject_to_qfloat(qobj));
  440. return;
  441. }
  442. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  443. full_name(qiv, name), "number");
  444. }
  445. static void qobject_input_type_number_keyval(Visitor *v, const char *name,
  446. double *obj, Error **errp)
  447. {
  448. QObjectInputVisitor *qiv = to_qiv(v);
  449. const char *str = qobject_input_get_keyval(qiv, name, errp);
  450. char *endp;
  451. if (!str) {
  452. return;
  453. }
  454. errno = 0;
  455. *obj = strtod(str, &endp);
  456. if (errno || endp == str || *endp) {
  457. /* TODO report -ERANGE more nicely */
  458. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  459. full_name(qiv, name), "number");
  460. }
  461. }
  462. static void qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
  463. Error **errp)
  464. {
  465. QObjectInputVisitor *qiv = to_qiv(v);
  466. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  467. *obj = NULL;
  468. if (!qobj) {
  469. return;
  470. }
  471. qobject_incref(qobj);
  472. *obj = qobj;
  473. }
  474. static void qobject_input_type_null(Visitor *v, const char *name, Error **errp)
  475. {
  476. QObjectInputVisitor *qiv = to_qiv(v);
  477. QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
  478. if (!qobj) {
  479. return;
  480. }
  481. if (qobject_type(qobj) != QTYPE_QNULL) {
  482. error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
  483. full_name(qiv, name), "null");
  484. }
  485. }
  486. static void qobject_input_type_size_keyval(Visitor *v, const char *name,
  487. uint64_t *obj, Error **errp)
  488. {
  489. QObjectInputVisitor *qiv = to_qiv(v);
  490. const char *str = qobject_input_get_keyval(qiv, name, errp);
  491. if (!str) {
  492. return;
  493. }
  494. if (qemu_strtosz(str, NULL, obj) < 0) {
  495. /* TODO report -ERANGE more nicely */
  496. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  497. full_name(qiv, name), "size");
  498. }
  499. }
  500. static void qobject_input_optional(Visitor *v, const char *name, bool *present)
  501. {
  502. QObjectInputVisitor *qiv = to_qiv(v);
  503. QObject *qobj = qobject_input_try_get_object(qiv, name, false);
  504. if (!qobj) {
  505. *present = false;
  506. return;
  507. }
  508. *present = true;
  509. }
  510. static void qobject_input_free(Visitor *v)
  511. {
  512. QObjectInputVisitor *qiv = to_qiv(v);
  513. while (!QSLIST_EMPTY(&qiv->stack)) {
  514. StackObject *tos = QSLIST_FIRST(&qiv->stack);
  515. QSLIST_REMOVE_HEAD(&qiv->stack, node);
  516. qobject_input_stack_object_free(tos);
  517. }
  518. qobject_decref(qiv->root);
  519. if (qiv->errname) {
  520. g_string_free(qiv->errname, TRUE);
  521. }
  522. g_free(qiv);
  523. }
  524. static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj)
  525. {
  526. QObjectInputVisitor *v = g_malloc0(sizeof(*v));
  527. assert(obj);
  528. v->visitor.type = VISITOR_INPUT;
  529. v->visitor.start_struct = qobject_input_start_struct;
  530. v->visitor.check_struct = qobject_input_check_struct;
  531. v->visitor.end_struct = qobject_input_pop;
  532. v->visitor.start_list = qobject_input_start_list;
  533. v->visitor.next_list = qobject_input_next_list;
  534. v->visitor.check_list = qobject_input_check_list;
  535. v->visitor.end_list = qobject_input_pop;
  536. v->visitor.start_alternate = qobject_input_start_alternate;
  537. v->visitor.optional = qobject_input_optional;
  538. v->visitor.free = qobject_input_free;
  539. v->root = obj;
  540. qobject_incref(obj);
  541. return v;
  542. }
  543. Visitor *qobject_input_visitor_new(QObject *obj)
  544. {
  545. QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
  546. v->visitor.type_int64 = qobject_input_type_int64;
  547. v->visitor.type_uint64 = qobject_input_type_uint64;
  548. v->visitor.type_bool = qobject_input_type_bool;
  549. v->visitor.type_str = qobject_input_type_str;
  550. v->visitor.type_number = qobject_input_type_number;
  551. v->visitor.type_any = qobject_input_type_any;
  552. v->visitor.type_null = qobject_input_type_null;
  553. return &v->visitor;
  554. }
  555. Visitor *qobject_input_visitor_new_keyval(QObject *obj)
  556. {
  557. QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
  558. v->visitor.type_int64 = qobject_input_type_int64_keyval;
  559. v->visitor.type_uint64 = qobject_input_type_uint64_keyval;
  560. v->visitor.type_bool = qobject_input_type_bool_keyval;
  561. v->visitor.type_str = qobject_input_type_str_keyval;
  562. v->visitor.type_number = qobject_input_type_number_keyval;
  563. v->visitor.type_any = qobject_input_type_any;
  564. v->visitor.type_null = qobject_input_type_null;
  565. v->visitor.type_size = qobject_input_type_size_keyval;
  566. v->keyval = true;
  567. return &v->visitor;
  568. }
  569. Visitor *qobject_input_visitor_new_str(const char *str,
  570. const char *implied_key,
  571. Error **errp)
  572. {
  573. bool is_json = str[0] == '{';
  574. QObject *obj;
  575. QDict *args;
  576. Visitor *v;
  577. if (is_json) {
  578. obj = qobject_from_json(str, errp);
  579. if (!obj) {
  580. /* Work around qobject_from_json() lossage TODO fix that */
  581. if (errp && !*errp) {
  582. error_setg(errp, "JSON parse error");
  583. return NULL;
  584. }
  585. return NULL;
  586. }
  587. args = qobject_to_qdict(obj);
  588. assert(args);
  589. v = qobject_input_visitor_new(QOBJECT(args));
  590. } else {
  591. args = keyval_parse(str, implied_key, errp);
  592. if (!args) {
  593. return NULL;
  594. }
  595. v = qobject_input_visitor_new_keyval(QOBJECT(args));
  596. }
  597. QDECREF(args);
  598. return v;
  599. }