json: Fix latent parser aborts at end of input
[qemu/ar7.git] / qobject / qjson.c
blob7f6903648723a1aa9528190f3af980f5134a6fc1
1 /*
2 * QObject JSON integration
4 * Copyright IBM, Corp. 2009
6 * Authors:
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-streamer.h"
17 #include "qapi/qmp/qjson.h"
18 #include "qapi/qmp/qbool.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qlist.h"
21 #include "qapi/qmp/qnum.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qemu/unicode.h"
25 typedef struct JSONParsingState
27 JSONMessageParser parser;
28 QObject *result;
29 Error *err;
30 } JSONParsingState;
32 static void consume_json(void *opaque, QObject *json, Error *err)
34 JSONParsingState *s = opaque;
36 assert(!json != !err);
37 assert(!s->result || !s->err);
39 if (s->result) {
40 qobject_unref(s->result);
41 s->result = NULL;
42 error_setg(&s->err, "Expecting at most one JSON value");
44 if (s->err) {
45 qobject_unref(json);
46 error_free(err);
47 return;
49 s->result = json;
50 s->err = err;
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.
63 static QObject *qobject_from_jsonv(const char *string, va_list *ap,
64 Error **errp)
66 JSONParsingState state = {};
68 json_message_parser_init(&state.parser, consume_json, &state, ap);
69 json_message_parser_feed(&state.parser, string, strlen(string));
70 json_message_parser_flush(&state.parser);
71 json_message_parser_destroy(&state.parser);
73 error_propagate(errp, state.err);
74 return state.result;
77 QObject *qobject_from_json(const char *string, Error **errp)
79 return qobject_from_jsonv(string, NULL, errp);
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.
87 QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
89 va_list ap_copy;
90 QObject *obj;
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);
97 assert(obj);
98 return obj;
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.
106 QObject *qobject_from_jsonf_nofail(const char *string, ...)
108 QObject *obj;
109 va_list ap;
111 va_start(ap, string);
112 obj = qobject_from_vjsonf_nofail(string, ap);
113 va_end(ap);
115 return obj;
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.
123 QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
125 QDict *qdict;
127 qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
128 assert(qdict);
129 return qdict;
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.
137 QDict *qdict_from_jsonf_nofail(const char *string, ...)
139 QDict *qdict;
140 va_list ap;
142 va_start(ap, string);
143 qdict = qdict_from_vjsonf_nofail(string, ap);
144 va_end(ap);
145 return qdict;
148 typedef struct ToJsonIterState
150 int indent;
151 int pretty;
152 int count;
153 QString *str;
154 } ToJsonIterState;
156 static void to_json(const QObject *obj, QString *str, int pretty, int indent);
158 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
160 ToJsonIterState *s = opaque;
161 QString *qkey;
162 int j;
164 if (s->count) {
165 qstring_append(s->str, s->pretty ? "," : ", ");
168 if (s->pretty) {
169 qstring_append(s->str, "\n");
170 for (j = 0 ; j < s->indent ; j++)
171 qstring_append(s->str, " ");
174 qkey = qstring_from_str(key);
175 to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
176 qobject_unref(qkey);
178 qstring_append(s->str, ": ");
179 to_json(obj, s->str, s->pretty, s->indent);
180 s->count++;
183 static void to_json_list_iter(QObject *obj, void *opaque)
185 ToJsonIterState *s = opaque;
186 int j;
188 if (s->count) {
189 qstring_append(s->str, s->pretty ? "," : ", ");
192 if (s->pretty) {
193 qstring_append(s->str, "\n");
194 for (j = 0 ; j < s->indent ; j++)
195 qstring_append(s->str, " ");
198 to_json(obj, s->str, s->pretty, s->indent);
199 s->count++;
202 static void to_json(const QObject *obj, QString *str, int pretty, int indent)
204 switch (qobject_type(obj)) {
205 case QTYPE_QNULL:
206 qstring_append(str, "null");
207 break;
208 case QTYPE_QNUM: {
209 QNum *val = qobject_to(QNum, obj);
210 char *buffer = qnum_to_string(val);
211 qstring_append(str, buffer);
212 g_free(buffer);
213 break;
215 case QTYPE_QSTRING: {
216 QString *val = qobject_to(QString, obj);
217 const char *ptr;
218 int cp;
219 char buf[16];
220 char *end;
222 ptr = qstring_get_str(val);
223 qstring_append(str, "\"");
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 */
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;
264 qstring_append(str, buf);
268 qstring_append(str, "\"");
269 break;
271 case QTYPE_QDICT: {
272 ToJsonIterState s;
273 QDict *val = qobject_to(QDict, obj);
275 s.count = 0;
276 s.str = str;
277 s.indent = indent + 1;
278 s.pretty = pretty;
279 qstring_append(str, "{");
280 qdict_iter(val, to_json_dict_iter, &s);
281 if (pretty) {
282 int j;
283 qstring_append(str, "\n");
284 for (j = 0 ; j < indent ; j++)
285 qstring_append(str, " ");
287 qstring_append(str, "}");
288 break;
290 case QTYPE_QLIST: {
291 ToJsonIterState s;
292 QList *val = qobject_to(QList, obj);
294 s.count = 0;
295 s.str = str;
296 s.indent = indent + 1;
297 s.pretty = pretty;
298 qstring_append(str, "[");
299 qlist_iter(val, (void *)to_json_list_iter, &s);
300 if (pretty) {
301 int j;
302 qstring_append(str, "\n");
303 for (j = 0 ; j < indent ; j++)
304 qstring_append(str, " ");
306 qstring_append(str, "]");
307 break;
309 case QTYPE_QBOOL: {
310 QBool *val = qobject_to(QBool, obj);
312 if (qbool_get_bool(val)) {
313 qstring_append(str, "true");
314 } else {
315 qstring_append(str, "false");
317 break;
319 default:
320 abort();
324 QString *qobject_to_json(const QObject *obj)
326 QString *str = qstring_new();
328 to_json(obj, str, 0, 0);
330 return str;
333 QString *qobject_to_json_pretty(const QObject *obj)
335 QString *str = qstring_new();
337 to_json(obj, str, 1, 0);
339 return str;