aboutsummaryrefslogtreecommitdiff
path: root/qobject/qjson.c
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2020-04-30 12:53:18 +0100
committerPeter Maydell <peter.maydell@linaro.org>2020-04-30 12:53:18 +0100
commit68bfd7db1e8b718187fd0ba4dde32396efcde668 (patch)
tree5ef9a47e0bc096200ae0828053ba0523933bb319 /qobject/qjson.c
parent157360331ab3a423e2481b62c60d5399bf2957bd (diff)
parent89bf68f933393a1bc0de4d07b59ffa8920da130f (diff)
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2020-04-30' into staging
QAPI patches for 2020-04-30 # gpg: Signature made Thu 30 Apr 2020 06:29:18 BST # gpg: using RSA key 354BC8B3D7EB2A6B68674E5F3870B400EB918653 # gpg: issuer "armbru@redhat.com" # gpg: Good signature from "Markus Armbruster <armbru@redhat.com>" [full] # gpg: aka "Markus Armbruster <armbru@pond.sub.org>" [full] # Primary key fingerprint: 354B C8B3 D7EB 2A6B 6867 4E5F 3870 B400 EB91 8653 * remotes/armbru/tags/pull-qapi-2020-04-30: qapi: Generate simpler marshalling code when no arguments qapi: Disallow qmp_marshal_FOO(NULL, ...) qom: Simplify object_property_get_enum() qapi: Only input visitors can actually fail qapi: Assert non-input visitors see only valid alternate tags qapi: Clean up visitor's recovery from input with invalid type qapi: Assert non-input visitors see only valid narrow integers qapi: Assert output visitors see only valid enum values qapi: Fix Visitor contract for start_alternate() qapi: Assert incomplete object occurs only in dealloc visitor qapi: Polish prose in visitor.h qapi: Document @errp usage more thoroughly in visitor.h qapi: Fix typo in visit_start_list()'s contract qapi: Fix the virtual walk example in visitor.h's big comment qapi: Belatedly update visitor.h's big comment for QAPI modules qemu-option: Clean up after the previous commit qobject: Eliminate qdict_iter(), use qdict_first(), qdict_next() qobject: Eliminate qlist_iter(), use QLIST_FOREACH_ENTRY() instead qobject: Factor out helper json_pretty_newline() qobject: Clean up QLIST_FOREACH_ENTRY() Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'qobject/qjson.c')
-rw-r--r--qobject/qjson.c107
1 files changed, 38 insertions, 69 deletions
diff --git a/qobject/qjson.c b/qobject/qjson.c
index db36101f3b..f1f2c69704 100644
--- a/qobject/qjson.c
+++ b/qobject/qjson.c
@@ -149,58 +149,18 @@ 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 to_json_dict_iter(const char *key, QObject *obj, void *opaque)
-{
- ToJsonIterState *s = opaque;
- QString *qkey;
- int j;
-
- if (s->count) {
- qstring_append(s->str, s->pretty ? "," : ", ");
- }
-
- if (s->pretty) {
- qstring_append(s->str, "\n");
- for (j = 0 ; j < s->indent ; j++)
- qstring_append(s->str, " ");
- }
-
- 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_list_iter(QObject *obj, void *opaque)
+static void json_pretty_newline(QString *str, bool pretty, int indent)
{
- ToJsonIterState *s = opaque;
- int j;
+ int i;
- if (s->count) {
- qstring_append(s->str, s->pretty ? "," : ", ");
- }
-
- if (s->pretty) {
- qstring_append(s->str, "\n");
- for (j = 0 ; j < s->indent ; j++)
- qstring_append(s->str, " ");
+ if (pretty) {
+ qstring_append(str, "\n");
+ for (i = 0; i < indent; i++) {
+ qstring_append(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)
@@ -273,40 +233,49 @@ 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);
- if (pretty) {
- int j;
- qstring_append(str, "\n");
- for (j = 0 ; j < indent ; j++)
- qstring_append(str, " ");
+
+ 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;
}
case QTYPE_QLIST: {
- ToJsonIterState s;
QList *val = qobject_to(QList, obj);
+ const char *comma = pretty ? "," : ", ";
+ const char *sep = "";
+ QListEntry *entry;
- s.count = 0;
- s.str = str;
- s.indent = indent + 1;
- s.pretty = pretty;
qstring_append(str, "[");
- qlist_iter(val, (void *)to_json_list_iter, &s);
- if (pretty) {
- int j;
- qstring_append(str, "\n");
- for (j = 0 ; j < indent ; j++)
- qstring_append(str, " ");
+
+ QLIST_FOREACH_ENTRY(val, entry) {
+ qstring_append(str, sep);
+ json_pretty_newline(str, pretty, indent + 1);
+ to_json(qlist_entry_obj(entry), str, pretty, indent + 1);
+ sep = comma;
}
+
+ json_pretty_newline(str, pretty, indent);
qstring_append(str, "]");
break;
}