2 * QObject JSON integration
4 * Copyright IBM, Corp. 2009
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/qmp/json-lexer.h"
16 #include "qapi/qmp/json-parser.h"
17 #include "qapi/qmp/json-streamer.h"
18 #include "qapi/qmp/qjson.h"
19 #include "qapi/qmp/qint.h"
20 #include "qapi/qmp/qlist.h"
21 #include "qapi/qmp/qbool.h"
22 #include "qapi/qmp/qfloat.h"
23 #include "qapi/qmp/qdict.h"
25 typedef struct JSONParsingState
27 JSONMessageParser parser;
32 static void parse_json(JSONMessageParser *parser, GQueue *tokens)
34 JSONParsingState *s = container_of(parser, JSONParsingState, parser);
35 s->result = json_parser_parse(tokens, s->ap);
38 QObject *qobject_from_jsonv(const char *string, va_list *ap)
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);
52 QObject *qobject_from_json(const char *string)
54 return qobject_from_jsonv(string, NULL);
58 * IMPORTANT: This function aborts on error, thus it must not
59 * be used with untrusted arguments.
61 QObject *qobject_from_jsonf(const char *string, ...)
67 obj = qobject_from_jsonv(string, &ap);
74 typedef struct ToJsonIterState
82 static void to_json(const QObject *obj, QString *str, int pretty, int indent);
84 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
86 ToJsonIterState *s = opaque;
91 qstring_append(s->str, s->pretty ? "," : ", ");
95 qstring_append(s->str, "\n");
96 for (j = 0 ; j < s->indent ; j++)
97 qstring_append(s->str, " ");
100 qkey = qstring_from_str(key);
101 to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
104 qstring_append(s->str, ": ");
105 to_json(obj, s->str, s->pretty, s->indent);
109 static void to_json_list_iter(QObject *obj, void *opaque)
111 ToJsonIterState *s = opaque;
115 qstring_append(s->str, s->pretty ? "," : ", ");
119 qstring_append(s->str, "\n");
120 for (j = 0 ; j < s->indent ; j++)
121 qstring_append(s->str, " ");
124 to_json(obj, s->str, s->pretty, s->indent);
128 static void to_json(const QObject *obj, QString *str, int pretty, int indent)
130 switch (qobject_type(obj)) {
132 qstring_append(str, "null");
135 QInt *val = qobject_to_qint(obj);
138 snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val));
139 qstring_append(str, buffer);
142 case QTYPE_QSTRING: {
143 QString *val = qobject_to_qstring(obj);
149 ptr = qstring_get_str(val);
150 qstring_append(str, "\"");
152 for (; *ptr; ptr = end) {
153 cp = mod_utf8_codepoint(ptr, 6, &end);
156 qstring_append(str, "\\\"");
159 qstring_append(str, "\\\\");
162 qstring_append(str, "\\b");
165 qstring_append(str, "\\f");
168 qstring_append(str, "\\n");
171 qstring_append(str, "\\r");
174 qstring_append(str, "\\t");
178 cp = 0xFFFD; /* replacement character */
181 /* beyond BMP; need a surrogate pair */
182 snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
183 0xD800 + ((cp - 0x10000) >> 10),
184 0xDC00 + ((cp - 0x10000) & 0x3FF));
185 } else if (cp < 0x20 || cp >= 0x7F) {
186 snprintf(buf, sizeof(buf), "\\u%04X", cp);
191 qstring_append(str, buf);
195 qstring_append(str, "\"");
200 QDict *val = qobject_to_qdict(obj);
204 s.indent = indent + 1;
206 qstring_append(str, "{");
207 qdict_iter(val, to_json_dict_iter, &s);
210 qstring_append(str, "\n");
211 for (j = 0 ; j < indent ; j++)
212 qstring_append(str, " ");
214 qstring_append(str, "}");
219 QList *val = qobject_to_qlist(obj);
223 s.indent = indent + 1;
225 qstring_append(str, "[");
226 qlist_iter(val, (void *)to_json_list_iter, &s);
229 qstring_append(str, "\n");
230 for (j = 0 ; j < indent ; j++)
231 qstring_append(str, " ");
233 qstring_append(str, "]");
237 QFloat *val = qobject_to_qfloat(obj);
241 /* FIXME: snprintf() is locale dependent; but JSON requires
242 * numbers to be formatted as if in the C locale. Dependence
243 * on C locale is a pervasive issue in QEMU. */
244 /* FIXME: This risks printing Inf or NaN, which are not valid
246 /* FIXME: the default precision of 6 for %f often causes
247 * rounding errors; we should be using DBL_DECIMAL_DIG (17),
248 * and only rounding to a shorter number if the result would
249 * still produce the same floating point value. */
250 len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val));
251 while (len > 0 && buffer[len - 1] == '0') {
255 if (len && buffer[len - 1] == '.') {
261 qstring_append(str, buffer);
265 QBool *val = qobject_to_qbool(obj);
267 if (qbool_get_bool(val)) {
268 qstring_append(str, "true");
270 qstring_append(str, "false");
279 QString *qobject_to_json(const QObject *obj)
281 QString *str = qstring_new();
283 to_json(obj, str, 0, 0);
288 QString *qobject_to_json_pretty(const QObject *obj)
290 QString *str = qstring_new();
292 to_json(obj, str, 1, 0);