]>
git.proxmox.com Git - mirror_qemu.git/blob - qjson.c
2 * QObject JSON integration
4 * Copyright IBM, Corp. 2009
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "json-lexer.h"
15 #include "json-parser.h"
16 #include "json-streamer.h"
24 typedef struct JSONParsingState
26 JSONMessageParser parser
;
31 static void parse_json(JSONMessageParser
*parser
, QList
*tokens
)
33 JSONParsingState
*s
= container_of(parser
, JSONParsingState
, parser
);
34 s
->result
= json_parser_parse(tokens
, s
->ap
);
37 QObject
*qobject_from_jsonv(const char *string
, va_list *ap
)
39 JSONParsingState state
= {};
43 json_message_parser_init(&state
.parser
, parse_json
);
44 json_message_parser_feed(&state
.parser
, string
, strlen(string
));
45 json_message_parser_flush(&state
.parser
);
46 json_message_parser_destroy(&state
.parser
);
51 QObject
*qobject_from_json(const char *string
)
53 return qobject_from_jsonv(string
, NULL
);
57 * IMPORTANT: This function aborts on error, thus it must not
58 * be used with untrusted arguments.
60 QObject
*qobject_from_jsonf(const char *string
, ...)
66 obj
= qobject_from_jsonv(string
, &ap
);
73 typedef struct ToJsonIterState
81 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
);
83 static void to_json_dict_iter(const char *key
, QObject
*obj
, void *opaque
)
85 ToJsonIterState
*s
= opaque
;
90 qstring_append(s
->str
, ", ");
93 qstring_append(s
->str
, "\n");
94 for (j
= 0 ; j
< s
->indent
; j
++)
95 qstring_append(s
->str
, " ");
98 qkey
= qstring_from_str(key
);
99 to_json(QOBJECT(qkey
), s
->str
, s
->pretty
, s
->indent
);
102 qstring_append(s
->str
, ": ");
103 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
107 static void to_json_list_iter(QObject
*obj
, void *opaque
)
109 ToJsonIterState
*s
= opaque
;
113 qstring_append(s
->str
, ", ");
116 qstring_append(s
->str
, "\n");
117 for (j
= 0 ; j
< s
->indent
; j
++)
118 qstring_append(s
->str
, " ");
121 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
125 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
)
127 switch (qobject_type(obj
)) {
129 QInt
*val
= qobject_to_qint(obj
);
132 snprintf(buffer
, sizeof(buffer
), "%" PRId64
, qint_get_int(val
));
133 qstring_append(str
, buffer
);
136 case QTYPE_QSTRING
: {
137 QString
*val
= qobject_to_qstring(obj
);
140 ptr
= qstring_get_str(val
);
141 qstring_append(str
, "\"");
143 if ((ptr
[0] & 0xE0) == 0xE0 &&
144 (ptr
[1] & 0x80) && (ptr
[2] & 0x80)) {
148 wchar
= (ptr
[0] & 0x0F) << 12;
149 wchar
|= (ptr
[1] & 0x3F) << 6;
150 wchar
|= (ptr
[2] & 0x3F);
153 snprintf(escape
, sizeof(escape
), "\\u%04X", wchar
);
154 qstring_append(str
, escape
);
155 } else if ((ptr
[0] & 0xE0) == 0xC0 && (ptr
[1] & 0x80)) {
159 wchar
= (ptr
[0] & 0x1F) << 6;
160 wchar
|= (ptr
[1] & 0x3F);
163 snprintf(escape
, sizeof(escape
), "\\u%04X", wchar
);
164 qstring_append(str
, escape
);
165 } else switch (ptr
[0]) {
167 qstring_append(str
, "\\\"");
170 qstring_append(str
, "\\\\");
173 qstring_append(str
, "\\b");
176 qstring_append(str
, "\\f");
179 qstring_append(str
, "\\n");
182 qstring_append(str
, "\\r");
185 qstring_append(str
, "\\t");
188 if (ptr
[0] <= 0x1F) {
190 snprintf(escape
, sizeof(escape
), "\\u%04X", ptr
[0]);
191 qstring_append(str
, escape
);
193 char buf
[2] = { ptr
[0], 0 };
194 qstring_append(str
, buf
);
201 qstring_append(str
, "\"");
206 QDict
*val
= qobject_to_qdict(obj
);
210 s
.indent
= indent
+ 1;
212 qstring_append(str
, "{");
213 qdict_iter(val
, to_json_dict_iter
, &s
);
216 qstring_append(str
, "\n");
217 for (j
= 0 ; j
< indent
; j
++)
218 qstring_append(str
, " ");
220 qstring_append(str
, "}");
225 QList
*val
= qobject_to_qlist(obj
);
229 s
.indent
= indent
+ 1;
231 qstring_append(str
, "[");
232 qlist_iter(val
, (void *)to_json_list_iter
, &s
);
235 qstring_append(str
, "\n");
236 for (j
= 0 ; j
< indent
; j
++)
237 qstring_append(str
, " ");
239 qstring_append(str
, "]");
243 QFloat
*val
= qobject_to_qfloat(obj
);
247 len
= snprintf(buffer
, sizeof(buffer
), "%f", qfloat_get_double(val
));
248 while (len
> 0 && buffer
[len
- 1] == '0') {
252 if (len
&& buffer
[len
- 1] == '.') {
258 qstring_append(str
, buffer
);
262 QBool
*val
= qobject_to_qbool(obj
);
264 if (qbool_get_int(val
)) {
265 qstring_append(str
, "true");
267 qstring_append(str
, "false");
272 /* XXX: should QError be emitted? */
278 QString
*qobject_to_json(const QObject
*obj
)
280 QString
*str
= qstring_new();
282 to_json(obj
, str
, 0, 0);
287 QString
*qobject_to_json_pretty(const QObject
*obj
)
289 QString
*str
= qstring_new();
291 to_json(obj
, str
, 1, 0);