]> git.proxmox.com Git - mirror_qemu.git/blame - qobject/qjson.c
json: Assert json_parser_parse() consumes all tokens on success
[mirror_qemu.git] / qobject / qjson.c
CommitLineData
b4748b9b
AL
1/*
2 * QObject JSON integration
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
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.
11 *
12 */
13
f2ad72b3 14#include "qemu/osdep.h"
99dbfd1d 15#include "qapi/error.h"
7b1b5d19
PB
16#include "qapi/qmp/json-streamer.h"
17#include "qapi/qmp/qjson.h"
6b673957 18#include "qapi/qmp/qbool.h"
452fcdbc 19#include "qapi/qmp/qdict.h"
47e6b297 20#include "qapi/qmp/qlist.h"
15280c36 21#include "qapi/qmp/qnum.h"
fc81fa1e 22#include "qapi/qmp/qstring.h"
f348b6d1 23#include "qemu/unicode.h"
b4748b9b
AL
24
25typedef struct JSONParsingState
26{
27 JSONMessageParser parser;
b4748b9b 28 QObject *result;
99dbfd1d 29 Error *err;
b4748b9b
AL
30} JSONParsingState;
31
62815d85 32static void consume_json(void *opaque, QObject *json, Error *err)
b4748b9b 33{
62815d85 34 JSONParsingState *s = opaque;
99dbfd1d 35
2a4794ba
MA
36 assert(!json != !err);
37 assert(!s->result || !s->err);
38
39 if (s->result) {
40 qobject_unref(s->result);
41 s->result = NULL;
42 error_setg(&s->err, "Expecting at most one JSON value");
43 }
44 if (s->err) {
45 qobject_unref(json);
46 error_free(err);
47 return;
48 }
62815d85 49 s->result = json;
2a4794ba 50 s->err = err;
b4748b9b
AL
51}
52
2d36e843
MA
53/*
54 * Parse @string as JSON value.
55 * If @ap is non-null, interpolate %-escapes.
56 * Takes ownership of %p arguments.
57 * On success, return the JSON value.
58 * On failure, store an error through @errp and return NULL.
59 * Ownership of %p arguments becomes indeterminate then. To avoid
60 * leaks, callers passing %p must terminate on error, e.g. by passing
61 * &error_abort.
62 */
63static QObject *qobject_from_jsonv(const char *string, va_list *ap,
64 Error **errp)
b4748b9b
AL
65{
66 JSONParsingState state = {};
67
62815d85 68 json_message_parser_init(&state.parser, consume_json, &state, ap);
b4748b9b
AL
69 json_message_parser_feed(&state.parser, string, strlen(string));
70 json_message_parser_flush(&state.parser);
71 json_message_parser_destroy(&state.parser);
72
99dbfd1d 73 error_propagate(errp, state.err);
b4748b9b
AL
74 return state.result;
75}
76
57348c2f 77QObject *qobject_from_json(const char *string, Error **errp)
8ff5a7d3 78{
57348c2f 79 return qobject_from_jsonv(string, NULL, errp);
8ff5a7d3
LC
80}
81
4ff18468
MA
82/*
83 * Parse @string as JSON value with %-escapes interpolated.
84 * Abort on error. Do not use with untrusted @string.
85 * Return the resulting QObject. It is never null.
86 */
87QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
88{
89 va_list ap_copy;
90 QObject *obj;
91
92 /* va_copy() is needed when va_list is an array type */
93 va_copy(ap_copy, ap);
94 obj = qobject_from_jsonv(string, &ap_copy, &error_abort);
95 va_end(ap_copy);
96
97 assert(obj);
98 return obj;
99}
100
6ce80fd8
MA
101/*
102 * Parse @string as JSON value with %-escapes interpolated.
103 * Abort on error. Do not use with untrusted @string.
104 * Return the resulting QObject. It is never null.
105 */
106QObject *qobject_from_jsonf_nofail(const char *string, ...)
b4748b9b 107{
8ff5a7d3 108 QObject *obj;
b4748b9b
AL
109 va_list ap;
110
111 va_start(ap, string);
4ff18468 112 obj = qobject_from_vjsonf_nofail(string, ap);
b4748b9b
AL
113 va_end(ap);
114
8ff5a7d3 115 return obj;
b4748b9b 116}
1fd825f7 117
4ff18468
MA
118/*
119 * Parse @string as JSON object with %-escapes interpolated.
120 * Abort on error. Do not use with untrusted @string.
121 * Return the resulting QDict. It is never null.
122 */
123QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
124{
125 QDict *qdict;
126
127 qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
128 assert(qdict);
129 return qdict;
130}
131
a193352f
MA
132/*
133 * Parse @string as JSON object with %-escapes interpolated.
134 * Abort on error. Do not use with untrusted @string.
135 * Return the resulting QDict. It is never null.
136 */
137QDict *qdict_from_jsonf_nofail(const char *string, ...)
138{
4ff18468 139 QDict *qdict;
a193352f
MA
140 va_list ap;
141
142 va_start(ap, string);
4ff18468 143 qdict = qdict_from_vjsonf_nofail(string, ap);
a193352f 144 va_end(ap);
4ff18468 145 return qdict;
a193352f
MA
146}
147
1fd825f7
AL
148typedef struct ToJsonIterState
149{
212b6008
DB
150 int indent;
151 int pretty;
1fd825f7
AL
152 int count;
153 QString *str;
154} ToJsonIterState;
155
212b6008 156static void to_json(const QObject *obj, QString *str, int pretty, int indent);
1fd825f7
AL
157
158static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
159{
160 ToJsonIterState *s = opaque;
161 QString *qkey;
212b6008 162 int j;
1fd825f7 163
4b58554a
HR
164 if (s->count) {
165 qstring_append(s->str, s->pretty ? "," : ", ");
166 }
212b6008
DB
167
168 if (s->pretty) {
169 qstring_append(s->str, "\n");
170 for (j = 0 ; j < s->indent ; j++)
171 qstring_append(s->str, " ");
1fd825f7
AL
172 }
173
174 qkey = qstring_from_str(key);
212b6008 175 to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
cb3e7f08 176 qobject_unref(qkey);
1fd825f7
AL
177
178 qstring_append(s->str, ": ");
212b6008 179 to_json(obj, s->str, s->pretty, s->indent);
1fd825f7
AL
180 s->count++;
181}
182
183static void to_json_list_iter(QObject *obj, void *opaque)
184{
185 ToJsonIterState *s = opaque;
212b6008 186 int j;
1fd825f7 187
4b58554a
HR
188 if (s->count) {
189 qstring_append(s->str, s->pretty ? "," : ", ");
190 }
212b6008
DB
191
192 if (s->pretty) {
193 qstring_append(s->str, "\n");
194 for (j = 0 ; j < s->indent ; j++)
195 qstring_append(s->str, " ");
1fd825f7
AL
196 }
197
212b6008 198 to_json(obj, s->str, s->pretty, s->indent);
1fd825f7
AL
199 s->count++;
200}
201
212b6008 202static void to_json(const QObject *obj, QString *str, int pretty, int indent)
1fd825f7
AL
203{
204 switch (qobject_type(obj)) {
481b002c
MA
205 case QTYPE_QNULL:
206 qstring_append(str, "null");
207 break;
01b2ffce 208 case QTYPE_QNUM: {
7dc847eb 209 QNum *val = qobject_to(QNum, obj);
01b2ffce 210 char *buffer = qnum_to_string(val);
1fd825f7 211 qstring_append(str, buffer);
01b2ffce 212 g_free(buffer);
1fd825f7
AL
213 break;
214 }
215 case QTYPE_QSTRING: {
7dc847eb 216 QString *val = qobject_to(QString, obj);
1fd825f7 217 const char *ptr;
e2ec3f97
MA
218 int cp;
219 char buf[16];
220 char *end;
1fd825f7
AL
221
222 ptr = qstring_get_str(val);
223 qstring_append(str, "\"");
e2ec3f97
MA
224
225 for (; *ptr; ptr = end) {
226 cp = mod_utf8_codepoint(ptr, 6, &end);
227 switch (cp) {
228 case '\"':
229 qstring_append(str, "\\\"");
230 break;
231 case '\\':
232 qstring_append(str, "\\\\");
233 break;
234 case '\b':
235 qstring_append(str, "\\b");
236 break;
237 case '\f':
238 qstring_append(str, "\\f");
239 break;
240 case '\n':
241 qstring_append(str, "\\n");
242 break;
243 case '\r':
244 qstring_append(str, "\\r");
245 break;
246 case '\t':
247 qstring_append(str, "\\t");
248 break;
249 default:
250 if (cp < 0) {
251 cp = 0xFFFD; /* replacement character */
1fd825f7 252 }
e2ec3f97
MA
253 if (cp > 0xFFFF) {
254 /* beyond BMP; need a surrogate pair */
255 snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
256 0xD800 + ((cp - 0x10000) >> 10),
257 0xDC00 + ((cp - 0x10000) & 0x3FF));
258 } else if (cp < 0x20 || cp >= 0x7F) {
259 snprintf(buf, sizeof(buf), "\\u%04X", cp);
260 } else {
261 buf[0] = cp;
262 buf[1] = 0;
1fd825f7 263 }
e2ec3f97
MA
264 qstring_append(str, buf);
265 }
266 };
267
1fd825f7
AL
268 qstring_append(str, "\"");
269 break;
270 }
271 case QTYPE_QDICT: {
272 ToJsonIterState s;
7dc847eb 273 QDict *val = qobject_to(QDict, obj);
1fd825f7
AL
274
275 s.count = 0;
276 s.str = str;
212b6008
DB
277 s.indent = indent + 1;
278 s.pretty = pretty;
1fd825f7
AL
279 qstring_append(str, "{");
280 qdict_iter(val, to_json_dict_iter, &s);
212b6008
DB
281 if (pretty) {
282 int j;
283 qstring_append(str, "\n");
284 for (j = 0 ; j < indent ; j++)
285 qstring_append(str, " ");
286 }
1fd825f7
AL
287 qstring_append(str, "}");
288 break;
289 }
290 case QTYPE_QLIST: {
291 ToJsonIterState s;
7dc847eb 292 QList *val = qobject_to(QList, obj);
1fd825f7
AL
293
294 s.count = 0;
295 s.str = str;
212b6008
DB
296 s.indent = indent + 1;
297 s.pretty = pretty;
1fd825f7
AL
298 qstring_append(str, "[");
299 qlist_iter(val, (void *)to_json_list_iter, &s);
212b6008
DB
300 if (pretty) {
301 int j;
302 qstring_append(str, "\n");
303 for (j = 0 ; j < indent ; j++)
304 qstring_append(str, " ");
305 }
1fd825f7
AL
306 qstring_append(str, "]");
307 break;
308 }
1fd825f7 309 case QTYPE_QBOOL: {
7dc847eb 310 QBool *val = qobject_to(QBool, obj);
1fd825f7 311
fc48ffc3 312 if (qbool_get_bool(val)) {
1fd825f7
AL
313 qstring_append(str, "true");
314 } else {
315 qstring_append(str, "false");
316 }
317 break;
318 }
a7c31816 319 default:
69dd62df 320 abort();
1fd825f7
AL
321 }
322}
323
324QString *qobject_to_json(const QObject *obj)
325{
326 QString *str = qstring_new();
327
212b6008
DB
328 to_json(obj, str, 0, 0);
329
330 return str;
331}
332
333QString *qobject_to_json_pretty(const QObject *obj)
334{
335 QString *str = qstring_new();
336
337 to_json(obj, str, 1, 0);
1fd825f7
AL
338
339 return str;
340}