]> git.ipfire.org Git - thirdparty/qemu.git/commitdiff
qobject: New qdict_from_jsonf_nofail()
authorMarkus Armbruster <armbru@redhat.com>
Tue, 3 Jul 2018 08:53:47 +0000 (10:53 +0200)
committerMichael Roth <mdroth@linux.vnet.ibm.com>
Tue, 24 Jul 2018 20:05:01 +0000 (15:05 -0500)
Many uses of qobject_from_jsonf() convert JSON objects.  Create new
convenience function qdict_from_jsonf_nofail() that includes the
conversion to QDict.  The next few commits will put it to use.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180703085358.13941-22-armbru@redhat.com>
(cherry picked from commit a193352ff9c7cd2cd07846118bc49921d0f53af8)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
include/qapi/qmp/qjson.h
qobject/qjson.c

index b274ac3a8678982957c688306d4fcdc486ddfb35..43b2ce2f337ea993f8cd9ef2760fa0362904425b 100644 (file)
@@ -19,6 +19,8 @@ QObject *qobject_from_jsonf(const char *string, ...) GCC_FMT_ATTR(1, 2);
 QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
     GCC_FMT_ATTR(1, 0);
 
+QDict *qdict_from_jsonf_nofail(const char *string, ...) GCC_FMT_ATTR(1, 2);
+
 QString *qobject_to_json(const QObject *obj);
 QString *qobject_to_json_pretty(const QObject *obj);
 
index 655d38adf1bc7e53fcd312bfa95ebedded4c2c75..3022b0d580e61a15839923db095d14529d12e3c8 100644 (file)
@@ -76,6 +76,24 @@ QObject *qobject_from_jsonf(const char *string, ...)
     return obj;
 }
 
+/*
+ * Parse @string as JSON object with %-escapes interpolated.
+ * Abort on error.  Do not use with untrusted @string.
+ * Return the resulting QDict.  It is never null.
+ */
+QDict *qdict_from_jsonf_nofail(const char *string, ...)
+{
+    QDict *obj;
+    va_list ap;
+
+    va_start(ap, string);
+    obj = qobject_to(QDict, qobject_from_jsonv(string, &ap, &error_abort));
+    va_end(ap);
+
+    assert(obj);
+    return obj;
+}
+
 typedef struct ToJsonIterState
 {
     int indent;