Revision 212b6008 qjson.c

b/qjson.c
72 72

  
73 73
typedef struct ToJsonIterState
74 74
{
75
    int indent;
76
    int pretty;
75 77
    int count;
76 78
    QString *str;
77 79
} ToJsonIterState;
78 80

  
79
static void to_json(const QObject *obj, QString *str);
81
static void to_json(const QObject *obj, QString *str, int pretty, int indent);
80 82

  
81 83
static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
82 84
{
83 85
    ToJsonIterState *s = opaque;
84 86
    QString *qkey;
87
    int j;
85 88

  
86
    if (s->count) {
89
    if (s->count)
87 90
        qstring_append(s->str, ", ");
91

  
92
    if (s->pretty) {
93
        qstring_append(s->str, "\n");
94
        for (j = 0 ; j < s->indent ; j++)
95
            qstring_append(s->str, "    ");
88 96
    }
89 97

  
90 98
    qkey = qstring_from_str(key);
91
    to_json(QOBJECT(qkey), s->str);
99
    to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
92 100
    QDECREF(qkey);
93 101

  
94 102
    qstring_append(s->str, ": ");
95
    to_json(obj, s->str);
103
    to_json(obj, s->str, s->pretty, s->indent);
96 104
    s->count++;
97 105
}
98 106

  
99 107
static void to_json_list_iter(QObject *obj, void *opaque)
100 108
{
101 109
    ToJsonIterState *s = opaque;
110
    int j;
102 111

  
103
    if (s->count) {
112
    if (s->count)
104 113
        qstring_append(s->str, ", ");
114

  
115
    if (s->pretty) {
116
        qstring_append(s->str, "\n");
117
        for (j = 0 ; j < s->indent ; j++)
118
            qstring_append(s->str, "    ");
105 119
    }
106 120

  
107
    to_json(obj, s->str);
121
    to_json(obj, s->str, s->pretty, s->indent);
108 122
    s->count++;
109 123
}
110 124

  
111
static void to_json(const QObject *obj, QString *str)
125
static void to_json(const QObject *obj, QString *str, int pretty, int indent)
112 126
{
113 127
    switch (qobject_type(obj)) {
114 128
    case QTYPE_QINT: {
......
193 207

  
194 208
        s.count = 0;
195 209
        s.str = str;
210
        s.indent = indent + 1;
211
        s.pretty = pretty;
196 212
        qstring_append(str, "{");
197 213
        qdict_iter(val, to_json_dict_iter, &s);
214
        if (pretty) {
215
            int j;
216
            qstring_append(str, "\n");
217
            for (j = 0 ; j < indent ; j++)
218
                qstring_append(str, "    ");
219
        }
198 220
        qstring_append(str, "}");
199 221
        break;
200 222
    }
......
204 226

  
205 227
        s.count = 0;
206 228
        s.str = str;
229
        s.indent = indent + 1;
230
        s.pretty = pretty;
207 231
        qstring_append(str, "[");
208 232
        qlist_iter(val, (void *)to_json_list_iter, &s);
233
        if (pretty) {
234
            int j;
235
            qstring_append(str, "\n");
236
            for (j = 0 ; j < indent ; j++)
237
                qstring_append(str, "    ");
238
        }
209 239
        qstring_append(str, "]");
210 240
        break;
211 241
    }
......
249 279
{
250 280
    QString *str = qstring_new();
251 281

  
252
    to_json(obj, str);
282
    to_json(obj, str, 0, 0);
283

  
284
    return str;
285
}
286

  
287
QString *qobject_to_json_pretty(const QObject *obj)
288
{
289
    QString *str = qstring_new();
290

  
291
    to_json(obj, str, 1, 0);
253 292

  
254 293
    return str;
255 294
}

Also available in: Unified diff