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-streamer.h"
17 #include "qapi/qmp/qjson.h"
18 #include "qapi/qmp/qbool.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qlist.h"
21 #include "qapi/qmp/qnum.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qemu/unicode.h"
25 typedef struct JSONParsingState
27 JSONMessageParser parser
;
32 static void consume_json(void *opaque
, QObject
*json
, Error
*err
)
34 JSONParsingState
*s
= opaque
;
36 assert(!json
!= !err
);
37 assert(!s
->result
|| !s
->err
);
40 qobject_unref(s
->result
);
42 error_setg(&s
->err
, "Expecting at most one JSON value");
54 * Parse @string as JSON value.
55 * If @ap is non-null, interpolate %-escapes.
56 * Takes ownership of %p arguments.
57 * On success, return the JSON value.
58 * On failure, store an error through @errp and return NULL.
59 * Ownership of %p arguments becomes indeterminate then. To avoid
60 * leaks, callers passing %p must terminate on error, e.g. by passing
63 static QObject
*qobject_from_jsonv(const char *string
, va_list *ap
,
66 JSONParsingState state
= {};
68 json_message_parser_init(&state
.parser
, consume_json
, &state
, ap
);
69 json_message_parser_feed(&state
.parser
, string
, strlen(string
));
70 json_message_parser_flush(&state
.parser
);
71 json_message_parser_destroy(&state
.parser
);
73 error_propagate(errp
, state
.err
);
77 QObject
*qobject_from_json(const char *string
, Error
**errp
)
79 return qobject_from_jsonv(string
, NULL
, errp
);
83 * Parse @string as JSON value with %-escapes interpolated.
84 * Abort on error. Do not use with untrusted @string.
85 * Return the resulting QObject. It is never null.
87 QObject
*qobject_from_vjsonf_nofail(const char *string
, va_list ap
)
92 /* va_copy() is needed when va_list is an array type */
94 obj
= qobject_from_jsonv(string
, &ap_copy
, &error_abort
);
102 * Parse @string as JSON value with %-escapes interpolated.
103 * Abort on error. Do not use with untrusted @string.
104 * Return the resulting QObject. It is never null.
106 QObject
*qobject_from_jsonf_nofail(const char *string
, ...)
111 va_start(ap
, string
);
112 obj
= qobject_from_vjsonf_nofail(string
, ap
);
119 * Parse @string as JSON object with %-escapes interpolated.
120 * Abort on error. Do not use with untrusted @string.
121 * Return the resulting QDict. It is never null.
123 QDict
*qdict_from_vjsonf_nofail(const char *string
, va_list ap
)
127 qdict
= qobject_to(QDict
, qobject_from_vjsonf_nofail(string
, ap
));
133 * Parse @string as JSON object with %-escapes interpolated.
134 * Abort on error. Do not use with untrusted @string.
135 * Return the resulting QDict. It is never null.
137 QDict
*qdict_from_jsonf_nofail(const char *string
, ...)
142 va_start(ap
, string
);
143 qdict
= qdict_from_vjsonf_nofail(string
, ap
);
148 typedef struct ToJsonIterState
156 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
);
158 static void to_json_dict_iter(const char *key
, QObject
*obj
, void *opaque
)
160 ToJsonIterState
*s
= opaque
;
165 qstring_append(s
->str
, s
->pretty
? "," : ", ");
169 qstring_append(s
->str
, "\n");
170 for (j
= 0 ; j
< s
->indent
; j
++)
171 qstring_append(s
->str
, " ");
174 qkey
= qstring_from_str(key
);
175 to_json(QOBJECT(qkey
), s
->str
, s
->pretty
, s
->indent
);
178 qstring_append(s
->str
, ": ");
179 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
183 static void to_json_list_iter(QObject
*obj
, void *opaque
)
185 ToJsonIterState
*s
= opaque
;
189 qstring_append(s
->str
, s
->pretty
? "," : ", ");
193 qstring_append(s
->str
, "\n");
194 for (j
= 0 ; j
< s
->indent
; j
++)
195 qstring_append(s
->str
, " ");
198 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
202 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
)
204 switch (qobject_type(obj
)) {
206 qstring_append(str
, "null");
209 QNum
*val
= qobject_to(QNum
, obj
);
210 char *buffer
= qnum_to_string(val
);
211 qstring_append(str
, buffer
);
215 case QTYPE_QSTRING
: {
216 QString
*val
= qobject_to(QString
, obj
);
222 ptr
= qstring_get_str(val
);
223 qstring_append(str
, "\"");
225 for (; *ptr
; ptr
= end
) {
226 cp
= mod_utf8_codepoint(ptr
, 6, &end
);
229 qstring_append(str
, "\\\"");
232 qstring_append(str
, "\\\\");
235 qstring_append(str
, "\\b");
238 qstring_append(str
, "\\f");
241 qstring_append(str
, "\\n");
244 qstring_append(str
, "\\r");
247 qstring_append(str
, "\\t");
251 cp
= 0xFFFD; /* replacement character */
254 /* beyond BMP; need a surrogate pair */
255 snprintf(buf
, sizeof(buf
), "\\u%04X\\u%04X",
256 0xD800 + ((cp
- 0x10000) >> 10),
257 0xDC00 + ((cp
- 0x10000) & 0x3FF));
258 } else if (cp
< 0x20 || cp
>= 0x7F) {
259 snprintf(buf
, sizeof(buf
), "\\u%04X", cp
);
264 qstring_append(str
, buf
);
268 qstring_append(str
, "\"");
273 QDict
*val
= qobject_to(QDict
, obj
);
277 s
.indent
= indent
+ 1;
279 qstring_append(str
, "{");
280 qdict_iter(val
, to_json_dict_iter
, &s
);
283 qstring_append(str
, "\n");
284 for (j
= 0 ; j
< indent
; j
++)
285 qstring_append(str
, " ");
287 qstring_append(str
, "}");
292 QList
*val
= qobject_to(QList
, obj
);
296 s
.indent
= indent
+ 1;
298 qstring_append(str
, "[");
299 qlist_iter(val
, (void *)to_json_list_iter
, &s
);
302 qstring_append(str
, "\n");
303 for (j
= 0 ; j
< indent
; j
++)
304 qstring_append(str
, " ");
306 qstring_append(str
, "]");
310 QBool
*val
= qobject_to(QBool
, obj
);
312 if (qbool_get_bool(val
)) {
313 qstring_append(str
, "true");
315 qstring_append(str
, "false");
324 QString
*qobject_to_json(const QObject
*obj
)
326 QString
*str
= qstring_new();
328 to_json(obj
, str
, 0, 0);
333 QString
*qobject_to_json_pretty(const QObject
*obj
)
335 QString
*str
= qstring_new();
337 to_json(obj
, str
, 1, 0);