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-parser.h"
17 #include "qapi/qmp/json-writer.h"
18 #include "qapi/qmp/qjson.h"
19 #include "qapi/qmp/qbool.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qapi/qmp/qlist.h"
22 #include "qapi/qmp/qnum.h"
23 #include "qapi/qmp/qstring.h"
25 typedef struct JSONParsingState {
26 JSONMessageParser parser;
31 static void consume_json(void *opaque, QObject *json, Error *err)
33 JSONParsingState *s = opaque;
35 assert(!json != !err);
36 assert(!s->result || !s->err);
39 qobject_unref(s->result);
41 error_setg(&s->err, "Expecting at most one JSON value");
53 * Parse @string as JSON value.
54 * If @ap is non-null, interpolate %-escapes.
55 * Takes ownership of %p arguments.
56 * On success, return the JSON value.
57 * On failure, store an error through @errp and return NULL.
58 * Ownership of %p arguments becomes indeterminate then. To avoid
59 * leaks, callers passing %p must terminate on error, e.g. by passing
62 static QObject *qobject_from_jsonv(const char *string, va_list *ap,
65 JSONParsingState state = {};
67 json_message_parser_init(&state.parser, consume_json, &state, ap);
68 json_message_parser_feed(&state.parser, string, strlen(string));
69 json_message_parser_flush(&state.parser);
70 json_message_parser_destroy(&state.parser);
72 if (!state.result && !state.err) {
73 error_setg(&state.err, "Expecting a JSON value");
76 error_propagate(errp, state.err);
80 QObject *qobject_from_json(const char *string, Error **errp)
82 return qobject_from_jsonv(string, NULL, errp);
86 * Parse @string as JSON value with %-escapes interpolated.
87 * Abort on error. Do not use with untrusted @string.
88 * Return the resulting QObject. It is never null.
90 QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
95 /* va_copy() is needed when va_list is an array type */
97 obj = qobject_from_jsonv(string, &ap_copy, &error_abort);
105 * Parse @string as JSON value with %-escapes interpolated.
106 * Abort on error. Do not use with untrusted @string.
107 * Return the resulting QObject. It is never null.
109 QObject *qobject_from_jsonf_nofail(const char *string, ...)
114 va_start(ap, string);
115 obj = qobject_from_vjsonf_nofail(string, ap);
122 * Parse @string as JSON object with %-escapes interpolated.
123 * Abort on error. Do not use with untrusted @string.
124 * Return the resulting QDict. It is never null.
126 QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
130 qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
136 * Parse @string as JSON object with %-escapes interpolated.
137 * Abort on error. Do not use with untrusted @string.
138 * Return the resulting QDict. It is never null.
140 QDict *qdict_from_jsonf_nofail(const char *string, ...)
145 va_start(ap, string);
146 qdict = qdict_from_vjsonf_nofail(string, ap);
151 static void to_json(JSONWriter *writer, const char *name,
154 switch (qobject_type(obj)) {
156 json_writer_null(writer, name);
159 QNum *val = qobject_to(QNum, obj);
163 json_writer_int64(writer, name, val->u.i64);
166 json_writer_uint64(writer, name, val->u.u64);
169 json_writer_double(writer, name, val->u.dbl);
176 case QTYPE_QSTRING: {
177 QString *val = qobject_to(QString, obj);
179 json_writer_str(writer, name, qstring_get_str(val));
183 QDict *val = qobject_to(QDict, obj);
184 const QDictEntry *entry;
186 json_writer_start_object(writer, name);
188 for (entry = qdict_first(val);
190 entry = qdict_next(val, entry)) {
191 to_json(writer, qdict_entry_key(entry), qdict_entry_value(entry));
194 json_writer_end_object(writer);
198 QList *val = qobject_to(QList, obj);
201 json_writer_start_array(writer, name);
203 QLIST_FOREACH_ENTRY(val, entry) {
204 to_json(writer, NULL, qlist_entry_obj(entry));
207 json_writer_end_array(writer);
211 QBool *val = qobject_to(QBool, obj);
213 json_writer_bool(writer, name, qbool_get_bool(val));
221 GString *qobject_to_json_pretty(const QObject *obj, bool pretty)
223 JSONWriter *writer = json_writer_new(pretty);
225 to_json(writer, NULL, obj);
226 return json_writer_get_and_free(writer);
229 GString *qobject_to_json(const QObject *obj)
231 return qobject_to_json_pretty(obj, false);