aboutsummaryrefslogtreecommitdiffstats
path: root/qjson.c
diff options
context:
space:
mode:
authorDavid 'Digit' Turner <digit@android.com>2011-05-11 00:22:51 +0200
committerDavid 'Digit' Turner <digit@android.com>2011-06-01 17:08:19 +0200
commitf0753acaab4309557754ec19d3e839fe6b5e356c (patch)
treeaca66d0f9969d71edacc64c7304c53d96895ed81 /qjson.c
parent8354d2d35fe4463816dcc52a96fa354940fdd38f (diff)
downloadexternal_qemu-f0753acaab4309557754ec19d3e839fe6b5e356c.zip
external_qemu-f0753acaab4309557754ec19d3e839fe6b5e356c.tar.gz
external_qemu-f0753acaab4309557754ec19d3e839fe6b5e356c.tar.bz2
qjson.c: minor integrate
Change-Id: Id2c8a26a772d7fe76b8ec6a339a32111946d1350
Diffstat (limited to 'qjson.c')
-rw-r--r--qjson.c55
1 files changed, 47 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;
}