All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: armbru@redhat.com
Subject: [Qemu-devel] [PATCH v6 13/15] qobject: Implement qobject_to_json() atop JSON visitor
Date: Mon, 10 Oct 2016 08:23:55 -0500	[thread overview]
Message-ID: <1476105837-9861-14-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1476105837-9861-1-git-send-email-eblake@redhat.com>

Rather than open-code two different JSON visitors, it's nicer to
make qobject_to_json() reuse the JSON output visitor.  The JSON
output visitor allowed us to separate formatting from walking
the tree, where the tree walk is normally done by the
QAPI-generated code for a "real" visit.  Now we can add another
tree walk of QObject, while reusing the formatting of the JSON
visitor, in a "virtual" visit.

Note that this virtual QObject tree walk can also be paired with
other visitors. The string output visitor isn't useful (it would
fail if structs are involved), but the (misnamed) QMP output
visitor could be used to perform a QObject deep clone.  However,
I did not find any obvious QObject clones in the current code
base that needed to use this trick.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v6: rebase, improve commit message, simplify iterators
[no v5 due to series split]
v4: new patch, inspired by discussion on v3 12/18
---
 include/qapi/qmp/qjson.h |   9 ++++
 qobject/qjson.c          | 113 +++++++++++++----------------------------------
 2 files changed, 40 insertions(+), 82 deletions(-)

diff --git a/include/qapi/qmp/qjson.h b/include/qapi/qmp/qjson.h
index 4b0e2b4..f8f4fcd 100644
--- a/include/qapi/qmp/qjson.h
+++ b/include/qapi/qmp/qjson.h
@@ -24,6 +24,15 @@ QObject *qobject_from_jsonv(const char *string, va_list *ap) GCC_FMT_ATTR(1, 0);
 /* Convert the object to QString; does not fail. */
 QString *qobject_to_json(const QObject *obj, bool pretty);

+/*
+ * Visit the object with an output visitor @v.
+ *
+ * @name represents the relationship of this object to any parent
+ * (ignored for a top-level visit, must be set if part of a
+ * dictionary, should be NULL if part of a list).
+ */
+void qobject_visit_output(Visitor *v, const char *name, const QObject *obj);
+
 int qstring_append_json_string(QString *qstring, const char *str);
 void qstring_append_json_number(QString *qstring, double number);

diff --git a/qobject/qjson.c b/qobject/qjson.c
index d45048b..4ce4b9a 100644
--- a/qobject/qjson.c
+++ b/qobject/qjson.c
@@ -19,6 +19,9 @@
 #include "qapi/qmp/qjson.h"
 #include "qapi/qmp/types.h"
 #include "qemu/unicode.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
+#include "qapi/json-output-visitor.h"

 typedef struct JSONParsingState
 {
@@ -69,115 +72,59 @@ QObject *qobject_from_jsonf(const char *string, ...)
     return obj;
 }

-typedef struct ToJsonIterState
-{
-    int indent;
-    bool pretty;
-    int count;
-    QString *str;
-} ToJsonIterState;
-
-static void to_json(const QObject *obj, QString *str, bool pretty, int indent);
-
-static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
-{
-    ToJsonIterState *s = opaque;
-
-    if (s->count) {
-        qstring_append(s->str, s->pretty ? "," : ", ");
-    }
-
-    if (s->pretty) {
-        qstring_append_printf(s->str, "\n%*s", 4 * s->indent, "");
-    }
-
-    qstring_append_json_string(s->str, key);
-
-    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)
-{
-    ToJsonIterState *s = opaque;
-
-    if (s->count) {
-        qstring_append(s->str, s->pretty ? "," : ", ");
-    }
-
-    if (s->pretty) {
-        qstring_append_printf(s->str, "\n%*s", 4 * s->indent, "");
-    }
-
-    to_json(obj, s->str, s->pretty, s->indent);
-    s->count++;
-}
-
-static void to_json(const QObject *obj, QString *str, bool pretty, int indent)
+void qobject_visit_output(Visitor *v, const char *name, const QObject *obj)
 {
     switch (qobject_type(obj)) {
     case QTYPE_QNULL:
-        qstring_append(str, "null");
+        visit_type_null(v, name, &error_abort);
         break;
     case QTYPE_QINT: {
-        QInt *val = qobject_to_qint(obj);
-        qstring_append_printf(str, "%" PRId64, qint_get_int(val));
+        int64_t val = qint_get_int(qobject_to_qint(obj));
+
+        visit_type_int64(v, name, &val, &error_abort);
         break;
     }
     case QTYPE_QSTRING: {
-        QString *val = qobject_to_qstring(obj);
+        const char *str = qstring_get_str(qobject_to_qstring(obj));
+
         /* FIXME: no way inform user if we replaced invalid UTF-8
          * sequences to avoid invalid JSON */
-        qstring_append_json_string(str, qstring_get_str(val));
+        visit_type_str(v, name, (char **)&str, &error_abort);
         break;
     }
     case QTYPE_QDICT: {
-        ToJsonIterState s;
         QDict *val = qobject_to_qdict(obj);
+        const QDictEntry *ent;

-        s.count = 0;
-        s.str = str;
-        s.indent = indent + 1;
-        s.pretty = pretty;
-        qstring_append_chr(str, '{');
-        qdict_iter(val, to_json_dict_iter, &s);
-        if (pretty) {
-            qstring_append_printf(str, "\n%*s", 4 * indent, "");
+        visit_start_struct(v, name, NULL, 0, &error_abort);
+        for (ent = qdict_first(val); ent; ent = qdict_next(val, ent)) {
+            qobject_visit_output(v, ent->key, ent->value);
         }
-        qstring_append_chr(str, '}');
+        visit_check_struct(v, &error_abort);
+        visit_end_struct(v, NULL);
         break;
     }
     case QTYPE_QLIST: {
-        ToJsonIterState s;
         QList *val = qobject_to_qlist(obj);
+        const QListEntry *ent;

-        s.count = 0;
-        s.str = str;
-        s.indent = indent + 1;
-        s.pretty = pretty;
-        qstring_append_chr(str, '[');
-        qlist_iter(val, (void *)to_json_list_iter, &s);
-        if (pretty) {
-            qstring_append_printf(str, "\n%*s", 4 * indent, "");
+        visit_start_list(v, name, NULL, 0, &error_abort);
+        QTAILQ_FOREACH(ent, &val->head, next) {
+            qobject_visit_output(v, NULL, ent->value);
         }
-        qstring_append_chr(str, ']');
+        visit_end_list(v, NULL);
         break;
     }
     case QTYPE_QFLOAT: {
         double val = qfloat_get_double(qobject_to_qfloat(obj));

-        qstring_append_json_number(str, val);
+        visit_type_number(v, name, &val, &error_abort);
         break;
     }
     case QTYPE_QBOOL: {
-        QBool *val = qobject_to_qbool(obj);
+        bool val = qbool_get_bool(qobject_to_qbool(obj));

-        if (qbool_get_bool(val)) {
-            qstring_append(str, "true");
-        } else {
-            qstring_append(str, "false");
-        }
+        visit_type_bool(v, name, &val, &error_abort);
         break;
     }
     default:
@@ -187,11 +134,13 @@ static void to_json(const QObject *obj, QString *str, bool pretty, int indent)

 QString *qobject_to_json(const QObject *obj, bool pretty)
 {
-    QString *str = qstring_new();
+    char *str;
+    Visitor *v = json_output_visitor_new(pretty, &str);

-    to_json(obj, str, pretty, 0);
-
-    return str;
+    qobject_visit_output(v, NULL, obj);
+    visit_complete(v, &str);
+    visit_free(v);
+    return qstring_wrap_str(str);
 }

 /**
-- 
2.7.4

  parent reply	other threads:[~2016-10-10 13:24 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-10 13:23 [Qemu-devel] [PATCH v6 00/15] Add qapi-to-JSON visitor Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 01/15] qapi: Visitor documentation tweak Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 02/15] qapi: Assert finite use of 'number' Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 03/15] qapi: Factor out JSON string escaping Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 04/15] qapi: Factor out JSON number formatting Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 05/15] qapi: Add qstring_append_printf() Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 06/15] qapi: Use qstring_append_chr() where appropriate Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 07/15] qstring: Add qstring_consume_str() Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 08/15] qstring: Add qstring_wrap_str() Eric Blake
2016-10-11 11:08   ` Marc-André Lureau
2016-10-11 15:04     ` Eric Blake
2016-10-11 15:05       ` Eric Blake
2016-10-11 15:25         ` Marc-André Lureau
2016-10-11 15:24   ` [Qemu-devel] [PATCH v6 08.5/15] fixup! " Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 09/15] qobject: Consolidate qobject_to_json() calls Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 10/15] tests: Test qobject_to_json() pretty formatting Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 11/15] qapi: Add JSON output visitor Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 12/15] qapi: Support pretty printing in " Eric Blake
2016-10-11 11:20   ` Marc-André Lureau
2016-10-11 15:09     ` Eric Blake
2016-10-10 13:23 ` Eric Blake [this message]
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 14/15] qapi: Add 'any' support to JSON output Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 15/15] qemu-img: Use new JSON output formatter Eric Blake
2016-10-11 12:14 ` [Qemu-devel] [PATCH v6 00/15] Add qapi-to-JSON visitor Marc-André Lureau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1476105837-9861-14-git-send-email-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.