[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 02/20] qobject: Factor out helper json_pretty_newline()
From: |
Markus Armbruster |
Subject: |
[PULL 02/20] qobject: Factor out helper json_pretty_newline() |
Date: |
Thu, 30 Apr 2020 07:30:46 +0200 |
Signed-off-by: Markus Armbruster <address@hidden>
Message-Id: <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
[Coding style in moved code tidied up]
---
qobject/qjson.c | 40 ++++++++++++++++------------------------
1 file changed, 16 insertions(+), 24 deletions(-)
diff --git a/qobject/qjson.c b/qobject/qjson.c
index db36101f3b..87422f600d 100644
--- a/qobject/qjson.c
+++ b/qobject/qjson.c
@@ -159,21 +159,28 @@ typedef struct 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)
+{
+ int i;
+
+ if (pretty) {
+ qstring_append(str, "\n");
+ for (i = 0; i < indent; i++) {
+ qstring_append(str, " ");
+ }
+ }
+}
+
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, " ");
- }
+ json_pretty_newline(s->str, s->pretty, s->indent);
qkey = qstring_from_str(key);
to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
@@ -187,17 +194,12 @@ static void to_json_dict_iter(const char *key, QObject
*obj, void *opaque)
static void to_json_list_iter(QObject *obj, void *opaque)
{
ToJsonIterState *s = opaque;
- 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, " ");
- }
+ json_pretty_newline(s->str, s->pretty, s->indent);
to_json(obj, s->str, s->pretty, s->indent);
s->count++;
@@ -282,12 +284,7 @@ static void to_json(const QObject *obj, QString *str, int
pretty, int indent)
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, " ");
- }
+ json_pretty_newline(str, pretty, indent);
qstring_append(str, "}");
break;
}
@@ -301,12 +298,7 @@ static void to_json(const QObject *obj, QString *str, int
pretty, int indent)
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, " ");
- }
+ json_pretty_newline(str, pretty, indent);
qstring_append(str, "]");
break;
}
--
2.21.1
- [PULL 00/20] QAPI patches for 2020-04-30, Markus Armbruster, 2020/04/30
- [PULL 01/20] qobject: Clean up QLIST_FOREACH_ENTRY(), Markus Armbruster, 2020/04/30
- [PULL 03/20] qobject: Eliminate qlist_iter(), use QLIST_FOREACH_ENTRY() instead, Markus Armbruster, 2020/04/30
- [PULL 02/20] qobject: Factor out helper json_pretty_newline(),
Markus Armbruster <=
- [PULL 08/20] qapi: Fix typo in visit_start_list()'s contract, Markus Armbruster, 2020/04/30
- [PULL 09/20] qapi: Document @errp usage more thoroughly in visitor.h, Markus Armbruster, 2020/04/30
- [PULL 12/20] qapi: Fix Visitor contract for start_alternate(), Markus Armbruster, 2020/04/30
- [PULL 11/20] qapi: Assert incomplete object occurs only in dealloc visitor, Markus Armbruster, 2020/04/30
- [PULL 10/20] qapi: Polish prose in visitor.h, Markus Armbruster, 2020/04/30
- [PULL 14/20] qapi: Assert non-input visitors see only valid narrow integers, Markus Armbruster, 2020/04/30
- [PULL 20/20] qapi: Generate simpler marshalling code when no arguments, Markus Armbruster, 2020/04/30
- [PULL 04/20] qobject: Eliminate qdict_iter(), use qdict_first(), qdict_next(), Markus Armbruster, 2020/04/30
- [PULL 05/20] qemu-option: Clean up after the previous commit, Markus Armbruster, 2020/04/30
- [PULL 06/20] qapi: Belatedly update visitor.h's big comment for QAPI modules, Markus Armbruster, 2020/04/30