]>
git.ipfire.org Git - thirdparty/qemu.git/blob - qobject/qjson.c
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/qbool.h"
21 #include "qapi/qmp/qdict.h"
22 #include "qapi/qmp/qlist.h"
23 #include "qapi/qmp/qnum.h"
24 #include "qapi/qmp/qstring.h"
25 #include "qemu/unicode.h"
27 typedef struct JSONParsingState
29 JSONMessageParser parser
;
35 static void parse_json(JSONMessageParser
*parser
, GQueue
*tokens
)
37 JSONParsingState
*s
= container_of(parser
, JSONParsingState
, parser
);
39 s
->result
= json_parser_parse_err(tokens
, s
->ap
, &s
->err
);
42 QObject
*qobject_from_jsonv(const char *string
, va_list *ap
, Error
**errp
)
44 JSONParsingState state
= {};
48 json_message_parser_init(&state
.parser
, parse_json
);
49 json_message_parser_feed(&state
.parser
, string
, strlen(string
));
50 json_message_parser_flush(&state
.parser
);
51 json_message_parser_destroy(&state
.parser
);
53 error_propagate(errp
, state
.err
);
57 QObject
*qobject_from_json(const char *string
, Error
**errp
)
59 return qobject_from_jsonv(string
, NULL
, errp
);
63 * IMPORTANT: This function aborts on error, thus it must not
64 * be used with untrusted arguments.
66 QObject
*qobject_from_jsonf(const char *string
, ...)
72 obj
= qobject_from_jsonv(string
, &ap
, &error_abort
);
80 * Parse @string as JSON object with %-escapes interpolated.
81 * Abort on error. Do not use with untrusted @string.
82 * Return the resulting QDict. It is never null.
84 QDict
*qdict_from_jsonf_nofail(const char *string
, ...)
90 obj
= qobject_to(QDict
, qobject_from_jsonv(string
, &ap
, &error_abort
));
97 typedef struct ToJsonIterState
105 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
);
107 static void to_json_dict_iter(const char *key
, QObject
*obj
, void *opaque
)
109 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 qkey
= qstring_from_str(key
);
124 to_json(QOBJECT(qkey
), s
->str
, s
->pretty
, s
->indent
);
127 qstring_append(s
->str
, ": ");
128 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
132 static void to_json_list_iter(QObject
*obj
, void *opaque
)
134 ToJsonIterState
*s
= opaque
;
138 qstring_append(s
->str
, s
->pretty
? "," : ", ");
142 qstring_append(s
->str
, "\n");
143 for (j
= 0 ; j
< s
->indent
; j
++)
144 qstring_append(s
->str
, " ");
147 to_json(obj
, s
->str
, s
->pretty
, s
->indent
);
151 static void to_json(const QObject
*obj
, QString
*str
, int pretty
, int indent
)
153 switch (qobject_type(obj
)) {
155 qstring_append(str
, "null");
158 QNum
*val
= qobject_to(QNum
, obj
);
159 char *buffer
= qnum_to_string(val
);
160 qstring_append(str
, buffer
);
164 case QTYPE_QSTRING
: {
165 QString
*val
= qobject_to(QString
, obj
);
171 ptr
= qstring_get_str(val
);
172 qstring_append(str
, "\"");
174 for (; *ptr
; ptr
= end
) {
175 cp
= mod_utf8_codepoint(ptr
, 6, &end
);
178 qstring_append(str
, "\\\"");
181 qstring_append(str
, "\\\\");
184 qstring_append(str
, "\\b");
187 qstring_append(str
, "\\f");
190 qstring_append(str
, "\\n");
193 qstring_append(str
, "\\r");
196 qstring_append(str
, "\\t");
200 cp
= 0xFFFD; /* replacement character */
203 /* beyond BMP; need a surrogate pair */
204 snprintf(buf
, sizeof(buf
), "\\u%04X\\u%04X",
205 0xD800 + ((cp
- 0x10000) >> 10),
206 0xDC00 + ((cp
- 0x10000) & 0x3FF));
207 } else if (cp
< 0x20 || cp
>= 0x7F) {
208 snprintf(buf
, sizeof(buf
), "\\u%04X", cp
);
213 qstring_append(str
, buf
);
217 qstring_append(str
, "\"");
222 QDict
*val
= qobject_to(QDict
, obj
);
226 s
.indent
= indent
+ 1;
228 qstring_append(str
, "{");
229 qdict_iter(val
, to_json_dict_iter
, &s
);
232 qstring_append(str
, "\n");
233 for (j
= 0 ; j
< indent
; j
++)
234 qstring_append(str
, " ");
236 qstring_append(str
, "}");
241 QList
*val
= qobject_to(QList
, obj
);
245 s
.indent
= indent
+ 1;
247 qstring_append(str
, "[");
248 qlist_iter(val
, (void *)to_json_list_iter
, &s
);
251 qstring_append(str
, "\n");
252 for (j
= 0 ; j
< indent
; j
++)
253 qstring_append(str
, " ");
255 qstring_append(str
, "]");
259 QBool
*val
= qobject_to(QBool
, obj
);
261 if (qbool_get_bool(val
)) {
262 qstring_append(str
, "true");
264 qstring_append(str
, "false");
273 QString
*qobject_to_json(const QObject
*obj
)
275 QString
*str
= qstring_new();
277 to_json(obj
, str
, 0, 0);
282 QString
*qobject_to_json_pretty(const QObject
*obj
)
284 QString
*str
= qstring_new();
286 to_json(obj
, str
, 1, 0);