qobject: Replace qobject_from_jsonf() by qobject_from_jsonf_nofail()
Commit ab45015a96
"qobject: Let qobject_from_jsonf() fail instead of
abort" fails to accomplish its stated aim: the function can still
abort due to its use of &error_abort.
Its rationale for letting it fail is that all remaining users cope
fine with failure. Well, they're just fine with aborting, too; it's
what they do on failure.
Simply reverting the broken commit would bring back the unfortunate
asymmetry between qobject_from_jsonf() and qobject_from_jsonv(): one
aborts, the other returns null. So also rename it to
qobject_from_jsonf_nofail().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180806065344.7103-7-armbru@redhat.com>
This commit is contained in:
parent
bb340eb238
commit
6ce80fd803
|
@ -15,11 +15,13 @@
|
||||||
#define QJSON_H
|
#define QJSON_H
|
||||||
|
|
||||||
QObject *qobject_from_json(const char *string, Error **errp);
|
QObject *qobject_from_json(const char *string, Error **errp);
|
||||||
QObject *qobject_from_jsonf(const char *string, ...) GCC_FMT_ATTR(1, 2);
|
|
||||||
QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
|
QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
|
||||||
GCC_FMT_ATTR(1, 0);
|
GCC_FMT_ATTR(1, 0);
|
||||||
|
|
||||||
QDict *qdict_from_jsonf_nofail(const char *string, ...) GCC_FMT_ATTR(1, 2);
|
QObject *qobject_from_jsonf_nofail(const char *string, ...)
|
||||||
|
GCC_FMT_ATTR(1, 2);
|
||||||
|
QDict *qdict_from_jsonf_nofail(const char *string, ...)
|
||||||
|
GCC_FMT_ATTR(1, 2);
|
||||||
|
|
||||||
QString *qobject_to_json(const QObject *obj);
|
QString *qobject_to_json(const QObject *obj);
|
||||||
QString *qobject_to_json_pretty(const QObject *obj);
|
QString *qobject_to_json_pretty(const QObject *obj);
|
||||||
|
|
|
@ -59,7 +59,12 @@ QObject *qobject_from_json(const char *string, Error **errp)
|
||||||
return qobject_from_jsonv(string, NULL, errp);
|
return qobject_from_jsonv(string, NULL, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
QObject *qobject_from_jsonf(const char *string, ...)
|
/*
|
||||||
|
* Parse @string as JSON value with %-escapes interpolated.
|
||||||
|
* Abort on error. Do not use with untrusted @string.
|
||||||
|
* Return the resulting QObject. It is never null.
|
||||||
|
*/
|
||||||
|
QObject *qobject_from_jsonf_nofail(const char *string, ...)
|
||||||
{
|
{
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
@ -68,6 +73,7 @@ QObject *qobject_from_jsonf(const char *string, ...)
|
||||||
obj = qobject_from_jsonv(string, &ap, &error_abort);
|
obj = qobject_from_jsonv(string, &ap, &error_abort);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
|
assert(obj);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -865,7 +865,8 @@ static void vararg_string(void)
|
||||||
QString *str;
|
QString *str;
|
||||||
|
|
||||||
str = qobject_to(QString,
|
str = qobject_to(QString,
|
||||||
qobject_from_jsonf("%s", test_cases[i].decoded));
|
qobject_from_jsonf_nofail("%s",
|
||||||
|
test_cases[i].decoded));
|
||||||
g_assert(str);
|
g_assert(str);
|
||||||
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
|
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
|
||||||
|
|
||||||
|
@ -998,17 +999,17 @@ static void vararg_number(void)
|
||||||
double valuef = 2.323423423;
|
double valuef = 2.323423423;
|
||||||
int64_t val;
|
int64_t val;
|
||||||
|
|
||||||
qnum = qobject_to(QNum, qobject_from_jsonf("%d", value));
|
qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%d", value));
|
||||||
g_assert(qnum_get_try_int(qnum, &val));
|
g_assert(qnum_get_try_int(qnum, &val));
|
||||||
g_assert_cmpint(val, ==, value);
|
g_assert_cmpint(val, ==, value);
|
||||||
qobject_unref(qnum);
|
qobject_unref(qnum);
|
||||||
|
|
||||||
qnum = qobject_to(QNum, qobject_from_jsonf("%lld", value_ll));
|
qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%lld", value_ll));
|
||||||
g_assert(qnum_get_try_int(qnum, &val));
|
g_assert(qnum_get_try_int(qnum, &val));
|
||||||
g_assert_cmpint(val, ==, value_ll);
|
g_assert_cmpint(val, ==, value_ll);
|
||||||
qobject_unref(qnum);
|
qobject_unref(qnum);
|
||||||
|
|
||||||
qnum = qobject_to(QNum, qobject_from_jsonf("%f", valuef));
|
qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%f", valuef));
|
||||||
g_assert(qnum_get_double(qnum) == valuef);
|
g_assert(qnum_get_double(qnum) == valuef);
|
||||||
qobject_unref(qnum);
|
qobject_unref(qnum);
|
||||||
}
|
}
|
||||||
|
@ -1042,13 +1043,13 @@ static void keyword_literal(void)
|
||||||
|
|
||||||
qobject_unref(qbool);
|
qobject_unref(qbool);
|
||||||
|
|
||||||
qbool = qobject_to(QBool, qobject_from_jsonf("%i", false));
|
qbool = qobject_to(QBool, qobject_from_jsonf_nofail("%i", false));
|
||||||
g_assert(qbool);
|
g_assert(qbool);
|
||||||
g_assert(qbool_get_bool(qbool) == false);
|
g_assert(qbool_get_bool(qbool) == false);
|
||||||
qobject_unref(qbool);
|
qobject_unref(qbool);
|
||||||
|
|
||||||
/* Test that non-zero values other than 1 get collapsed to true */
|
/* Test that non-zero values other than 1 get collapsed to true */
|
||||||
qbool = qobject_to(QBool, qobject_from_jsonf("%i", 2));
|
qbool = qobject_to(QBool, qobject_from_jsonf_nofail("%i", 2));
|
||||||
g_assert(qbool);
|
g_assert(qbool);
|
||||||
g_assert(qbool_get_bool(qbool) == true);
|
g_assert(qbool_get_bool(qbool) == true);
|
||||||
qobject_unref(qbool);
|
qobject_unref(qbool);
|
||||||
|
@ -1298,7 +1299,7 @@ static void simple_varargs(void)
|
||||||
embedded_obj = qobject_from_json("[32, 42]", &error_abort);
|
embedded_obj = qobject_from_json("[32, 42]", &error_abort);
|
||||||
g_assert(embedded_obj != NULL);
|
g_assert(embedded_obj != NULL);
|
||||||
|
|
||||||
obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
|
obj = qobject_from_jsonf_nofail("[%d, 2, %p]", 1, embedded_obj);
|
||||||
g_assert(qlit_equal_qobject(&decoded, obj));
|
g_assert(qlit_equal_qobject(&decoded, obj));
|
||||||
|
|
||||||
qobject_unref(obj);
|
qobject_unref(obj);
|
||||||
|
|
|
@ -77,8 +77,8 @@ void qtest_quit(QTestState *s);
|
||||||
* qtest_qmp:
|
* qtest_qmp:
|
||||||
* @s: #QTestState instance to operate on.
|
* @s: #QTestState instance to operate on.
|
||||||
* @fmt...: QMP message to send to qemu, formatted like
|
* @fmt...: QMP message to send to qemu, formatted like
|
||||||
* qobject_from_jsonf(). See parse_escape() for what's supported
|
* qobject_from_jsonf_nofail(). See parse_escape() for what's
|
||||||
* after '%'.
|
* supported after '%'.
|
||||||
*
|
*
|
||||||
* Sends a QMP message to QEMU and returns the response.
|
* Sends a QMP message to QEMU and returns the response.
|
||||||
*/
|
*/
|
||||||
|
@ -88,8 +88,8 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
|
||||||
* qtest_qmp_send:
|
* qtest_qmp_send:
|
||||||
* @s: #QTestState instance to operate on.
|
* @s: #QTestState instance to operate on.
|
||||||
* @fmt...: QMP message to send to qemu, formatted like
|
* @fmt...: QMP message to send to qemu, formatted like
|
||||||
* qobject_from_jsonf(). See parse_escape() for what's supported
|
* qobject_from_jsonf_nofail(). See parse_escape() for what's
|
||||||
* after '%'.
|
* supported after '%'.
|
||||||
*
|
*
|
||||||
* Sends a QMP message to QEMU and leaves the response in the stream.
|
* Sends a QMP message to QEMU and leaves the response in the stream.
|
||||||
*/
|
*/
|
||||||
|
@ -99,8 +99,8 @@ void qtest_qmp_send(QTestState *s, const char *fmt, ...);
|
||||||
* qtest_qmpv:
|
* qtest_qmpv:
|
||||||
* @s: #QTestState instance to operate on.
|
* @s: #QTestState instance to operate on.
|
||||||
* @fmt: QMP message to send to QEMU, formatted like
|
* @fmt: QMP message to send to QEMU, formatted like
|
||||||
* qobject_from_jsonf(). See parse_escape() for what's supported
|
* qobject_from_jsonf_nofail(). See parse_escape() for what's
|
||||||
* after '%'.
|
* supported after '%'.
|
||||||
* @ap: QMP message arguments
|
* @ap: QMP message arguments
|
||||||
*
|
*
|
||||||
* Sends a QMP message to QEMU and returns the response.
|
* Sends a QMP message to QEMU and returns the response.
|
||||||
|
@ -111,8 +111,8 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
|
||||||
* qtest_qmp_vsend:
|
* qtest_qmp_vsend:
|
||||||
* @s: #QTestState instance to operate on.
|
* @s: #QTestState instance to operate on.
|
||||||
* @fmt: QMP message to send to QEMU, formatted like
|
* @fmt: QMP message to send to QEMU, formatted like
|
||||||
* qobject_from_jsonf(). See parse_escape() for what's supported
|
* qobject_from_jsonf_nofail(). See parse_escape() for what's
|
||||||
* after '%'.
|
* supported after '%'.
|
||||||
* @ap: QMP message arguments
|
* @ap: QMP message arguments
|
||||||
*
|
*
|
||||||
* Sends a QMP message to QEMU and leaves the response in the stream.
|
* Sends a QMP message to QEMU and leaves the response in the stream.
|
||||||
|
@ -552,8 +552,8 @@ static inline void qtest_end(void)
|
||||||
/**
|
/**
|
||||||
* qmp:
|
* qmp:
|
||||||
* @fmt...: QMP message to send to qemu, formatted like
|
* @fmt...: QMP message to send to qemu, formatted like
|
||||||
* qobject_from_jsonf(). See parse_escape() for what's supported
|
* qobject_from_jsonf_nofail(). See parse_escape() for what's
|
||||||
* after '%'.
|
* supported after '%'.
|
||||||
*
|
*
|
||||||
* Sends a QMP message to QEMU and returns the response.
|
* Sends a QMP message to QEMU and returns the response.
|
||||||
*/
|
*/
|
||||||
|
@ -562,8 +562,8 @@ QDict *qmp(const char *fmt, ...);
|
||||||
/**
|
/**
|
||||||
* qmp_send:
|
* qmp_send:
|
||||||
* @fmt...: QMP message to send to qemu, formatted like
|
* @fmt...: QMP message to send to qemu, formatted like
|
||||||
* qobject_from_jsonf(). See parse_escape() for what's supported
|
* qobject_from_jsonf_nofail(). See parse_escape() for what's
|
||||||
* after '%'.
|
* supported after '%'.
|
||||||
*
|
*
|
||||||
* Sends a QMP message to QEMU and leaves the response in the stream.
|
* Sends a QMP message to QEMU and leaves the response in the stream.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue