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/types.h"
20 #include "qemu/unicode.h"
22 typedef struct JSONParsingState
24 JSONMessageParser parser;
29 static void parse_json(JSONMessageParser *parser, GQueue *tokens)
31 JSONParsingState *s = container_of(parser, JSONParsingState, parser);
32 s->result = json_parser_parse(tokens, s->ap);
35 QObject *qobject_from_jsonv(const char *string, va_list *ap)
37 JSONParsingState state = {};
41 json_message_parser_init(&state.parser, parse_json);
42 json_message_parser_feed(&state.parser, string, strlen(string));
43 json_message_parser_flush(&state.parser);
44 json_message_parser_destroy(&state.parser);
49 QObject *qobject_from_json(const char *string)
51 return qobject_from_jsonv(string, NULL);
55 * IMPORTANT: This function aborts on error, thus it must not
56 * be used with untrusted arguments.
58 QObject *qobject_from_jsonf(const char *string, ...)
64 obj = qobject_from_jsonv(string, &ap);
71 typedef struct ToJsonIterState
79 static void to_json(const QObject *obj, QString *str, int pretty, int indent);
81 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
83 ToJsonIterState *s = opaque;
88 qstring_append(s->str, s->pretty ? "," : ", ");
92 qstring_append(s->str, "\n");
93 for (j = 0 ; j < s->indent ; j++)
94 qstring_append(s->str, " ");
97 qkey = qstring_from_str(key);
98 to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
101 qstring_append(s->str, ": ");
102 to_json(obj, s->str, s->pretty, s->indent);
106 static void to_json_list_iter(QObject *obj, void *opaque)
108 ToJsonIterState *s = opaque;
112 qstring_append(s->str, s->pretty ? "," : ", ");
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 qstring_append(str, "null");
132 QInt *val = qobject_to_qint(obj);
135 snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val));
136 qstring_append(str, buffer);
139 case QTYPE_QSTRING: {
140 QString *val = qobject_to_qstring(obj);
146 ptr = qstring_get_str(val);
147 qstring_append(str, "\"");
149 for (; *ptr; ptr = end) {
150 cp = mod_utf8_codepoint(ptr, 6, &end);
153 qstring_append(str, "\\\"");
156 qstring_append(str, "\\\\");
159 qstring_append(str, "\\b");
162 qstring_append(str, "\\f");
165 qstring_append(str, "\\n");
168 qstring_append(str, "\\r");
171 qstring_append(str, "\\t");
175 cp = 0xFFFD; /* replacement character */
178 /* beyond BMP; need a surrogate pair */
179 snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
180 0xD800 + ((cp - 0x10000) >> 10),
181 0xDC00 + ((cp - 0x10000) & 0x3FF));
182 } else if (cp < 0x20 || cp >= 0x7F) {
183 snprintf(buf, sizeof(buf), "\\u%04X", cp);
188 qstring_append(str, buf);
192 qstring_append(str, "\"");
197 QDict *val = qobject_to_qdict(obj);
201 s.indent = indent + 1;
203 qstring_append(str, "{");
204 qdict_iter(val, to_json_dict_iter, &s);
207 qstring_append(str, "\n");
208 for (j = 0 ; j < indent ; j++)
209 qstring_append(str, " ");
211 qstring_append(str, "}");
216 QList *val = qobject_to_qlist(obj);
220 s.indent = indent + 1;
222 qstring_append(str, "[");
223 qlist_iter(val, (void *)to_json_list_iter, &s);
226 qstring_append(str, "\n");
227 for (j = 0 ; j < indent ; j++)
228 qstring_append(str, " ");
230 qstring_append(str, "]");
234 QFloat *val = qobject_to_qfloat(obj);
238 /* FIXME: snprintf() is locale dependent; but JSON requires
239 * numbers to be formatted as if in the C locale. Dependence
240 * on C locale is a pervasive issue in QEMU. */
241 /* FIXME: This risks printing Inf or NaN, which are not valid
243 /* FIXME: the default precision of 6 for %f often causes
244 * rounding errors; we should be using DBL_DECIMAL_DIG (17),
245 * and only rounding to a shorter number if the result would
246 * still produce the same floating point value. */
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_bool(val)) {
265 qstring_append(str, "true");
267 qstring_append(str, "false");
276 QString *qobject_to_json(const QObject *obj)
278 QString *str = qstring_new();
280 to_json(obj, str, 0, 0);
285 QString *qobject_to_json_pretty(const QObject *obj)
287 QString *str = qstring_new();
289 to_json(obj, str, 1, 0);