Skip to content

Commit

Permalink
qobject: Change qobject_to_json()'s value to GString
Browse files Browse the repository at this point in the history
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 <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
Markus Armbruster committed Dec 19, 2020
1 parent f1cc129 commit eab3a46
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 93 deletions.
6 changes: 3 additions & 3 deletions block.c
Original file line number Diff line number Diff line change
Expand Up @@ -6995,13 +6995,13 @@ void bdrv_refresh_filename(BlockDriverState *bs)
if (bs->exact_filename[0]) {
pstrcpy(bs->filename, sizeof(bs->filename), bs->exact_filename);
} else {
QString *json = qobject_to_json(QOBJECT(bs->full_open_options));
GString *json = qobject_to_json(QOBJECT(bs->full_open_options));
if (snprintf(bs->filename, sizeof(bs->filename), "json:%s",
qstring_get_str(json)) >= sizeof(bs->filename)) {
json->str) >= sizeof(bs->filename)) {
/* Give user a hint if we truncated things. */
strcpy(bs->filename + sizeof(bs->filename) - 4, "...");
}
qobject_unref(json);
g_string_free(json, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion block/rbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ static void qemu_rbd_parse_filename(const char *filename, QDict *options,

if (keypairs) {
qdict_put(options, "=keyvalue-pairs",
qobject_to_json(QOBJECT(keypairs)));
qstring_from_gstring(qobject_to_json(QOBJECT(keypairs))));
}

done:
Expand Down
4 changes: 2 additions & 2 deletions include/qapi/qmp/qjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
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, bool pretty);
GString *qobject_to_json(const QObject *obj);
GString *qobject_to_json_pretty(const QObject *obj, bool pretty);

#endif /* QJSON_H */
14 changes: 7 additions & 7 deletions monitor/qmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ static void monitor_qmp_cleanup_queue_and_resume(MonitorQMP *mon)
void qmp_send_response(MonitorQMP *mon, const QDict *rsp)
{
const QObject *data = QOBJECT(rsp);
QString *json;
GString *json;

json = qobject_to_json_pretty(data, mon->pretty);
assert(json != NULL);

qstring_append_chr(json, '\n');
monitor_puts(&mon->common, qstring_get_str(json));
g_string_append_c(json, '\n');
monitor_puts(&mon->common, json->str);

qobject_unref(json);
g_string_free(json, true);
}

/*
Expand Down Expand Up @@ -320,9 +320,9 @@ static void handle_qmp_command(void *opaque, QObject *req, Error *err)
} /* else will fail qmp_dispatch() */

if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
QString *req_json = qobject_to_json(req);
trace_handle_qmp_command(mon, qstring_get_str(req_json));
qobject_unref(req_json);
GString *req_json = qobject_to_json(req);
trace_handle_qmp_command(mon, req_json->str);
g_string_free(req_json, true);
}

if (qdict && qmp_is_oob(qdict)) {
Expand Down
25 changes: 12 additions & 13 deletions qemu-img.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "qapi/qobject-output-visitor.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qstring.h"
#include "qemu/cutils.h"
#include "qemu/config-file.h"
#include "qemu/option.h"
Expand Down Expand Up @@ -627,18 +626,18 @@ static int img_create(int argc, char **argv)

static void dump_json_image_check(ImageCheck *check, bool quiet)
{
QString *str;
GString *str;
QObject *obj;
Visitor *v = qobject_output_visitor_new(&obj);

visit_type_ImageCheck(v, NULL, &check, &error_abort);
visit_complete(v, &obj);
str = qobject_to_json_pretty(obj, true);
assert(str != NULL);
qprintf(quiet, "%s\n", qstring_get_str(str));
qprintf(quiet, "%s\n", str->str);
qobject_unref(obj);
visit_free(v);
qobject_unref(str);
g_string_free(str, true);
}

static void dump_human_image_check(ImageCheck *check, bool quiet)
Expand Down Expand Up @@ -2789,34 +2788,34 @@ static void dump_snapshots(BlockDriverState *bs)

static void dump_json_image_info_list(ImageInfoList *list)
{
QString *str;
GString *str;
QObject *obj;
Visitor *v = qobject_output_visitor_new(&obj);

visit_type_ImageInfoList(v, NULL, &list, &error_abort);
visit_complete(v, &obj);
str = qobject_to_json_pretty(obj, true);
assert(str != NULL);
printf("%s\n", qstring_get_str(str));
printf("%s\n", str->str);
qobject_unref(obj);
visit_free(v);
qobject_unref(str);
g_string_free(str, true);
}

static void dump_json_image_info(ImageInfo *info)
{
QString *str;
GString *str;
QObject *obj;
Visitor *v = qobject_output_visitor_new(&obj);

visit_type_ImageInfo(v, NULL, &info, &error_abort);
visit_complete(v, &obj);
str = qobject_to_json_pretty(obj, true);
assert(str != NULL);
printf("%s\n", qstring_get_str(str));
printf("%s\n", str->str);
qobject_unref(obj);
visit_free(v);
qobject_unref(str);
g_string_free(str, true);
}

static void dump_human_image_info_list(ImageInfoList *list)
Expand Down Expand Up @@ -5236,18 +5235,18 @@ static int img_dd(int argc, char **argv)

static void dump_json_block_measure_info(BlockMeasureInfo *info)
{
QString *str;
GString *str;
QObject *obj;
Visitor *v = qobject_output_visitor_new(&obj);

visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
visit_complete(v, &obj);
str = qobject_to_json_pretty(obj, true);
assert(str != NULL);
printf("%s\n", qstring_get_str(str));
printf("%s\n", str->str);
qobject_unref(obj);
visit_free(v);
qobject_unref(str);
g_string_free(str, true);
}

static int img_measure(int argc, char **argv)
Expand Down
22 changes: 7 additions & 15 deletions qga/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "qapi/qmp/json-parser.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qstring.h"
#include "guest-agent-core.h"
#include "qga-qapi-init-commands.h"
#include "qapi/qmp/qerror.h"
Expand Down Expand Up @@ -528,8 +527,7 @@ static void become_daemon(const char *pidfile)

static int send_response(GAState *s, const QDict *rsp)
{
const char *buf;
QString *payload_qstr, *response_qstr;
GString *response;
GIOStatus status;

g_assert(s->channel);
Expand All @@ -538,25 +536,19 @@ static int send_response(GAState *s, const QDict *rsp)
return 0;
}

payload_qstr = qobject_to_json(QOBJECT(rsp));
if (!payload_qstr) {
response = qobject_to_json(QOBJECT(rsp));
if (!response) {
return -EINVAL;
}

if (s->delimit_response) {
s->delimit_response = false;
response_qstr = qstring_new();
qstring_append_chr(response_qstr, QGA_SENTINEL_BYTE);
qstring_append(response_qstr, qstring_get_str(payload_qstr));
qobject_unref(payload_qstr);
} else {
response_qstr = payload_qstr;
g_string_prepend_c(response, QGA_SENTINEL_BYTE);
}

qstring_append_chr(response_qstr, '\n');
buf = qstring_get_str(response_qstr);
status = ga_channel_write_all(s->channel, buf, strlen(buf));
qobject_unref(response_qstr);
g_string_append_c(response, '\n');
status = ga_channel_write_all(s->channel, response->str, response->len);
g_string_free(response, true);
if (status != G_IO_STATUS_NORMAL) {
return -EIO;
}
Expand Down
6 changes: 3 additions & 3 deletions qobject/qjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ static void to_json(const QObject *obj, GString *accu, bool pretty, int indent)
}
}

QString *qobject_to_json_pretty(const QObject *obj, bool pretty)
GString *qobject_to_json_pretty(const QObject *obj, bool pretty)
{
GString *accu = g_string_new(NULL);

to_json(obj, accu, pretty, 0);
return qstring_from_gstring(accu);
return accu;
}

QString *qobject_to_json(const QObject *obj)
GString *qobject_to_json(const QObject *obj)
{
return qobject_to_json_pretty(obj, false);
}
4 changes: 2 additions & 2 deletions qom/object_interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qerror.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qstring.h"
#include "qapi/qobject-input-visitor.h"
#include "qom/object_interfaces.h"
#include "qemu/help_option.h"
Expand Down Expand Up @@ -207,7 +206,8 @@ char *object_property_help(const char *name, const char *type,
g_string_append(str, description);
}
if (defval) {
g_autofree char *def_json = qstring_free(qobject_to_json(defval), TRUE);
g_autofree char *def_json = g_string_free(qobject_to_json(defval),
true);
g_string_append_printf(str, " (default: %s)", def_json);
}

Expand Down
7 changes: 3 additions & 4 deletions qom/qom-hmp-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "qapi/qapi-commands-qom.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qstring.h"
#include "qom/object.h"

void hmp_qom_list(Monitor *mon, const QDict *qdict)
Expand Down Expand Up @@ -78,9 +77,9 @@ void hmp_qom_get(Monitor *mon, const QDict *qdict)
QObject *obj = qmp_qom_get(path, property, &err);

if (err == NULL) {
QString *str = qobject_to_json_pretty(obj, true);
monitor_printf(mon, "%s\n", qstring_get_str(str));
qobject_unref(str);
GString *str = qobject_to_json_pretty(obj, true);
monitor_printf(mon, "%s\n", str->str);
g_string_free(str, true);
}

qobject_unref(obj);
Expand Down
Loading

0 comments on commit eab3a46

Please sign in to comment.