]> git.proxmox.com Git - mirror_qemu.git/blame - qobject/qjson.c
migration: report SaveStateEntry id and name on failure
[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"
86cdf9ec 16#include "qapi/qmp/json-parser.h"
7b1b5d19 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
dd98e848
MA
73 if (!state.result && !state.err) {
74 error_setg(&state.err, "Expecting a JSON value");
75 }
76
99dbfd1d 77 error_propagate(errp, state.err);
b4748b9b
AL
78 return state.result;
79}
80
57348c2f 81QObject *qobject_from_json(const char *string, Error **errp)
8ff5a7d3 82{
57348c2f 83 return qobject_from_jsonv(string, NULL, errp);
8ff5a7d3
LC
84}
85
4ff18468
MA
86/*
87 * Parse @string as JSON value with %-escapes interpolated.
88 * Abort on error. Do not use with untrusted @string.
89 * Return the resulting QObject. It is never null.
90 */
91QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
92{
93 va_list ap_copy;
94 QObject *obj;
95
96 /* va_copy() is needed when va_list is an array type */
97 va_copy(ap_copy, ap);
98 obj = qobject_from_jsonv(string, &ap_copy, &error_abort);
99 va_end(ap_copy);
100
101 assert(obj);
102 return obj;
103}
104
6ce80fd8
MA
105/*
106 * Parse @string as JSON value with %-escapes interpolated.
107 * Abort on error. Do not use with untrusted @string.
108 * Return the resulting QObject. It is never null.
109 */
110QObject *qobject_from_jsonf_nofail(const char *string, ...)
b4748b9b 111{
8ff5a7d3 112 QObject *obj;
b4748b9b
AL
113 va_list ap;
114
115 va_start(ap, string);
4ff18468 116 obj = qobject_from_vjsonf_nofail(string, ap);
b4748b9b
AL
117 va_end(ap);
118
8ff5a7d3 119 return obj;
b4748b9b 120}
1fd825f7 121
4ff18468
MA
122/*
123 * Parse @string as JSON object with %-escapes interpolated.
124 * Abort on error. Do not use with untrusted @string.
125 * Return the resulting QDict. It is never null.
126 */
127QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
128{
129 QDict *qdict;
130
131 qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
132 assert(qdict);
133 return qdict;
134}
135
a193352f
MA
136/*
137 * Parse @string as JSON object with %-escapes interpolated.
138 * Abort on error. Do not use with untrusted @string.
139 * Return the resulting QDict. It is never null.
140 */
141QDict *qdict_from_jsonf_nofail(const char *string, ...)
142{
4ff18468 143 QDict *qdict;
a193352f
MA
144 va_list ap;
145
146 va_start(ap, string);
4ff18468 147 qdict = qdict_from_vjsonf_nofail(string, ap);
a193352f 148 va_end(ap);
4ff18468 149 return qdict;
a193352f
MA
150}
151
1fd825f7
AL
152typedef struct ToJsonIterState
153{
212b6008
DB
154 int indent;
155 int pretty;
1fd825f7
AL
156 int count;
157 QString *str;
158} ToJsonIterState;
159
212b6008 160static void to_json(const QObject *obj, QString *str, int pretty, int indent);
1fd825f7
AL
161
162static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
163{
164 ToJsonIterState *s = opaque;
165 QString *qkey;
212b6008 166 int j;
1fd825f7 167
4b58554a
HR
168 if (s->count) {
169 qstring_append(s->str, s->pretty ? "," : ", ");
170 }
212b6008
DB
171
172 if (s->pretty) {
173 qstring_append(s->str, "\n");
174 for (j = 0 ; j < s->indent ; j++)
175 qstring_append(s->str, " ");
1fd825f7
AL
176 }
177
178 qkey = qstring_from_str(key);
212b6008 179 to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
cb3e7f08 180 qobject_unref(qkey);
1fd825f7
AL
181
182 qstring_append(s->str, ": ");
212b6008 183 to_json(obj, s->str, s->pretty, s->indent);
1fd825f7
AL
184 s->count++;
185}
186
187static void to_json_list_iter(QObject *obj, void *opaque)
188{
189 ToJsonIterState *s = opaque;
212b6008 190 int j;
1fd825f7 191
4b58554a
HR
192 if (s->count) {
193 qstring_append(s->str, s->pretty ? "," : ", ");
194 }
212b6008
DB
195
196 if (s->pretty) {
197 qstring_append(s->str, "\n");
198 for (j = 0 ; j < s->indent ; j++)
199 qstring_append(s->str, " ");
1fd825f7
AL
200 }
201
212b6008 202 to_json(obj, s->str, s->pretty, s->indent);
1fd825f7
AL
203 s->count++;
204}
205
212b6008 206static void to_json(const QObject *obj, QString *str, int pretty, int indent)
1fd825f7
AL
207{
208 switch (qobject_type(obj)) {
481b002c
MA
209 case QTYPE_QNULL:
210 qstring_append(str, "null");
211 break;
01b2ffce 212 case QTYPE_QNUM: {
7dc847eb 213 QNum *val = qobject_to(QNum, obj);
01b2ffce 214 char *buffer = qnum_to_string(val);
1fd825f7 215 qstring_append(str, buffer);
01b2ffce 216 g_free(buffer);
1fd825f7
AL
217 break;
218 }
219 case QTYPE_QSTRING: {
7dc847eb 220 QString *val = qobject_to(QString, obj);
1fd825f7 221 const char *ptr;
e2ec3f97
MA
222 int cp;
223 char buf[16];
224 char *end;
1fd825f7
AL
225
226 ptr = qstring_get_str(val);
227 qstring_append(str, "\"");
e2ec3f97
MA
228
229 for (; *ptr; ptr = end) {
230 cp = mod_utf8_codepoint(ptr, 6, &end);
231 switch (cp) {
232 case '\"':
233 qstring_append(str, "\\\"");
234 break;
235 case '\\':
236 qstring_append(str, "\\\\");
237 break;
238 case '\b':
239 qstring_append(str, "\\b");
240 break;
241 case '\f':
242 qstring_append(str, "\\f");
243 break;
244 case '\n':
245 qstring_append(str, "\\n");
246 break;
247 case '\r':
248 qstring_append(str, "\\r");
249 break;
250 case '\t':
251 qstring_append(str, "\\t");
252 break;
253 default:
254 if (cp < 0) {
255 cp = 0xFFFD; /* replacement character */
1fd825f7 256 }
e2ec3f97
MA
257 if (cp > 0xFFFF) {
258 /* beyond BMP; need a surrogate pair */
259 snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
260 0xD800 + ((cp - 0x10000) >> 10),
261 0xDC00 + ((cp - 0x10000) & 0x3FF));
262 } else if (cp < 0x20 || cp >= 0x7F) {
263 snprintf(buf, sizeof(buf), "\\u%04X", cp);
264 } else {
265 buf[0] = cp;
266 buf[1] = 0;
1fd825f7 267 }
e2ec3f97
MA
268 qstring_append(str, buf);
269 }
270 };
271
1fd825f7
AL
272 qstring_append(str, "\"");
273 break;
274 }
275 case QTYPE_QDICT: {
276 ToJsonIterState s;
7dc847eb 277 QDict *val = qobject_to(QDict, obj);
1fd825f7
AL
278
279 s.count = 0;
280 s.str = str;
212b6008
DB
281 s.indent = indent + 1;
282 s.pretty = pretty;
1fd825f7
AL
283 qstring_append(str, "{");
284 qdict_iter(val, to_json_dict_iter, &s);
212b6008
DB
285 if (pretty) {
286 int j;
287 qstring_append(str, "\n");
288 for (j = 0 ; j < indent ; j++)
289 qstring_append(str, " ");
290 }
1fd825f7
AL
291 qstring_append(str, "}");
292 break;
293 }
294 case QTYPE_QLIST: {
295 ToJsonIterState s;
7dc847eb 296 QList *val = qobject_to(QList, obj);
1fd825f7
AL
297
298 s.count = 0;
299 s.str = str;
212b6008
DB
300 s.indent = indent + 1;
301 s.pretty = pretty;
1fd825f7
AL
302 qstring_append(str, "[");
303 qlist_iter(val, (void *)to_json_list_iter, &s);
212b6008
DB
304 if (pretty) {
305 int j;
306 qstring_append(str, "\n");
307 for (j = 0 ; j < indent ; j++)
308 qstring_append(str, " ");
309 }
1fd825f7
AL
310 qstring_append(str, "]");
311 break;
312 }
1fd825f7 313 case QTYPE_QBOOL: {
7dc847eb 314 QBool *val = qobject_to(QBool, obj);
1fd825f7 315
fc48ffc3 316 if (qbool_get_bool(val)) {
1fd825f7
AL
317 qstring_append(str, "true");
318 } else {
319 qstring_append(str, "false");
320 }
321 break;
322 }
a7c31816 323 default:
69dd62df 324 abort();
1fd825f7
AL
325 }
326}
327
328QString *qobject_to_json(const QObject *obj)
329{
330 QString *str = qstring_new();
331
212b6008
DB
332 to_json(obj, str, 0, 0);
333
334 return str;
335}
336
337QString *qobject_to_json_pretty(const QObject *obj)
338{
339 QString *str = qstring_new();
340
341 to_json(obj, str, 1, 0);
1fd825f7
AL
342
343 return str;
344}