diff options
author | Markus Armbruster | 2020-04-15 10:30:47 +0200 |
---|---|---|
committer | Markus Armbruster | 2020-04-30 06:51:15 +0200 |
commit | 7b1cd1c65abad70b26fbe9b11991bd88f0d956e1 (patch) | |
tree | 33210bd085fd63afb53ef3db5319f524a8e325f5 /qobject/qjson.c | |
parent | qobject: Eliminate qlist_iter(), use QLIST_FOREACH_ENTRY() instead (diff) | |
download | qemu-7b1cd1c65abad70b26fbe9b11991bd88f0d956e1.tar.gz qemu-7b1cd1c65abad70b26fbe9b11991bd88f0d956e1.tar.xz qemu-7b1cd1c65abad70b26fbe9b11991bd88f0d956e1.zip |
qobject: Eliminate qdict_iter(), use qdict_first(), qdict_next()
qdict_iter() has just three uses and no test coverage. Replace by
qdict_first(), qdict_next() for more concise code and less type
punning.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200415083048.14339-5-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'qobject/qjson.c')
-rw-r--r-- | qobject/qjson.c | 54 |
1 files changed, 20 insertions, 34 deletions
diff --git a/qobject/qjson.c b/qobject/qjson.c index f0eebc5fda..f1f2c69704 100644 --- a/qobject/qjson.c +++ b/qobject/qjson.c @@ -149,14 +149,6 @@ QDict *qdict_from_jsonf_nofail(const char *string, ...) return qdict; } -typedef struct ToJsonIterState -{ - int indent; - int pretty; - int count; - QString *str; -} ToJsonIterState; - static void to_json(const QObject *obj, QString *str, int pretty, int indent); static void json_pretty_newline(QString *str, bool pretty, int indent) @@ -171,26 +163,6 @@ static void json_pretty_newline(QString *str, bool pretty, int indent) } } -static void to_json_dict_iter(const char *key, QObject *obj, void *opaque) -{ - ToJsonIterState *s = opaque; - QString *qkey; - - if (s->count) { - qstring_append(s->str, s->pretty ? "," : ", "); - } - - json_pretty_newline(s->str, s->pretty, s->indent); - - qkey = qstring_from_str(key); - to_json(QOBJECT(qkey), s->str, s->pretty, s->indent); - qobject_unref(qkey); - - qstring_append(s->str, ": "); - to_json(obj, s->str, s->pretty, s->indent); - s->count++; -} - static void to_json(const QObject *obj, QString *str, int pretty, int indent) { switch (qobject_type(obj)) { @@ -261,15 +233,29 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) break; } case QTYPE_QDICT: { - ToJsonIterState s; QDict *val = qobject_to(QDict, obj); + const char *comma = pretty ? "," : ", "; + const char *sep = ""; + const QDictEntry *entry; + QString *qkey; - s.count = 0; - s.str = str; - s.indent = indent + 1; - s.pretty = pretty; qstring_append(str, "{"); - qdict_iter(val, to_json_dict_iter, &s); + + for (entry = qdict_first(val); + entry; + entry = qdict_next(val, entry)) { + qstring_append(str, sep); + json_pretty_newline(str, pretty, indent + 1); + + qkey = qstring_from_str(qdict_entry_key(entry)); + to_json(QOBJECT(qkey), str, pretty, indent + 1); + qobject_unref(qkey); + + qstring_append(str, ": "); + to_json(qdict_entry_value(entry), str, pretty, indent + 1); + sep = comma; + } + json_pretty_newline(str, pretty, indent); qstring_append(str, "}"); break; |