static QString *from_json_str(const char *jstr, bool single, Error **errp) { char quote = single ? '\'' : '"'; char *qjstr = g_strdup_printf("%c%s%c", quote, jstr, quote); QString *ret = qobject_to(QString, qobject_from_json(qjstr, errp)); g_free(qjstr); return ret; }
/* similar to validate_test_init(), but does not expect a string * literal/format json_string argument and so can be used for * programatically generated strings (and we can't pass in programatically * generated strings via %s format parameters since qobject_from_jsonv() * will wrap those in double-quotes and treat the entire object as a * string) */ static Visitor *validate_test_init_raw(TestInputVisitorData *data, const char *json_string) { Visitor *v; data->obj = qobject_from_json(json_string); g_assert(data->obj != NULL); data->qiv = qmp_input_visitor_new_strict(data->obj); g_assert(data->qiv != NULL); v = qmp_input_get_visitor(data->qiv); g_assert(v != NULL); return v; }
static QAuthZ * qauthz_list_file_load(QAuthZListFile *fauthz, Error **errp) { GError *err = NULL; gchar *content = NULL; gsize len; QObject *obj = NULL; QDict *pdict; Visitor *v = NULL; QAuthZ *ret = NULL; trace_qauthz_list_file_load(fauthz, fauthz->filename); if (!g_file_get_contents(fauthz->filename, &content, &len, &err)) { error_setg(errp, "Unable to read '%s': %s", fauthz->filename, err->message); goto cleanup; } obj = qobject_from_json(content, errp); if (!obj) { goto cleanup; } pdict = qobject_to(QDict, obj); if (!pdict) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "obj", "dict"); goto cleanup; } v = qobject_input_visitor_new(obj); ret = (QAuthZ *)user_creatable_add_type(TYPE_QAUTHZ_LIST, NULL, pdict, v, errp); cleanup: visit_free(v); qobject_unref(obj); if (err) { g_error_free(err); } g_free(content); return ret; }
static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json, Error **errp) { QList *keypairs; QString *name; QString *value; const char *key; size_t remaining; int ret = 0; if (!keypairs_json) { return ret; } keypairs = qobject_to(QList, qobject_from_json(keypairs_json, &error_abort)); remaining = qlist_size(keypairs) / 2; assert(remaining); while (remaining--) { name = qobject_to(QString, qlist_pop(keypairs)); value = qobject_to(QString, qlist_pop(keypairs)); assert(name && value); key = qstring_get_str(name); ret = rados_conf_set(cluster, key, qstring_get_str(value)); qobject_unref(value); if (ret < 0) { error_setg_errno(errp, -ret, "invalid conf option %s", key); qobject_unref(name); ret = -EINVAL; break; } qobject_unref(name); } qobject_unref(keypairs); return ret; }
/* test core visitor methods */ static void test_visitor_core(void) { QmpOutputVisitor *mo; QmpInputVisitor *mi; Visitor *v; TestStruct ts = { 42, 82 }; TestStruct *pts = &ts; TestStructList *lts = NULL; Error *err = NULL; QObject *obj; QList *qlist; QDict *qdict; QString *str; int64_t value = 0; mo = qmp_output_visitor_new(); v = qmp_output_get_visitor(mo); visit_type_TestStruct(v, &pts, NULL, &err); obj = qmp_output_get_qobject(mo); str = qobject_to_json(obj); printf("%s\n", qstring_get_str(str)); QDECREF(str); obj = QOBJECT(qint_from_int(0x42)); mi = qmp_input_visitor_new(obj); v = qmp_input_get_visitor(mi); visit_type_int(v, &value, NULL, &err); if (err) { g_error("%s", error_get_pretty(err)); } g_assert(value == 0x42); qobject_decref(obj); obj = qobject_from_json("{'x': 42, 'y': 84}"); mi = qmp_input_visitor_new(obj); v = qmp_input_get_visitor(mi); pts = NULL; visit_type_TestStruct(v, &pts, NULL, &err); if (err) { g_error("%s", error_get_pretty(err)); } g_assert(pts != NULL); g_assert(pts->x == 42); g_assert(pts->y == 84); qobject_decref(obj); g_free(pts); /* test list input visitor */ obj = qobject_from_json("[{'x': 42, 'y': 84}, {'x': 12, 'y': 24}]"); mi = qmp_input_visitor_new(obj); v = qmp_input_get_visitor(mi); visit_type_TestStructList(v, <s, NULL, &err); if (err) { g_error("%s", error_get_pretty(err)); } g_assert(lts != NULL); g_assert(lts->value->x == 42); g_assert(lts->value->y == 84); g_assert(lts->next != NULL); g_assert(lts->next->value->x == 12); g_assert(lts->next->value->y == 24); g_assert(lts->next->next == NULL); qobject_decref(obj); /* test list output visitor */ mo = qmp_output_visitor_new(); v = qmp_output_get_visitor(mo); visit_type_TestStructList(v, <s, NULL, &err); if (err) { g_error("%s", error_get_pretty(err)); } obj = qmp_output_get_qobject(mo); g_print("obj: %s\n", qstring_get_str(qobject_to_json(obj))); qlist = qobject_to_qlist(obj); assert(qlist); obj = qlist_pop(qlist); qdict = qobject_to_qdict(obj); assert(qdict); assert(qdict_get_int(qdict, "x") == 42); assert(qdict_get_int(qdict, "y") == 84); qobject_decref(obj); obj = qlist_pop(qlist); qdict = qobject_to_qdict(obj); assert(qdict); assert(qdict_get_int(qdict, "x") == 12); assert(qdict_get_int(qdict, "y") == 24); qobject_decref(obj); qmp_output_visitor_cleanup(mo); QDECREF(qlist); }