From eab3a4678b07267c39e7290a6e9e7690b1d2a521 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 11 Dec 2020 18:11:37 +0100 Subject: qobject: Change qobject_to_json()'s value to GString qobject_to_json() and qobject_to_json_pretty() build a GString, then covert it to QString. Just one of the callers actually needs a QString: qemu_rbd_parse_filename(). A few others need a string they can modify: qmp_send_response(), qga's send_response(), to_json_str(), and qmp_fd_vsend_fds(). The remainder just need a string. Change qobject_to_json() and qobject_to_json_pretty() to return the GString. qemu_rbd_parse_filename() now has to convert to QString. All others save a QString temporary. to_json_str() actually becomes a bit simpler, because GString provides more convenient modification functions. Signed-off-by: Markus Armbruster Message-Id: <20201211171152.146877-6-armbru@redhat.com> --- tests/check-qjson.c | 56 ++++++++++++++++++-------------------- tests/qtest/libqtest.c | 20 ++++++-------- tests/test-visitor-serialization.c | 6 ++-- 3 files changed, 39 insertions(+), 43 deletions(-) (limited to 'tests') diff --git a/tests/check-qjson.c b/tests/check-qjson.c index ca8fb816e9..337add0838 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -35,17 +35,15 @@ static QString *from_json_str(const char *jstr, bool single, Error **errp) static char *to_json_str(QString *str) { - QString *json = qobject_to_json(QOBJECT(str)); - char *jstr; + GString *json = qobject_to_json(QOBJECT(str)); if (!json) { return NULL; } /* peel off double quotes */ - jstr = g_strndup(qstring_get_str(json) + 1, - qstring_get_length(json) - 2); - qobject_unref(json); - return jstr; + g_string_truncate(json, json->len - 1); + g_string_erase(json, 0, 1); + return g_string_free(json, false); } static void escaped_string(void) @@ -809,7 +807,7 @@ static void int_number(void) QNum *qnum; int64_t ival; uint64_t uval; - QString *str; + GString *str; for (i = 0; test_cases[i].encoded; i++) { qnum = qobject_to(QNum, @@ -828,9 +826,9 @@ static void int_number(void) (double)test_cases[i].decoded); str = qobject_to_json(QOBJECT(qnum)); - g_assert_cmpstr(qstring_get_str(str), ==, + g_assert_cmpstr(str->str, ==, test_cases[i].reencoded ?: test_cases[i].encoded); - qobject_unref(str); + g_string_free(str, true); qobject_unref(qnum); } @@ -851,7 +849,7 @@ static void uint_number(void) QNum *qnum; int64_t ival; uint64_t uval; - QString *str; + GString *str; for (i = 0; test_cases[i].encoded; i++) { qnum = qobject_to(QNum, @@ -865,9 +863,9 @@ static void uint_number(void) (double)test_cases[i].decoded); str = qobject_to_json(QOBJECT(qnum)); - g_assert_cmpstr(qstring_get_str(str), ==, + g_assert_cmpstr(str->str, ==, test_cases[i].reencoded ?: test_cases[i].encoded); - qobject_unref(str); + g_string_free(str, true); qobject_unref(qnum); } @@ -892,7 +890,7 @@ static void float_number(void) QNum *qnum; int64_t ival; uint64_t uval; - QString *str; + GString *str; for (i = 0; test_cases[i].encoded; i++) { qnum = qobject_to(QNum, @@ -904,9 +902,9 @@ static void float_number(void) g_assert(!qnum_get_try_uint(qnum, &uval)); str = qobject_to_json(QOBJECT(qnum)); - g_assert_cmpstr(qstring_get_str(str), ==, + g_assert_cmpstr(str->str, ==, test_cases[i].reencoded ?: test_cases[i].encoded); - qobject_unref(str); + g_string_free(str, true); qobject_unref(qnum); } @@ -917,7 +915,7 @@ static void keyword_literal(void) QObject *obj; QBool *qbool; QNull *null; - QString *str; + GString *str; obj = qobject_from_json("true", &error_abort); qbool = qobject_to(QBool, obj); @@ -925,8 +923,8 @@ static void keyword_literal(void) g_assert(qbool_get_bool(qbool) == true); str = qobject_to_json(obj); - g_assert(strcmp(qstring_get_str(str), "true") == 0); - qobject_unref(str); + g_assert_cmpstr(str->str, ==, "true"); + g_string_free(str, true); qobject_unref(qbool); @@ -936,8 +934,8 @@ static void keyword_literal(void) g_assert(qbool_get_bool(qbool) == false); str = qobject_to_json(obj); - g_assert(strcmp(qstring_get_str(str), "false") == 0); - qobject_unref(str); + g_assert_cmpstr(str->str, ==, "false"); + g_string_free(str, true); qobject_unref(qbool); @@ -1087,7 +1085,7 @@ static void simple_dict(void) for (i = 0; test_cases[i].encoded; i++) { QObject *obj; - QString *str; + GString *str; obj = qobject_from_json(test_cases[i].encoded, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); @@ -1095,10 +1093,10 @@ static void simple_dict(void) str = qobject_to_json(obj); qobject_unref(obj); - obj = qobject_from_json(qstring_get_str(str), &error_abort); + obj = qobject_from_json(str->str, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); qobject_unref(obj); - qobject_unref(str); + g_string_free(str, true); } } @@ -1196,7 +1194,7 @@ static void simple_list(void) for (i = 0; test_cases[i].encoded; i++) { QObject *obj; - QString *str; + GString *str; obj = qobject_from_json(test_cases[i].encoded, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); @@ -1204,10 +1202,10 @@ static void simple_list(void) str = qobject_to_json(obj); qobject_unref(obj); - obj = qobject_from_json(qstring_get_str(str), &error_abort); + obj = qobject_from_json(str->str, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); qobject_unref(obj); - qobject_unref(str); + g_string_free(str, true); } } @@ -1258,7 +1256,7 @@ static void simple_whitespace(void) for (i = 0; test_cases[i].encoded; i++) { QObject *obj; - QString *str; + GString *str; obj = qobject_from_json(test_cases[i].encoded, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); @@ -1266,11 +1264,11 @@ static void simple_whitespace(void) str = qobject_to_json(obj); qobject_unref(obj); - obj = qobject_from_json(qstring_get_str(str), &error_abort); + obj = qobject_from_json(str->str, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); qobject_unref(obj); - qobject_unref(str); + g_string_free(str, true); } } diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c index 213fa4f8fe..8e93b0a707 100644 --- a/tests/qtest/libqtest.c +++ b/tests/qtest/libqtest.c @@ -652,27 +652,25 @@ void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num, /* No need to send anything for an empty QObject. */ if (qobj) { int log = getenv("QTEST_LOG") != NULL; - QString *qstr = qobject_to_json(qobj); - const char *str; + GString *str = qobject_to_json(qobj); /* * BUG: QMP doesn't react to input until it sees a newline, an * object, or an array. Work-around: give it a newline. */ - qstring_append_chr(qstr, '\n'); - str = qstring_get_str(qstr); + g_string_append_c(str, '\n'); if (log) { - fprintf(stderr, "%s", str); + fprintf(stderr, "%s", str->str); } /* Send QMP request */ if (fds && fds_num > 0) { - socket_send_fds(fd, fds, fds_num, str, qstring_get_length(qstr)); + socket_send_fds(fd, fds, fds_num, str->str, str->len); } else { - socket_send(fd, str, qstring_get_length(qstr)); + socket_send(fd, str->str, str->len); } - qobject_unref(qstr); + g_string_free(str, true); qobject_unref(qobj); } } @@ -1197,9 +1195,9 @@ void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...) g_assert(response); if (!qdict_haskey(response, "return")) { - QString *s = qobject_to_json_pretty(QOBJECT(response), true); - g_test_message("%s", qstring_get_str(s)); - qobject_unref(s); + GString *s = qobject_to_json_pretty(QOBJECT(response), true); + g_test_message("%s", s->str); + g_string_free(s, true); } g_assert(qdict_haskey(response, "return")); qobject_unref(response); diff --git a/tests/test-visitor-serialization.c b/tests/test-visitor-serialization.c index dfe120a50d..4629958647 100644 --- a/tests/test-visitor-serialization.c +++ b/tests/test-visitor-serialization.c @@ -957,15 +957,15 @@ static void qmp_deserialize(void **native_out, void *datap, VisitorFunc visit, Error **errp) { QmpSerializeData *d = datap; - QString *output_json; + GString *output_json; QObject *obj_orig, *obj; visit_complete(d->qov, &d->obj); obj_orig = d->obj; output_json = qobject_to_json(obj_orig); - obj = qobject_from_json(qstring_get_str(output_json), &error_abort); + obj = qobject_from_json(output_json->str, &error_abort); - qobject_unref(output_json); + g_string_free(output_json, true); d->qiv = qobject_input_visitor_new(obj); qobject_unref(obj_orig); qobject_unref(obj); -- cgit v1.2.3