[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 01/23] Add support for JSON pretty printing
From: |
Luiz Capitulino |
Subject: |
[Qemu-devel] [PATCH 01/23] Add support for JSON pretty printing |
Date: |
Fri, 1 Oct 2010 15:19:05 -0300 |
From: Daniel P. Berrange <address@hidden>
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 <address@hidden>
Signed-off-by: Luiz Capitulino <address@hidden>
---
qjson.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++--------
qjson.h | 1 +
2 files changed, 48 insertions(+), 8 deletions(-)
diff --git a/qjson.c b/qjson.c
index e4ee433..f9c8e77 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");
+ 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: {
@@ -193,8 +207,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;
}
@@ -204,8 +226,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;
}
@@ -249,7 +279,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 */
--
1.7.3.1.50.g1e633
- [Qemu-devel] [PULL 00/23]: Monitor queue, Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 01/23] Add support for JSON pretty printing,
Luiz Capitulino <=
- [Qemu-devel] [PATCH 02/23] Add option to turn on JSON pretty printing in monitor, Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 03/23] disable guest-provided stats on "info balloon" command, Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 04/23] Monitor: Introduce search_dispatch_table(), Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 05/23] QMP: handle_qmp_command(): Move 'cmd' sanity check, Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 06/23] QMP: Don't use do_info(), Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 08/23] Monitor: Drop is_async_return(), Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 07/23] Monitor: Drop QMP bits from do_info(), Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 09/23] Monitor: Convert do_info() back to HMP, Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 10/23] Monitor: Introduce the qmp-commands.hx file, Luiz Capitulino, 2010/10/01
- [Qemu-devel] [PATCH 11/23] QMP: Introduce qmp_find_cmd(), Luiz Capitulino, 2010/10/01