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 "qapi/qmp/json-lexer.h"
15 #include "qapi/qmp/json-parser.h"
16 #include "qapi/qmp/json-streamer.h"
17 #include "qapi/qmp/qjson.h"
18 #include "qapi/qmp/qint.h"
19 #include "qapi/qmp/qlist.h"
20 #include "qapi/qmp/qbool.h"
21 #include "qapi/qmp/qfloat.h"
22 #include "qapi/qmp/qdict.h"
24 typedef struct JSONParsingState
26 JSONMessageParser parser
;
31 static void parse_json(JSONMessageParser
*parser
, QList
*tokens
)
33 JSONParsingState
*s
= container_of(parser
, JSONParsingState
, parser
);
34 s
->result
= json_parser_parse(tokens
, s
->ap
);
37 QObject
*qobject_from_jsonv(const char *string
, va_list *ap
)
39 JSONParsingState state
= {};
43 json_message_parser_init(&state
.parser
, parse_json
);
44 json_message_parser_feed(&state
.parser
, string
, strlen(string
));
45 json_message_parser_flush(&state
.parser
);
46 json_message_parser_destroy(&state
.parser
);
51 QObject
*qobject_from_json(const char *string
)
53 return qobject_from_jsonv(string
, NULL
);
57 * IMPORTANT: This function aborts on error, thus it must not
58 * be used with untrusted arguments.
60 QObject
*qobject_from_jsonf(const char *string
, ...)
66 obj
= qobject_from_jsonv(string
, &ap
);
73 typedef struct ToJsonIterState
81 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
);
83 static void to_json_dict_iter(const char *key
, QObject
*obj
, void *opaque
)
85 ToJsonIterState
*s
= opaque
;
90 qstring_append(s
->str
, s
->pretty
? "," : ", ");
94 qstring_append(s
->str
, "\n");
95 for (j
= 0 ; j
< s
->indent
; j
++)
96 qstring_append(s
->str
, " ");
99 qkey
= qstring_from_str(key
);
100 to_json(QOBJECT(qkey
), s
->str
, s
->pretty
, s
->indent
);
103 qstring_append(s
->str
, ": ");
104 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
108 static void to_json_list_iter(QObject
*obj
, void *opaque
)
110 ToJsonIterState
*s
= opaque
;
114 qstring_append(s
->str
, s
->pretty
? "," : ", ");
118 qstring_append(s
->str
, "\n");
119 for (j
= 0 ; j
< s
->indent
; j
++)
120 qstring_append(s
->str
, " ");
123 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
127 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
)
129 switch (qobject_type(obj
)) {
131 QInt
*val
= qobject_to_qint(obj
);
134 snprintf(buffer
, sizeof(buffer
), "%" PRId64
, qint_get_int(val
));
135 qstring_append(str
, buffer
);
138 case QTYPE_QSTRING
: {
139 QString
*val
= qobject_to_qstring(obj
);
145 ptr
= qstring_get_str(val
);
146 qstring_append(str
, "\"");
148 for (; *ptr
; ptr
= end
) {
149 cp
= mod_utf8_codepoint(ptr
, 6, &end
);
152 qstring_append(str
, "\\\"");
155 qstring_append(str
, "\\\\");
158 qstring_append(str
, "\\b");
161 qstring_append(str
, "\\f");
164 qstring_append(str
, "\\n");
167 qstring_append(str
, "\\r");
170 qstring_append(str
, "\\t");
174 cp
= 0xFFFD; /* replacement character */
177 /* beyond BMP; need a surrogate pair */
178 snprintf(buf
, sizeof(buf
), "\\u%04X\\u%04X",
179 0xD800 + ((cp
- 0x10000) >> 10),
180 0xDC00 + ((cp
- 0x10000) & 0x3FF));
181 } else if (cp
< 0x20 || cp
>= 0x7F) {
182 snprintf(buf
, sizeof(buf
), "\\u%04X", cp
);
187 qstring_append(str
, buf
);
191 qstring_append(str
, "\"");
196 QDict
*val
= qobject_to_qdict(obj
);
200 s
.indent
= indent
+ 1;
202 qstring_append(str
, "{");
203 qdict_iter(val
, to_json_dict_iter
, &s
);
206 qstring_append(str
, "\n");
207 for (j
= 0 ; j
< indent
; j
++)
208 qstring_append(str
, " ");
210 qstring_append(str
, "}");
215 QList
*val
= qobject_to_qlist(obj
);
219 s
.indent
= indent
+ 1;
221 qstring_append(str
, "[");
222 qlist_iter(val
, (void *)to_json_list_iter
, &s
);
225 qstring_append(str
, "\n");
226 for (j
= 0 ; j
< indent
; j
++)
227 qstring_append(str
, " ");
229 qstring_append(str
, "]");
233 QFloat
*val
= qobject_to_qfloat(obj
);
237 len
= snprintf(buffer
, sizeof(buffer
), "%f", qfloat_get_double(val
));
238 while (len
> 0 && buffer
[len
- 1] == '0') {
242 if (len
&& buffer
[len
- 1] == '.') {
248 qstring_append(str
, buffer
);
252 QBool
*val
= qobject_to_qbool(obj
);
254 if (qbool_get_int(val
)) {
255 qstring_append(str
, "true");
257 qstring_append(str
, "false");
262 /* XXX: should QError be emitted? */
270 QString
*qobject_to_json(const QObject
*obj
)
272 QString
*str
= qstring_new();
274 to_json(obj
, str
, 0, 0);
279 QString
*qobject_to_json_pretty(const QObject
*obj
)
281 QString
*str
= qstring_new();
283 to_json(obj
, str
, 1, 0);