From: Markus Armbruster Date: Tue, 3 Jul 2018 08:53:47 +0000 (+0200) Subject: qobject: New qdict_from_jsonf_nofail() X-Git-Tag: v2.12.1~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=441784598e7cf1695657cd958a5f6e3326da01f9;p=thirdparty%2Fqemu.git qobject: New qdict_from_jsonf_nofail() 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 Reviewed-by: Eric Blake Message-Id: <20180703085358.13941-22-armbru@redhat.com> (cherry picked from commit a193352ff9c7cd2cd07846118bc49921d0f53af8) Signed-off-by: Michael Roth --- diff --git a/include/qapi/qmp/qjson.h b/include/qapi/qmp/qjson.h index b274ac3a867..43b2ce2f337 100644 --- a/include/qapi/qmp/qjson.h +++ b/include/qapi/qmp/qjson.h @@ -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); diff --git a/qobject/qjson.c b/qobject/qjson.c index 655d38adf1b..3022b0d580e 100644 --- a/qobject/qjson.c +++ b/qobject/qjson.c @@ -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;