Simplify construction of a single provider by using
virJSONValueObjectAdd and restructuring the code.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/qemu/qemu_monitor_json.c | 32 ++++++++++++++------------------
1 file changed, 14 insertions(+), 18 deletions(-)
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index db99017555..01e2aaa2cf 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -8828,36 +8828,32 @@ qemuMonitorJSONQueryStats(qemuMonitor *mon,
g_autoptr(virJSONValue) reply = NULL;
g_autoptr(virJSONValue) vcpu_list = NULL;
g_autoptr(virJSONValue) provider_list = NULL;
-
size_t i;
if (providers) {
provider_list = virJSONValueNewArray();
for (i = 0; i < providers->len; i++) {
- g_autoptr(virJSONValue) provider_obj = virJSONValueNewObject();
qemuMonitorQueryStatsProvider *provider = providers->pdata[i];
- const char *type_str =
qemuMonitorQueryStatsProviderTypeToString(provider->type);
- virBitmap *names = provider->names;
-
- if (virJSONValueObjectAppendString(provider_obj, "provider",
type_str) < 0)
- return NULL;
-
- if (!virBitmapIsAllClear(names)) {
- g_autoptr(virJSONValue) provider_names = virJSONValueNewArray();
- ssize_t curBit = -1;
+ g_autoptr(virJSONValue) provider_obj = NULL;
+ g_autoptr(virJSONValue) provider_names = NULL;
+ ssize_t curBit = -1;
- while ((curBit = virBitmapNextSetBit(names, curBit)) != -1) {
- const char *name = qemuMonitorQueryStatsNameTypeToString(curBit);
+ while ((curBit = virBitmapNextSetBit(provider->names, curBit)) != -1) {
+ if (!provider_names)
+ provider_names = virJSONValueNewArray();
- if (virJSONValueArrayAppendString(provider_names, name) < 0)
- return NULL;
- }
-
- if (virJSONValueObjectAppend(provider_obj, "names",
&provider_names) < 0)
+ if (virJSONValueArrayAppendString(provider_names,
+
qemuMonitorQueryStatsNameTypeToString(curBit)) < 0)
return NULL;
}
+ if (virJSONValueObjectAdd(&provider_obj,
+ "s:provider",
qemuMonitorQueryStatsProviderTypeToString(provider->type),
+ "A:names", &provider_names,
+ NULL) < 0)
+ return NULL;
+
if (virJSONValueArrayAppend(provider_list, &provider_obj) < 0)
return NULL;
}
--
2.39.1