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/types.h"
21 #include "qemu/unicode.h"
23 typedef struct JSONParsingState
25 JSONMessageParser parser
;
31 static void parse_json(JSONMessageParser
*parser
, GQueue
*tokens
)
33 JSONParsingState
*s
= container_of(parser
, JSONParsingState
, parser
);
35 s
->result
= json_parser_parse_err(tokens
, s
->ap
, &s
->err
);
38 QObject
*qobject_from_jsonv(const char *string
, va_list *ap
, Error
**errp
)
40 JSONParsingState state
= {};
44 json_message_parser_init(&state
.parser
, parse_json
);
45 json_message_parser_feed(&state
.parser
, string
, strlen(string
));
46 json_message_parser_flush(&state
.parser
);
47 json_message_parser_destroy(&state
.parser
);
49 error_propagate(errp
, state
.err
);
53 QObject
*qobject_from_json(const char *string
, Error
**errp
)
55 return qobject_from_jsonv(string
, NULL
, errp
);
59 * IMPORTANT: This function aborts on error, thus it must not
60 * be used with untrusted arguments.
62 QObject
*qobject_from_jsonf(const char *string
, ...)
68 obj
= qobject_from_jsonv(string
, &ap
, &error_abort
);
75 typedef struct ToJsonIterState
83 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
);
85 static void to_json_dict_iter(const char *key
, QObject
*obj
, void *opaque
)
87 ToJsonIterState
*s
= opaque
;
92 qstring_append(s
->str
, s
->pretty
? "," : ", ");
96 qstring_append(s
->str
, "\n");
97 for (j
= 0 ; j
< s
->indent
; j
++)
98 qstring_append(s
->str
, " ");
101 qkey
= qstring_from_str(key
);
102 to_json(QOBJECT(qkey
), s
->str
, s
->pretty
, s
->indent
);
105 qstring_append(s
->str
, ": ");
106 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
110 static void to_json_list_iter(QObject
*obj
, void *opaque
)
112 ToJsonIterState
*s
= opaque
;
116 qstring_append(s
->str
, s
->pretty
? "," : ", ");
120 qstring_append(s
->str
, "\n");
121 for (j
= 0 ; j
< s
->indent
; j
++)
122 qstring_append(s
->str
, " ");
125 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
129 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
)
131 switch (qobject_type(obj
)) {
133 qstring_append(str
, "null");
136 QInt
*val
= qobject_to_qint(obj
);
139 snprintf(buffer
, sizeof(buffer
), "%" PRId64
, qint_get_int(val
));
140 qstring_append(str
, buffer
);
143 case QTYPE_QSTRING
: {
144 QString
*val
= qobject_to_qstring(obj
);
150 ptr
= qstring_get_str(val
);
151 qstring_append(str
, "\"");
153 for (; *ptr
; ptr
= end
) {
154 cp
= mod_utf8_codepoint(ptr
, 6, &end
);
157 qstring_append(str
, "\\\"");
160 qstring_append(str
, "\\\\");
163 qstring_append(str
, "\\b");
166 qstring_append(str
, "\\f");
169 qstring_append(str
, "\\n");
172 qstring_append(str
, "\\r");
175 qstring_append(str
, "\\t");
179 cp
= 0xFFFD; /* replacement character */
182 /* beyond BMP; need a surrogate pair */
183 snprintf(buf
, sizeof(buf
), "\\u%04X\\u%04X",
184 0xD800 + ((cp
- 0x10000) >> 10),
185 0xDC00 + ((cp
- 0x10000) & 0x3FF));
186 } else if (cp
< 0x20 || cp
>= 0x7F) {
187 snprintf(buf
, sizeof(buf
), "\\u%04X", cp
);
192 qstring_append(str
, buf
);
196 qstring_append(str
, "\"");
201 QDict
*val
= qobject_to_qdict(obj
);
205 s
.indent
= indent
+ 1;
207 qstring_append(str
, "{");
208 qdict_iter(val
, to_json_dict_iter
, &s
);
211 qstring_append(str
, "\n");
212 for (j
= 0 ; j
< indent
; j
++)
213 qstring_append(str
, " ");
215 qstring_append(str
, "}");
220 QList
*val
= qobject_to_qlist(obj
);
224 s
.indent
= indent
+ 1;
226 qstring_append(str
, "[");
227 qlist_iter(val
, (void *)to_json_list_iter
, &s
);
230 qstring_append(str
, "\n");
231 for (j
= 0 ; j
< indent
; j
++)
232 qstring_append(str
, " ");
234 qstring_append(str
, "]");
238 QFloat
*val
= qobject_to_qfloat(obj
);
242 /* FIXME: snprintf() is locale dependent; but JSON requires
243 * numbers to be formatted as if in the C locale. Dependence
244 * on C locale is a pervasive issue in QEMU. */
245 /* FIXME: This risks printing Inf or NaN, which are not valid
247 /* FIXME: the default precision of 6 for %f often causes
248 * rounding errors; we should be using DBL_DECIMAL_DIG (17),
249 * and only rounding to a shorter number if the result would
250 * still produce the same floating point value. */
251 len
= snprintf(buffer
, sizeof(buffer
), "%f", qfloat_get_double(val
));
252 while (len
> 0 && buffer
[len
- 1] == '0') {
256 if (len
&& buffer
[len
- 1] == '.') {
262 qstring_append(str
, buffer
);
266 QBool
*val
= qobject_to_qbool(obj
);
268 if (qbool_get_bool(val
)) {
269 qstring_append(str
, "true");
271 qstring_append(str
, "false");
280 QString
*qobject_to_json(const QObject
*obj
)
282 QString
*str
= qstring_new();
284 to_json(obj
, str
, 0, 0);
289 QString
*qobject_to_json_pretty(const QObject
*obj
)
291 QString
*str
= qstring_new();
293 to_json(obj
, str
, 1, 0);