Skip to content

Commit

Permalink
qobject: New qdict_from_jsonf_nofail()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Markus Armbruster committed Jul 3, 2018
1 parent cab5ad8 commit a193352
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/qapi/qmp/qjson.h
Expand Up @@ -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);

Expand Down
18 changes: 18 additions & 0 deletions qobject/qjson.c
Expand Up @@ -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;
Expand Down

0 comments on commit a193352

Please sign in to comment.