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 QNum
*val
= qobject_to_qnum(obj
);
137 char *buffer
= qnum_to_string(val
);
138 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 QBool
*val
= qobject_to_qbool(obj
);
239 if (qbool_get_bool(val
)) {
240 qstring_append(str
, "true");
242 qstring_append(str
, "false");
251 QString
*qobject_to_json(const QObject
*obj
)
253 QString
*str
= qstring_new();
255 to_json(obj
, str
, 0, 0);
260 QString
*qobject_to_json_pretty(const QObject
*obj
)
262 QString
*str
= qstring_new();
264 to_json(obj
, str
, 1, 0);