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 "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/json-lexer.h"
17 #include "qapi/qmp/json-parser.h"
18 #include "qapi/qmp/json-streamer.h"
19 #include "qapi/qmp/qjson.h"
20 #include "qapi/qmp/qbool.h"
21 #include "qapi/qmp/qdict.h"
22 #include "qapi/qmp/qlist.h"
23 #include "qapi/qmp/qnum.h"
24 #include "qapi/qmp/qstring.h"
25 #include "qemu/unicode.h"
27 typedef struct JSONParsingState
29 JSONMessageParser parser
;
35 static void parse_json(JSONMessageParser
*parser
, GQueue
*tokens
)
37 JSONParsingState
*s
= container_of(parser
, JSONParsingState
, parser
);
39 s
->result
= json_parser_parse_err(tokens
, s
->ap
, &s
->err
);
43 * Parse @string as JSON value.
44 * If @ap is non-null, interpolate %-escapes.
45 * Takes ownership of %p arguments.
46 * On success, return the JSON value.
47 * On failure, store an error through @errp and return NULL.
48 * Ownership of %p arguments becomes indeterminate then. To avoid
49 * leaks, callers passing %p must terminate on error, e.g. by passing
52 static QObject
*qobject_from_jsonv(const char *string
, va_list *ap
,
55 JSONParsingState state
= {};
59 json_message_parser_init(&state
.parser
, parse_json
);
60 json_message_parser_feed(&state
.parser
, string
, strlen(string
));
61 json_message_parser_flush(&state
.parser
);
62 json_message_parser_destroy(&state
.parser
);
64 error_propagate(errp
, state
.err
);
68 QObject
*qobject_from_json(const char *string
, Error
**errp
)
70 return qobject_from_jsonv(string
, NULL
, errp
);
74 * Parse @string as JSON value with %-escapes interpolated.
75 * Abort on error. Do not use with untrusted @string.
76 * Return the resulting QObject. It is never null.
78 QObject
*qobject_from_vjsonf_nofail(const char *string
, va_list ap
)
83 /* va_copy() is needed when va_list is an array type */
85 obj
= qobject_from_jsonv(string
, &ap_copy
, &error_abort
);
93 * Parse @string as JSON value with %-escapes interpolated.
94 * Abort on error. Do not use with untrusted @string.
95 * Return the resulting QObject. It is never null.
97 QObject
*qobject_from_jsonf_nofail(const char *string
, ...)
102 va_start(ap
, string
);
103 obj
= qobject_from_vjsonf_nofail(string
, ap
);
110 * Parse @string as JSON object with %-escapes interpolated.
111 * Abort on error. Do not use with untrusted @string.
112 * Return the resulting QDict. It is never null.
114 QDict
*qdict_from_vjsonf_nofail(const char *string
, va_list ap
)
118 qdict
= qobject_to(QDict
, qobject_from_vjsonf_nofail(string
, ap
));
124 * Parse @string as JSON object with %-escapes interpolated.
125 * Abort on error. Do not use with untrusted @string.
126 * Return the resulting QDict. It is never null.
128 QDict
*qdict_from_jsonf_nofail(const char *string
, ...)
133 va_start(ap
, string
);
134 qdict
= qdict_from_vjsonf_nofail(string
, ap
);
139 typedef struct ToJsonIterState
147 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
);
149 static void to_json_dict_iter(const char *key
, QObject
*obj
, void *opaque
)
151 ToJsonIterState
*s
= opaque
;
156 qstring_append(s
->str
, s
->pretty
? "," : ", ");
160 qstring_append(s
->str
, "\n");
161 for (j
= 0 ; j
< s
->indent
; j
++)
162 qstring_append(s
->str
, " ");
165 qkey
= qstring_from_str(key
);
166 to_json(QOBJECT(qkey
), s
->str
, s
->pretty
, s
->indent
);
169 qstring_append(s
->str
, ": ");
170 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
174 static void to_json_list_iter(QObject
*obj
, void *opaque
)
176 ToJsonIterState
*s
= opaque
;
180 qstring_append(s
->str
, s
->pretty
? "," : ", ");
184 qstring_append(s
->str
, "\n");
185 for (j
= 0 ; j
< s
->indent
; j
++)
186 qstring_append(s
->str
, " ");
189 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
193 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
)
195 switch (qobject_type(obj
)) {
197 qstring_append(str
, "null");
200 QNum
*val
= qobject_to(QNum
, obj
);
201 char *buffer
= qnum_to_string(val
);
202 qstring_append(str
, buffer
);
206 case QTYPE_QSTRING
: {
207 QString
*val
= qobject_to(QString
, obj
);
213 ptr
= qstring_get_str(val
);
214 qstring_append(str
, "\"");
216 for (; *ptr
; ptr
= end
) {
217 cp
= mod_utf8_codepoint(ptr
, 6, &end
);
220 qstring_append(str
, "\\\"");
223 qstring_append(str
, "\\\\");
226 qstring_append(str
, "\\b");
229 qstring_append(str
, "\\f");
232 qstring_append(str
, "\\n");
235 qstring_append(str
, "\\r");
238 qstring_append(str
, "\\t");
242 cp
= 0xFFFD; /* replacement character */
245 /* beyond BMP; need a surrogate pair */
246 snprintf(buf
, sizeof(buf
), "\\u%04X\\u%04X",
247 0xD800 + ((cp
- 0x10000) >> 10),
248 0xDC00 + ((cp
- 0x10000) & 0x3FF));
249 } else if (cp
< 0x20 || cp
>= 0x7F) {
250 snprintf(buf
, sizeof(buf
), "\\u%04X", cp
);
255 qstring_append(str
, buf
);
259 qstring_append(str
, "\"");
264 QDict
*val
= qobject_to(QDict
, obj
);
268 s
.indent
= indent
+ 1;
270 qstring_append(str
, "{");
271 qdict_iter(val
, to_json_dict_iter
, &s
);
274 qstring_append(str
, "\n");
275 for (j
= 0 ; j
< indent
; j
++)
276 qstring_append(str
, " ");
278 qstring_append(str
, "}");
283 QList
*val
= qobject_to(QList
, obj
);
287 s
.indent
= indent
+ 1;
289 qstring_append(str
, "[");
290 qlist_iter(val
, (void *)to_json_list_iter
, &s
);
293 qstring_append(str
, "\n");
294 for (j
= 0 ; j
< indent
; j
++)
295 qstring_append(str
, " ");
297 qstring_append(str
, "]");
301 QBool
*val
= qobject_to(QBool
, obj
);
303 if (qbool_get_bool(val
)) {
304 qstring_append(str
, "true");
306 qstring_append(str
, "false");
315 QString
*qobject_to_json(const QObject
*obj
)
317 QString
*str
= qstring_new();
319 to_json(obj
, str
, 0, 0);
324 QString
*qobject_to_json_pretty(const QObject
*obj
)
326 QString
*str
= qstring_new();
328 to_json(obj
, str
, 1, 0);