From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=51182 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OMRJo-0004ZD-BY for qemu-devel@nongnu.org; Wed, 09 Jun 2010 15:52:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OMRJm-0000AO-UK for qemu-devel@nongnu.org; Wed, 09 Jun 2010 15:52:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58406) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OMRJm-0000A3-L6 for qemu-devel@nongnu.org; Wed, 09 Jun 2010 15:52:06 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o59Jq41h031455 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 9 Jun 2010 15:52:04 -0400 Date: Wed, 9 Jun 2010 16:51:59 -0300 From: Luiz Capitulino Subject: Re: [Qemu-devel] [PATCH 01/19] Add support for JSON pretty printing Message-ID: <20100609165159.3b6ca7fc@redhat.com> In-Reply-To: <1275921752-29420-2-git-send-email-berrange@redhat.com> References: <1275921752-29420-1-git-send-email-berrange@redhat.com> <1275921752-29420-2-git-send-email-berrange@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Daniel P. Berrange" Cc: qemu-devel@nongnu.org On Mon, 7 Jun 2010 15:42:14 +0100 "Daniel P. Berrange" wrote: > The monitor does not pretty-print JSON output, so that everything > will be on a single line reply. When JSON docs get large this is > quite unpleasant to read. For the future command line capabilities > query ability, huge JSON docs will be available. This needs the > ability to pretty-print. > > This introduces a new API qobject_to_json_pretty() that does > a minimal indentation of list and dict members. As an example, > this makes > > {"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}} > > Output as > > { > "QMP": { > "version": { > "micro": 50, > "minor": 12, > "package": "", > "major": 0 > }, > "capabilities": [ > ] > } > } > > NB: this is not turned on for the QMP monitor. > > Signed-off-by: Daniel P. Berrange > --- > qjson.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++-------- > qjson.h | 1 + > 2 files changed, 48 insertions(+), 8 deletions(-) > > diff --git a/qjson.c b/qjson.c > index 483c667..f402103 100644 > --- a/qjson.c > +++ b/qjson.c > @@ -72,43 +72,57 @@ QObject *qobject_from_jsonf(const char *string, ...) > > typedef struct ToJsonIterState > { > + int indent; > + int pretty; > int count; > QString *str; > } ToJsonIterState; > > -static void to_json(const QObject *obj, QString *str); > +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) { > + if (s->count) > qstring_append(s->str, ", "); > + > + 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); > + to_json(QOBJECT(qkey), s->str, s->pretty, s->indent); > QDECREF(qkey); > > qstring_append(s->str, ": "); > - to_json(obj, 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; > + int j; > > - if (s->count) { > + if (s->count) > qstring_append(s->str, ", "); > + > + if (s->pretty) { > + qstring_append(s->str, "\n"); Nitpick: we also have qstring_append_chr() > + for (j = 0 ; j < s->indent ; j++) > + qstring_append(s->str, " "); > } > > - to_json(obj, s->str); > + to_json(obj, s->str, s->pretty, s->indent); > s->count++; > } > > -static void to_json(const QObject *obj, QString *str) > +static void to_json(const QObject *obj, QString *str, int pretty, int indent) > { > switch (qobject_type(obj)) { > case QTYPE_QINT: { > @@ -190,8 +204,16 @@ static void to_json(const QObject *obj, QString *str) > > 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, " "); > + } > qstring_append(str, "}"); > break; > } > @@ -201,8 +223,16 @@ static void to_json(const QObject *obj, QString *str) > > 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, " "); > + } > qstring_append(str, "]"); > break; > } > @@ -246,7 +276,16 @@ QString *qobject_to_json(const QObject *obj) > { > QString *str = qstring_new(); > > - to_json(obj, str); > + to_json(obj, str, 0, 0); > + > + return str; > +} > + > +QString *qobject_to_json_pretty(const QObject *obj) > +{ > + QString *str = qstring_new(); > + > + to_json(obj, str, 1, 0); > > return str; > } > diff --git a/qjson.h b/qjson.h > index 7afec2e..cd60e0b 100644 > --- a/qjson.h > +++ b/qjson.h > @@ -24,5 +24,6 @@ QObject *qobject_from_jsonf(const char *string, ...) > QObject *qobject_from_jsonv(const char *string, va_list *ap); > > QString *qobject_to_json(const QObject *obj); > +QString *qobject_to_json_pretty(const QObject *obj); > > #endif /* QJSON_H */